Created
December 6, 2018 18:48
-
-
Save linnil1/82ab6f85a9c1f3c22060e8cf02dc44de to your computer and use it in GitHub Desktop.
A simple split image interactive python code.
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
import matplotlib.pyplot as plt | |
import os | |
from PIL import Image | |
folder_name = 'split/' | |
os.makedirs(folder_name, exist_ok=True) | |
images = sorted([f for f in os.listdir() if f.endswith('.JPG')]) | |
def resize(img, dirt): | |
ori_shape = img.size | |
diff = ori_shape[0] - ori_shape[1] | |
if diff > 0: | |
if dirt in [0, 2]: | |
img = img.crop([diff, 0, *ori_shape]) | |
if dirt in [1, 3]: | |
img = img.crop([0 0, ori_shape[0] - diff, ori_shape[1]]) | |
else: | |
diff = -diff | |
if dirt in [0, 1]: | |
img = img.crop([0, diff, *ori_shape]) | |
if dirt in [2, 3]: | |
img = img.crop([0, 0, ori_shape[0], ori_shape[1] - diff]) | |
return img.resize([1500, 1500]) | |
def onclick(event, name, img): | |
print(event.xdata, event.ydata) | |
mid_x = int(event.xdata) | |
mid_y = int(event.ydata) | |
fullname = folder_name + name + '{}.JPG' | |
resize(Image.fromarray(img[:mid_y, :mid_x]), 0).save(fullname.format(0)) | |
resize(Image.fromarray(img[:mid_y, mid_x:]), 1).save(fullname.format(1)) | |
resize(Image.fromarray(img[mid_y:, :mid_x]), 2).save(fullname.format(2)) | |
resize(Image.fromarray(img[mid_y:, mid_x:]), 3).save(fullname.format(3)) | |
plt.close() | |
for name in images: | |
fig, ax = plt.subplots(figsize=(12, 8), dpi=100) | |
img = plt.imread(name) | |
cid = fig.canvas.mpl_connect('button_press_event', | |
lambda event: onclick(event, name[:-4], img)) | |
plt.imshow(img) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment