Embed any image into a prime number.
Project description
Primify
Transform any image into a prime number that looks like the image if glanced upon from far away.
How does it work
We proceed in 5 steps:

We resize the image to contain at most a
maxdigits
amount of pixels. 
Run various image processing steps like edge enhancement and smoothing before converting the image into greyscale.

We then quantise the image into just having 5 to 10 greyness levels.

Now we map each greyness level to a digit, et voila, we have embedded the picture into a number.

It now remains to tweak some of the digits until we find a prime number that still looks like the image.
Note: According to the prime number theorem, the density of prime numbers is asymptotically of order 1/log(n). Hence, if we have some number n with m digits, the number of primality tests that we expect to do until we hit a prime number is roughly proportional to m. Since we use the Baillie–PSW primality test, the overall expected computational complexity of our prime searching procedure is O(n*log(n)³).
How to use
Simply get the primify
command line tool via pip install primify
.
You can also import the PrimeImage
class from primify.base
or use cli.py
as a commandline script.
Commandline tool
usage: primify [h] [image IMAGE_PATH] [maxdigits MAX_DIGITS]
[outputfile OUTPUT_FILE]
Commandline tool for converting images to primes
optional arguments:
h, help show this help message and exit
image IMAGE_PATH, i IMAGE_PATH
Source image to be converted
maxdigits MAX_DIGITS, d MAX_DIGITS
Maximal number of digits the prime can have
outputfile OUTPUT_FILE, o OUTPUT_FILE
File name of the file containing the prime.
Made by Levi B.
Thus, if you have the source image at ./source.png
and you want to convert it into a prime contained in ./prime.txt
which has at most 5000 digits. Then you should run:
primify v image ./source.png maxdigits 5000 outputfile prime.txt
Importing the PrimeImage class
You can also simply import the PrimeImage
class from primify.base
and use that class in your own code. Check the documentation for details on how to interact with the underlying API.
Additional Material
Daniel Temkin wrote a lovely article on his blog esoteric.codes giving some interesting insight and background for this tool. You can read it here.
Project details
Release history Release notifications  RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for primify2.0.0py2.py3noneany.whl
Algorithm  Hash digest  

SHA256  119fef92135f99460782064869653926e5d73d765f7336005ed96bd729092f06 

MD5  cfa7a3afd08fc6edcc0453a2ace2adc4 

BLAKE2b256  9f3489ab67b2f2e4cbf214c8bdfa743da4eb23c19ae918de582f8e84933c762d 