Skip to content

Instantly share code, notes, and snippets.

@ethanfuerst
Last active November 7, 2022 14:49
Show Gist options
  • Save ethanfuerst/b9d45e420dc8f4f225be9771509cdd09 to your computer and use it in GitHub Desktop.
Save ethanfuerst/b9d45e420dc8f4f225be9771509cdd09 to your computer and use it in GitHub Desktop.
Prime Number Background Generator
from sympy import sieve
from PIL import Image, ImageDraw
# I like to make SCREEN_WIDTH = BOXES_WIDE. This will make each box 1 pixel.
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 2400
BOXES_WIDE = 1000
box_size = SCREEN_WIDTH // BOXES_WIDE
BOXES_LONG = 2400 // box_size
num_primes = BOXES_LONG * BOXES_WIDE
sieve.extend(num_primes)
im = Image.new(mode="L", size=(SCREEN_WIDTH, SCREEN_HEIGHT))
draw = ImageDraw.Draw(im)
col_num = 0
row_num = 0
for box_num in range(1, num_primes + 1):
if box_num in sieve:
x1 = col_num * box_size
y1 = row_num * box_size
x2 = (col_num + 1) * box_size - 1
y2 = (row_num + 1) * box_size - 1
draw.rectangle([(x1, y1), (x2, y2)], fill=175)
col_num += 1
if col_num % BOXES_WIDE == 0:
col_num = 0
row_num += 1
im.show()
@ethanfuerst
Copy link
Author

ethanfuerst commented Nov 7, 2022

This script places grey boxes (or pixels) along a blank black image if the corresponding number of the box is prime. The result kinda looks like static from an old TV but you can see some cool patterns too! I like to use it for my phone background.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment