Skip to content

Instantly share code, notes, and snippets.

@zaftzaft
Last active April 22, 2018 05:30
Show Gist options
  • Save zaftzaft/3ed1a40c26224258801511b622dd6b9c to your computer and use it in GitHub Desktop.
Save zaftzaft/3ed1a40c26224258801511b622dd6b9c to your computer and use it in GitHub Desktop.
画像を分割するやつ
import sys
from PIL import Image
im = Image.open(sys.argv[1])
(w, h) = im.size
im.crop((0, 0, w/2, h/2)).save("f/a.png")
im.crop((w/2, 0, w, h/2)).save("f/b.png")
im.crop((0, h/2, w/2, h)).save("f/c.png")
im.crop((w/2, h/2, w, h)).save("f/d.png")
import sys
from PIL import Image
im = Image.open(sys.argv[1])
(w, h) = im.size
wn = 3
hn = 3
p = 10
canvas = Image.new("RGB", (w+(p*(wn-1)), h+(p*(hn-1))), (0,0,0))
for y in range(0, hn):
for x in range(0, wn):
x1 = x * (w/wn)
x2 = (x+1) * (w/wn)
y1 = y * (h/hn)
y2 = (y+1) * (h/hn)
fragm = im.crop((x1, y1, x2, y2))
fragm.save("f/{}-{}.png".format(x, y))
canvas.paste(fragm, (int(x1+(p*x)), int(y1+(p*y))))
canvas.save("f/t.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment