使用python

时间:2019-11-26 00:18:07

标签: python pdf png

我有一组保存为.png和.pdf的图像。我想知道是否有某种方式可以将png或pdf组合在一起,这样我每页可以有多个。我知道如何使用PdfFileMerger()将几个pdf合并在一起,但是每页只放置一项。有什么办法可以做到这一点?我同时拥有pdf和png,因此无论我需要使用哪一个都可以。

我基本上想做类似的事情:

拍摄image1.png, image2.png, image3.png,以及 image4.png并创建如下内容:

outputfile1:

-----------------------------
              |
              |
image1.png    | image2.png
              |
              |
-----------------------------
              |
              |
image3.png    | image4.png
              |
              |
-----------------------------

我真的很感激!

1 个答案:

答案 0 :(得分:2)

您可以使用PIL / pillow创建大的空白图像,然后将小图像放置在此图像的不同位置。

from PIL import Image

# get images    
img1 = Image.open('image1.png')
img2 = Image.open('image2.png')
img3 = Image.open('image3.png')
img4 = Image.open('image4.png')

# get width and height
w1, h1 = img1.size
w2, h2 = img2.size
w3, h3 = img3.size
w4, h4 = img4.size

# to calculate size of new image 
w = max(w1, w2, w3, w4)
h = max(h1, h2, h3, h4)

# create big empty image with place for images
new_image = Image.new('RGB', (w*2, h*2))

# put images on new_image
new_image.paste(img1, (0, 0))
new_image.paste(img2, (w, 0))
new_image.paste(img3, (0, h))
new_image.paste(img4, (w, h))

# save it
new_image.save('new.png')

顺便说一句:您可以将其写在for循环中。


与程序ImageMagick(不带Python)相同。

但是您可以在Python中将这些命令与os.system('convert ...')

一起使用
$ convert image1.png image2.png +append row1.png
$ convert image3.png image4.png +append row2.png
$ convert row1.png row2.png -append new.png

+append加入行,-append加入列。

即使在一个命令中,也可以执行此操作:Stitching Image Set Together

$ convert image1.png image2.png image3.png image4.png +append -crop 2x1@ -append new.png

如果您使用new.pdf而不是new.png,那么它可以创建PDF


有一个使用ImageMagick的Python模块Wand。代码类似于pillow

from wand.image import Image

img1 = Image(filename='image1.png')
img2 = Image(filename='image2.png')
img3 = Image(filename='image3.png')
img4 = Image(filename='image4.png')

w1, h1 = img1.size
w2, h2 = img2.size
w3, h3 = img3.size
w4, h4 = img4.size

w = max(w1, w2, w3, w4)
h = max(h1, h2, h3, h4)

new_image = Image(width=w*2, height=h*2)

new_image.composite(image=img1, left=0, top=0)
new_image.composite(image=img2, left=w, top=0)
new_image.composite(image=img3, left=0, top=h)
new_image.composite(image=img4, left=w, top=h)

new_image.save(filename='new.png')

使用cv2numpy的示例,但是在图像尺寸相同时可以使用。如果它们不同,则它们需要空的行和列以具有相同的大小。

import cv2
import numpy as np

img1 = cv2.imread('image1.png')
img2 = cv2.imread('image2.png')
img3 = cv2.imread('image3.png')
img4 = cv2.imread('image4.png')

row1 = np.concatenate((img1, img2), axis=1)
row2 = np.concatenate((img3, img4), axis=1)
new_image = np.concatenate((row1, row2))

# or
row1 = np.hstack((img1, img2))
row2 = np.hstack((img3, img4))
new_image = np.vstack((row1, row2))

cv2.imwrite('new.png', new_image)

类似于matplotlibnumpy

import matplotlib.image
import numpy as np

img1 = matplotlib.image.imread('image1.png')
img2 = matplotlib.image.imread('image2.png')
img3 = matplotlib.image.imread('image3.png')
img4 = matplotlib.image.imread('image4.png')

row1 = np.concatenate((img1, img2), axis=1)
row2 = np.concatenate((img3, img4), axis=1)
new_image = np.concatenate((row1, row2))

# or
row1 = np.hstack((img1, img2))
row2 = np.hstack((img3, img4))
new_image = np.vstack((row1, row2))

matplotlib.image.imsave('new.png', new_image)

类似于imageionumpy

import imageio
import numpy as np

img1 = imageio.imread('image1.png')
img2 = imageio.imread('image2.png')
img3 = imageio.imread('image3.png')
img4 = imageio.imread('image4.png')

row1 = np.concatenate((img1, img2), axis=1)
row2 = np.concatenate((img3, img4), axis=1)
new_image = np.concatenate((row1, row2))

# or
row1 = np.hstack((img1, img2))
row2 = np.hstack((img3, img4))
new_image = np.vstack((row1, row2))

imageio.imwrite('new.png', new_image)