Last active
May 20, 2020 13:30
-
-
Save choco0908/9e29786aa276f61329c0ce056e65d676 to your computer and use it in GitHub Desktop.
파이썬 코드로 웹툰형 이미지 만들기 ( Combining images to vertical using python )
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 glob | |
import os | |
from PIL import Image | |
def combineImage(full_width,full_height,image_key,image_list,index): | |
canvas = Image.new('RGB', (full_width, full_height), 'white') | |
output_height = 0 | |
for im in image_list: | |
width, height = im.size | |
canvas.paste(im, (0, output_height)) | |
output_height += height | |
print('Start Merge image to out//'+image_key+'_merged_'+str(index)+'.jpg') | |
canvas.save('out//'+image_key+'_merged_'+str(index)+'.jpg') | |
def listImage(image_key,image_value): | |
full_width, full_height,index = 0, 0, 1 | |
image_list = [] | |
for i in image_value: | |
im = Image.open(image_key+"_"+str(i)+".jpg") | |
print('Get '+image_key+"_"+str(i)+".jpg") | |
width, height = im.size | |
if full_height+height > 65000: | |
combineImage(full_width,full_height,image_key,image_list,index) | |
index = index + 1 | |
image_list = [] | |
full_width, full_height = 0, 0 | |
image_list.append(im) | |
full_width = max(full_width, width) | |
full_height += height | |
combineImage(full_width,full_height,image_key,image_list,index) | |
if __name__ == '__main__' : | |
print('[+] Start Combining Images') | |
target_dir = "./" | |
files = glob.glob(target_dir + "*.jpg") | |
name_list = {} | |
# Make Directory | |
try: | |
if not(os.path.isdir('out')): | |
os.makedirs(os.path.join('out')) | |
except OSError as e: | |
if e.errno != errno.EEXIST: | |
print("Failed to create directory!!!!!") | |
raise | |
for f in files: | |
name = f.split('\\')[1] | |
key = name.split('_')[0] | |
value = name.split('_')[1].split('.')[0] | |
if key in name_list.keys(): | |
name_list[key].append(int(value)) | |
else: | |
name_list[key] = [int(value)] | |
name_list[key].sort() | |
for key,value in name_list.items(): | |
listImage(key,value) | |
print('[+] Complete Combining Images') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment