Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Feb 2, 2017

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
You can’t perform that action at this time.