Last active
December 25, 2021 05:24
-
-
Save alongthecloud/92aa4660ce106451721aaa47d272105d to your computer and use it in GitHub Desktop.
code for divides a picture into pieces and shuffle it (그림을 조각내어 섞는 코드)
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment