Skip to content

Instantly share code, notes, and snippets.

@recuraki
Created June 22, 2021 15:39
Show Gist options
  • Save recuraki/c5c0b44ec7caec6efa2d6e802c8131aa to your computer and use it in GitHub Desktop.
Save recuraki/c5c0b44ec7caec6efa2d6e802c8131aa to your computer and use it in GitHub Desktop.
アルパカ塗り
from PIL import Image, ImageDraw, ImageChops, ImageEnhance
img = Image.open("animal_arupaka.png")
dat = img.split()
r,b,g,a = img.split()
basecolor = (212, 195, 188) #毛の色
cangap = 18
# 毛を推察する。+-gapは許容
r = r.point(lambda x: 1 if basecolor[0] - cangap <= x <= basecolor[0] + cangap else 0, mode="1")
g = g.point(lambda x: 1 if basecolor[1] - cangap <= x <= basecolor[1] + cangap else 0, mode="1")
b = b.point(lambda x: 1 if basecolor[2] - cangap <= x <= basecolor[2] + cangap else 0, mode="1")
ph = 0
dankai = 255
for i in range(255):
# アルパカの毛皮
mask = ImageChops.logical_and(r, g)
mask = ImageChops.logical_and(mask, b)
d = ImageDraw.Draw(mask) # 毛皮塗る
newh = int(mask.height / dankai * i) # 新しく塗る高さ
haba = mask.height // dankai + 3 # 塗る幅
d.rectangle([(0,0), (mask.width, newh)], fill="black") #その区間より上を黒でマスク
d.rectangle([(0, newh+ haba), (mask.width, mask.height)], fill="black") # 同じく下をマスク
# ペンキのように塗る
dst_color = (255-255//dankai*i,255//dankai*i,255-255//dankai*i)
img.paste(Image.new("RGB", img.size, dst_color), mask=mask)
ph = newh
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment