Skip to content

Instantly share code, notes, and snippets.

@nejdetckenobi
Created February 2, 2017 13:30
Show Gist options
  • Save nejdetckenobi/d21047f478c3f18d3cb35ecd01aabcb2 to your computer and use it in GitHub Desktop.
Save nejdetckenobi/d21047f478c3f18d3cb35ecd01aabcb2 to your computer and use it in GitHub Desktop.
A python command line tool to create mosaic pics
from PIL import Image
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--input', dest='input_file', type=str, required=True)
parser.add_argument('--size', dest='pix_size', type=int, required=True)
parser.add_argument('--output', dest='output_file', type=str, required=True)
args = parser.parse_args()
background_color = (255,)*3
pix_size = args.pix_size
image = Image.open(args.input_file)
image = image.resize((image.size[0]//pix_size, image.size[1]//pix_size),
Image.NEAREST)
image = image.resize((image.size[0]*pix_size, image.size[1]*pix_size),
Image.NEAREST)
pixel = image.load()
for i in range(0, image.size[0], pix_size):
for j in range(0, image.size[1], pix_size):
for r in range(pix_size):
pixel[i+r, j] = background_color
pixel[i, j+r] = background_color
image.save(args.output_file, 'png')
@nejdetckenobi
Copy link
Author

Usage:

./pixelization.py --input "/path/to/image.png" --output "/path/to/output.png" --size GRID_CELL_SIZE

Output is always a PNG file.

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