그림을 조각내어서 퍼즐처럼 섞어주는 프로그램, pillow 라이브러리가 필요하다.
puzzled_image.py sample.jpg 4 4 2
처럼 사용하면 된다.
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
# puzzled_image.py | |
# pip install pillow | |
import os | |
import sys | |
import random | |
from PIL import Image | |
from PIL import ImageOps | |
def puzzled_image(filepath, count_x, count_y,shuffle_count, invert=False): | |
filename = os.path.splitext(filepath)[0] | |
try: | |
srcim = Image.open(filepath) | |
cropX = int(srcim.size[0]/count_x) | |
cropY = int(srcim.size[1]/count_y) | |
srcBoxs = [] | |
dstBoxs = [] | |
for i in range(0, count_y): | |
for j in range(0, count_x): | |
x = j * cropX | |
y = i * cropY | |
bb = (x, y, x+cropX , y+cropY) | |
srcBoxs.append(bb) | |
dstBoxs.append(bb) | |
for i in range(shuffle_count): | |
random.shuffle(srcBoxs) | |
dstim = Image.new('RGB', srcim.size) | |
count = len(srcBoxs) | |
for i in range(0, count): | |
p = srcim.crop(srcBoxs[i]) | |
if invert: | |
if i%2 == 0: | |
p = ImageOps.invert(p) | |
dstim.paste(p, dstBoxs[i]) | |
# print(srcBoxs[i]) | |
dstim.save(filename + "_puzzled.png", "PNG") | |
except e: | |
print(e) | |
return | |
if __name__ == '__main__': | |
argc = len(sys.argv) | |
if argc != 5: | |
print("Usage:\tpuzzled_image.py <image_file_path x y shuffle_count>\n\tx,y is number of pieces.") | |
else: | |
countX = int(sys.argv[2]) | |
countY = int(sys.argv[3]) | |
shuffle_count = int(sys.argv[4]) | |
puzzled_image(sys.argv[1], countX, countY, shuffle_count, False) |