옆으로 긴 이미지를 정사각형으로 자르는 파이썬 python 코드
카테고리 없음
2021. 1. 13. 10:36
해당 경로와 하위 경로에 있는 모든 이미지를 정사각형으로 자릅니다.
가로로 긴 경우만 작동하지만 조금만 손보면 세로로 긴 이미지도 OK!
개인적으로 메모하기 위해 적었지만 고칠 내용이 있으면 언제든 말해주세요 :)
import os
from PIL import Image
for root, dirs, files in os.walk('./'):
print("탐색중인 경로: ", dirs)
for idx, file in enumerate(files):
fname, ext = os.path.splitext(file)
if ext in ['.jpg','.png','.gif','.heic','.webp']:
full_dir = root + "/" + file
# print("files", files)
# print("root", root)
# print("dirs", dirs)
# print("full_dir", full_dir)
im = Image.open(full_dir)
width, height = im.size
print("Width: ",width,"px, Height: ",height,"px -> ",file)
if width != height : #일단 가로로 긴 경우만 생각하자
crop_image = im.crop(((width/2)-(height/2),0,(width/2)+(height/2),height))
crop_image.save(os.path.basename(file))
else:
print("정사각형")
print((width/2)-(height/2),0,(width/2)+(height/2),height)
#n = input('입력 : ')