Python Image Libraryで水玉模様の壁紙作成 ref: http://qiita.com/items/3623a4771a22e85839e9
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
# -*- coding: utf-8 -*- | |
from PIL import Image | |
from PIL import ImageDraw | |
import colorsys | |
import sys | |
### http://code.activestate.com/recipes/266466-html-colors-tofrom-rgb-tuples/history/2/ | |
def RGBToHTMLColor(rgb_tuple): | |
""" convert an (R, G, B) tuple to #RRGGBB """ | |
hexcolor = '#%02x%02x%02x' % rgb_tuple | |
# that's it! '%02x' means zero-padded, 2-digit hex values | |
return hexcolor | |
def HTMLColorToRGB(colorstring): | |
""" convert #RRGGBB to an (R, G, B) tuple """ | |
colorstring = colorstring.strip() | |
if colorstring[0] == '#': colorstring = colorstring[1:] | |
if len(colorstring) != 6: | |
raise(ValueError, "input #%s is not in #RRGGBB format" % colorstring) | |
r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:] | |
r, g, b = [int(n, 16) for n in (r, g, b)] | |
return (r, g, b) | |
def HTMLColorToPILColor(colorstring): | |
""" converts #RRGGBB to PIL-compatible integers""" | |
colorstring = colorstring.strip() | |
while colorstring[0] == '#': colorstring = colorstring[1:] | |
# get bytes in reverse order to deal with PIL quirk | |
colorstring = colorstring[-2:] + colorstring[2:4] + colorstring[:2] | |
# finally, make it numeric | |
color = int(colorstring, 16) | |
return color | |
def PILColorToRGB(pil_color): | |
""" convert a PIL-compatible integer into an (r, g, b) tuple """ | |
hexstr = '%06x' % pil_color | |
# reverse byte order | |
r, g, b = hexstr[4:], hexstr[2:4], hexstr[:2] | |
r, g, b = [int(n, 16) for n in (r, g, b)] | |
return (r, g, b) | |
def PILColorToHTMLColor(pil_integer): | |
return RGBToHTMLColor(PILColorToRGB(pil_integer)) | |
def RGBToPILColor(rgb_tuple): | |
return HTMLColorToPILColor(RGBToHTMLColor(rgb_tuple)) | |
args = sys.argv | |
if(len(args) != 3): | |
print("Args Error"); | |
print("Usage %s [BaseHLS] [SubHLS]" % args[0]) | |
quit() | |
colors = [] | |
# 引数の解析 | |
for arg in args[1:]: | |
a = arg.split(":") | |
rgb1 = colorsys.hls_to_rgb(int(a[0]) / 360, int(a[1]) / 100, int(a[2]) / 100) | |
color = RGBToPILColor((rgb1[0] * 255, rgb1[1] * 255, rgb1[2] * 255)) | |
print("added color:", color) | |
colors += [color] | |
img = Image.new("RGB", (100, 100), "white") | |
hw = img.size[0] / 2 | |
hh = img.size[1] / 2 | |
draw = ImageDraw.Draw(img) | |
for i, c in zip(range(4), colors + colors[::-1]): | |
im = divmod(i, 2) | |
r = im[0] * hw | |
l = im[1] * hh | |
print("draw point", l, "x", r) | |
draw.ellipse((l, r, l + hw, r + hh), fill=c) | |
img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment