LoginSignup
2
2

More than 5 years have passed since last update.

Python Image Libraryで水玉模様の壁紙作成

Posted at

まんまです。PythonとPIL(Python Image Library)を使って水玉模様の壁紙を作ります
あらかじめPILとPythonのインストールが必要です。
インストールは下あたりを参考に。

使い方

引数を二つとります。二つの引数の形式はどちらも同じで、HLS形式の色指定を:で区切ったものです。CSS3でサポートしているhsl表記と引数の順序が異なるので注意
360:100:100
最初の:の前の数値はHue(色相)です。0~360の値をとります。
次の数値はLuminance(明度)です。0~100の値をとります。
最後の数値はSaturation(彩度)です。0~100の値をとります。
この値を二つ指定することで、水玉の丸の色を指定します。
wallpaper.py 84:84:84 164:98:84

なお、Python Image Libraryでは画像の縮小時しかアンチエイリアス処理が適用できないことから、画像のサイズは100*100固定で出力し、お使いのイメージビューアで表示します。
IrfanViewなどで表示して縮小後、お好みの形式で保存してください(IrfanViewのほうが画像の縮小アルゴリズムが豊富かつ強力だったので、このようにしました)。

ソースコード

なお、RGB→PILで使用できるカラーコード形式に変換するコードは、以下サイトのものを使用しています。

wallpaper.py
# -*- 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()
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2