Skip to content

Instantly share code, notes, and snippets.

@natecraddock
Created August 3, 2016 20:58
Show Gist options
  • Select an option

  • Save natecraddock/7b28f68e7f8dbaf0ba5df54d7995e9e8 to your computer and use it in GitHub Desktop.

Select an option

Save natecraddock/7b28f68e7f8dbaf0ba5df54d7995e9e8 to your computer and use it in GitHub Desktop.
Some simple Python scripts to generate every possible an n x n image of black and transparent pixels. The other one merges the images together
# Nathan Craddock 2016
from PIL import Image
import random
import os
from subprocess import run
from itertools import product
size = (4, 4)
colors = [(0, 0, 0, 0), (0, 0, 0, 255)]
filepath = "images"
def create_image(name, pattern):
img = Image.new('RGBA', size)
i = 0
pixels = img.load()
for y in range(img.size[1]):
for x in range(img.size[0]):
if pattern[i]:
col = colors[1]
else:
col = colors[0]
pixels[x, y] = col
i += 1
img.save(os.path.join(filepath, str(name)) + '.png')
patterns = product([False, True], repeat=size[0] * size[1])
image_name = 1
for p in patterns:
create_image(image_name, list(p))
image_name += 1
from PIL import Image
import os
size = (1024, 1024)
filepath = "images"
img = Image.new('RGBA', size)
pixels = img.load()
i = 1
for y in range(256):
for x in range(256):
# We are at the coords of a specific image, time to add the pixels
part = Image.open(os.path.join(filepath, str(i)) + '.png')
part_pixels = part.load()
for py in range(part.size[1]):
for px in range(part.size[0]):
pixels[x * 4 + px, y * 4 + py] = part_pixels[px, py]
i += 1
img.save('final_merge.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment