PDF 파일을 일괄 이미지로 변경해야할 일이 가끔 있다..인쇄를 막아놨다거나…..쿨럭…
필요한 부분을 한장씩 스샷 찍어서 편집하기도 그렇고 해서….파이썬으로 간단하게 스크립트를 만들었음.
이미지로 변환 + crop! (상하좌우로 여백들이 생기더라구요…-_-)
from pdf2image import convert_from_path, convert_from_bytes
from PIL import Image
import os
import glob
def conv(fname, out_dir) :
images = convert_from_path(fname, fmt='jpg', output_folder=out_dir, poppler_path = r"Release-22.01.0-0\\poppler-22.01.0\\Library\\bin")
def image_crop( infilename , save_path):
img = Image.open( infilename )
(img_w, img_h) = img.size
print(img.size)
#bbox = (h*grid_h, w*grid_w, (h+1)*(grid_h), (w+1)*(grid_w))
s_x = 105
s_y = 105
e_x = img_w - 105
e_y = img_h - 105
bbox = ( s_x, s_y, e_x, e_y)
# 가로 세로 시작, 가로 세로 끝
# Returns a rectangular region from this image.
# The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate. See Coordinate System.
crop_img = img.crop(bbox)
fname = os.path.basename(infilename)
savename = os.path.join(save_path, fname)
crop_img.save(savename)
print('save file ' + savename + '....')
def conv_and_cut(fname) :
out_dir = os.path.basename(fname).split(".")[0]
os.makedirs(out_dir, exist_ok=True)
conv(fname, out_dir)
files = glob.glob(out_dir + "/*.jpg")
resize_dir = out_dir+"_resize"
os.makedirs(resize_dir, exist_ok=True)
for x in files :
image_crop(x, resize_dir)
#break
if __name__ == "__main__" :
conv_and_cut("3.pdf")
conv_and_cut("4.pdf")
conv_and_cut("5.pdf")
conv_and_cut("6.pdf")
필요하신 분은 참고해서 사용하시길 바랍니다.
필요 라이브러리
https://pypi.org/project/pdf2image/
https://github.com/oschwartz10612/poppler-windows/releases/