Skip to main content

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.

result Build Status

How does it work

We proceed in 5 steps:

  1. We resize the image to contain at most a --max_digits amount of pixels.

  2. Run various image processing steps like edge enhancement and smoothing before converting the image into grey-scale.

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

Note: There are multiple different methods for quantising the color levels and some methods will produces better results for some images. Make sure to play around with the --method parameter to get the best result.

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

  2. 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.primify_base or use cli.py as a command-line script.

Requirements

Make sure you meet all the dependencies inside the requirements.txt. I would recommend to use pypy, as it seems to decrease compiling time by about 20%.

Command-line tool

usage: primify [-h] [--image IMAGE_PATH] [--max_digits MAX_DIGITS]
                  [--method {0,1,2}] [--output_dir OUTPUT_DIR]
                  [--output_file OUTPUT_FILE] [-v]

Command-line tool for converting images to primes

optional arguments:
  -h, --help            show this help message and exit
  --image IMAGE_PATH    Source image to be converted.
  --max_digits MAX_DIGITS
                        Maximal number of digits the prime can have.
  --method {0,1,2}      Method for converting image. Tweak 'till happy
  --output_dir OUTPUT_DIR
                        Directory of the output text file
  --output_file OUTPUT_FILE
                        File name of the text file containing the prime.
  -v                    Verbose output (Recommended!)

Thus, if you have the source image at ./source.png and you want to convert it into a prime contained in ./prime/prime.txt which has at most 5000 digits and using conversion method 0 (other options are 1 or 2). Then you should run:

primify -v --image ./source.png --max_digits 5000 --method 0 --output_dir ./prime/ --output_file prime.txt

Importing the PrimeImage class

you can also simply import the PrimeImage class from primify.primify_base and use that class in your own code. Take a look at the source code to see what methods and attributes there are.

Project details


Download files

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

Files for primify, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size primify-1.0.2.tar.gz (1.4 MB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page