PDF 有多页,一次性转成JPG 图片,JAVA 报内存溢出,
现改为,每一页存成一张图片,然后再将多张图片合成一张图片。
安装库
pip3 install Image -i https://pypi.tuna.tsinghua.edu.cn/simple
cv2.imread()不支持中文路径, 如果有中文读出来会是 None
# coding=utf-8
from PIL import Image
import cv2
import numpy as np
def merge(img_array, direction="horizontal", gap=0):
img_array = [Image.fromarray(img) for img in img_array]
w, h = img_array[0].size
if direction == "horizontal":
result = Image.new(img_array[0].mode, ((w + gap) * len(img_array) - gap, h))
for i, img in enumerate(img_array):
result.paste(img, box=((w + gap) * i, 0))
elif direction == "vertical":
result = Image.new(img_array[0].mode, (w, (h + gap) * len(img_array) - gap))
for i, img in enumerate(img_array):
result.paste(img, box=(0, (h + gap) * i))
else:
raise ValueError("The direction parameter has only two options: horizontal and vertical")
return np.array(result)
if __name__ == '__main__':
img_list = []
for i in range(0, 10):
img = cv2.imread("D:\\Users\\Pictures\\4\\%s.PNG" % (i + 1))
img_list.append(img)
img = merge(img_list, "vertical")
cv2.imwrite("MergeImage.png", img)
源图
合并后的图