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 a number 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

You can either import the PrimeImage class from primify.py or use primify.py as a command-line tool.

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.py [-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:

python primify.py -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.py 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.

Source Distribution

primify-1.0.1.tar.gz (1.4 MB view details)

Uploaded Source

File details

Details for the file primify-1.0.1.tar.gz.

File metadata

  • Download URL: primify-1.0.1.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.3

File hashes

Hashes for primify-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3b4336c43d3a37d7fae819526e53131f9fb6684433fb3368b567d6b9256d59d3
MD5 69e5b420d67efedf22754c08ca56966c
BLAKE2b-256 2561b9090942832ce1ecd2feb6aeb9dd65ee2ae9d3394ef5c6d53c1c7f220e15

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