Last active
August 15, 2016 19:11
-
-
Save gamesbook/b29ad84bb596f6ebba991778feeee4a0 to your computer and use it in GitHub Desktop.
Generate a colour swathe from colours in an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Purpose: Generate a colour swathe from colours in an image | |
Author: D Hohls | |
Date: 2016/08/16 | |
Requires: | |
pip install pillow | |
Usage: | |
python swathe.py -i IMAGE_IN.PNG -o IMAGE_OUT.PNG | |
If output (-o) not supplied, name will default to IMAGE_IN_swathe.PNG | |
Small black & white rectangles can be added to the centre of each color, | |
to show contrasts | |
With debug on, the script will display hexadecimal and RGBA colors in a list | |
Tags: | |
python, color, colour, swathe, swatch, image, contrast, hue, RGB | |
""" | |
from PIL import Image | |
import argparse | |
import os | |
def positive_int(value): | |
"""Check `value` is positive integer""" | |
ivalue = int(value) | |
if ivalue < 0: | |
raise argparse.ArgumentTypeError( | |
"%s is not a positive integer value" % value) | |
return ivalue | |
def main(args): | |
"""Create color swathe image from colors in input image.""" | |
fname = args.input #'/home/derek/Desktop/excel_props/bluepinkyellow.png' | |
if not fname: | |
print "Input filename required! Help via:\npython swathe.py --help" | |
return | |
fname_out = args.output | |
if not fname_out: | |
filename, file_extension = os.path.splitext(fname) | |
fname_out = '%s_%s%s' % (filename, 'swathe', file_extension) | |
base = args.width or 24 | |
colors = Image.open(fname).getcolors() | |
width = base * len(colors) | |
height = base * 2 | |
pixels = Image.new('RGBA', (width, height)) | |
_colors = [color[1] for color in colors] | |
_sorted = sorted(_colors, key=lambda ele: (ele[0], ele[1], ele[2])) | |
if args.debug: | |
print "# x Hex RGBA " | |
print "--------------------------------------" | |
for key, color in enumerate(_sorted): | |
pos = base * key | |
if args.debug: | |
print "%03d %05d %s %s" % ( | |
key, pos, '#%02x%02x%02x'.upper() % color[0:3], color) | |
for x in range(pos, base + pos): | |
for y in range(0, height): | |
pixels.putpixel((x, y), color) | |
if args.contrast: | |
left = base / 4 + pos | |
top = base / 4 | |
black = (0, 0, 0, 255) | |
for x in range(left, left + int(base / 2)): | |
for y in range(top * 3, top * 3 + top): | |
pixels.putpixel((x, y), black) | |
white = (255, 255, 255, 255) | |
for x in range(left, left + int(base / 2)): | |
for y in range(top * 4, top * 4 + top): | |
pixels.putpixel((x, y), white) | |
pixels.save(fname_out) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'-i', '--input', | |
help="Name of input file") | |
parser.add_argument( | |
'-o', '--output', | |
help="Name of output file") | |
parser.add_argument( | |
'-w', '--width', default=24, type=positive_int, | |
help="Width per color in pixels (default=24)") | |
parser.add_argument( | |
'-c', '--contrast', default=False, action='store_true', | |
help="Add black&white marker to each color") | |
parser.add_argument( | |
'-d', '--debug', default=False, action='store_true', | |
help="Switch debug on") | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment