728x90
만든 이유 ?
- 프로젝트에 필요한 자료를 일정한 크기로 잘라야하기에 자동으로 이미지를 자를 수 있는 Python 코드를 사용해봄
장점은 ?
- 여러 개의 문서를 같은 크기로 자르는 데 for문을 사용하면 되기에 캡처 도구 없이 빠르게 이미지를 자를 수 있음
단점은 ?
- 같은 크기로 자르지 않을 경우 추가 작업이 필요할 수도 있음(코드 수정 등)
pip install pillow
pip install PIL이 아닌 cmd 관리자 모드로 열어서 위 코드를 써야합니다.
from PIL import Image
import os
class img_devide():
def __init__(self, img_path, i):
self.img_path = img_path
self.path = ''
self.lengh = 0
self.width = 0
self.height = 0
self.img = ''
self.i = i
def empty_img(self):
area = (50, 120, 740, 990) # 시작 x좌표, 시작 y좌표, 끝 x좌표, 끝 y좌표
cropped_img = self.img.crop(area) # 파일을 area 영역만큼 crop(이미지를 자름)
cropped_img.save(str(self.img_path) + '/full/'+str(self.i)+'.jpg')
# 자른 파일을 원하는 위치에 원하는 이름으로 저장
def set_image(self):
self.img = Image.open(self.img_path + str(self.i) + ".jpg") # 파일의 경로 및 이름
self.width = self.img.size[0]
self.height = self.img.size[1]
self.lengh = int(self.width / self.height)
for i in range(1,145):
a = img_devide("C:/Users/Ai/Desktop/cut/", i) # 파일의 경로
a.set_image()
a.empty_img()
같은 144개의 이미지를 같은 크기로 자르는 코드
728x90