Skip to main content

Stretch circles into squares and squish squares into circles

Reason this release was yanked:

not installable (missing py_modules in setup.cfg)

Project description

squircle.py is a Python utility for stretching circles into squares and squishing squares into circles. It requires Python 3.6 or later.

Installation

pip install squircle

Usage:

import numpy as np
from PIL import Image

from squircle import to_circle, to_square

square = np.asarray(Image.open('some-square-image.jpg'))
circle = to_circle(square)
and_back_to_square = to_square(circle)

there's 3 stretching methods you can choose from

>>> from squircle import methods
>>> list(methods.keys())
['fgs', 'stretch', 'elliptical']
>>> circle = to_circle(square, method='elliptical')

Stretching methods

Fernández-Guasti squircle (fgs)

The Fernández-Guasti squircle is used by default.

https://squircular.blogspot.com/2015/09/fernandez-guastis-squircle.html

http://mathworld.wolfram.com/Squircle.html

Simple Stretching (stretch)

This method "just linearly stretches each point radially so that the rim of the circle matches the rim of the square".

https://squircular.blogspot.com/2015/09/elliptical-arc-mapping.html

Elliptical grid mapping (elliptical)

"The way I went about this was to think of a line of constant x (as well as a line of constant y) getting mapped to an ellipse in the circle"

https://mathproofs.blogspot.com/2005/07/mapping-square-to-circle.html

https://squircular.blogspot.com/2015/09/mapping-circle-to-square.html

Schwarz-Christoffel conformal mapping

raise NotImplementedError. The math is difficult.

https://squircular.blogspot.com/2015/09/schwarz-christoffel-mapping.html

http://jcgt.org/published/0005/02/01/

Development

After pip install tox you can run squircle's (limited) test set with

tox

On Ubuntu, you also need the following dependencies for numpy and matplotlib

sudo apt install python3-dev libjpeg-dev zlib1g-dev libfreetype6-dev

You can visually inspect the transformations by uncommenting the matplotlib code in test_squircle.py and then invoking the tests directly with pytest instead of tox

pytest

but you'll need to install the dependencies of the tests manually, which you can do with pip install pytest numpy matplotlib pillow.

TODOs

If you would like to help with this project, the open issues on GitHub should list some ideas.

Credits

This code is converted from the C++ sources on Chamberlain Fong's blog posts, which (I think) are based on his paper Analytical Methods for Squaring the Disc (2014).

The square image in test_images/ is https://commons.wikimedia.org/wiki/File:Graph-paper.svg and the circle is taken from Chamberlain Fong's blog posts.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

squircle-0.2.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

squircle-0.2.1-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file squircle-0.2.1.tar.gz.

File metadata

  • Download URL: squircle-0.2.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0+

File hashes

Hashes for squircle-0.2.1.tar.gz
Algorithm Hash digest
SHA256 be3c6e4d9a460dd762a8e9bf00d53cfed34380f166e7aeb09a85fc51516a3692
MD5 141420dd8f28cf43df4b1077bef97e85
BLAKE2b-256 6e9858cfba89a30ed5636657fc4800c4d5b7f5247efeccbb8906df8fd894a802

See more details on using hashes here.

File details

Details for the file squircle-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: squircle-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0+

File hashes

Hashes for squircle-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9449e3418ca3fc2043f0837556a27878b1f927cc3c0e4c1906e06fcfedfe3846
MD5 acaccc680abae9efe222eb9c2695be86
BLAKE2b-256 b44274f1cd49c3dc3566dc0e5b48ed1c1fc154f66d9fe65efbc4658e61785a4a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page