LoginSignup
12
7

More than 5 years have passed since last update.

画像を格子状にカットして画像回転してから並べ直す

Last updated at Posted at 2017-10-16

思いつきで作った。深い意味は無い。

original.jpg

from PIL import Image
import random

#縦の枚数
xb = 20
#横の枚数
yb = 20

#1枚あたりの横サイズ(px)
bw = 30
#1枚あたりの縦サイズ(px)
bh = 30

#余白
pd = 3

#回転の角度を設定(自動でマイナス分も設定)
dgree = 20

#画像を開く
pic = Image.open('original.jpg')
#アルファチャネルモードに変換
pic = pic.convert('RGBA')

#画像の縦横サイズを取得
[gwidth,gheight] = pic.size

#完成画像用のキャンバスを適切な大きさで用意する
output = Image.new('RGBA', (gwidth+((xb+1)*pd), gheight+((yb+1)*pd)), (255, 255, 255))

#1枚ずつ切り出して処理
for y in range(yb):
    for x in range(xb):
        p = pic.crop((x*bw,y*bh,x*bw+bw,y*bh+bh))
        d = random.randint(dgree*-1,dgree)
        p = p.rotate(d,Image.BICUBIC,expand = 1)
        output.paste(p,(x*bw+(x*pd),y*bh+(y*pd)),p)

#保存する
output.save('output.png')

output.png

プロパティをイジって何度か生成していると絵の表情が変わって面白い

output.png

12
7
2

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
12
7