使用python每页有多个png/pdf

2022-12-05 01:57:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组图片我已经保存为.png和.pdf。我想知道是否有任何方式组合在一起,无论是PNG或PDF在这样一种方式,我可以有多个每页。我知道如何使用PdfFileMerger()将多个PDF合并在一起,但这样每页只放一个项目。有没有办法做到这一点?我有两个PDF和PNG,所以无论哪一个我需要使用我可以

我基本上想做一些事情,比如:

拍摄image1.png, 图片2.png, image3.png和 image4.png并创建如下内容:

输出文件1:

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

我真的很感激


Tags: 项目内容pdfpng方式图片事情每页办法image1image3pdffilemergerimage4
1条回答
网友
1楼 · 发布于 2022-12-05 01:57:43

您可以使用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-循环中


没有Python的程序ImageMagick也是如此

但是您可以在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


Python模块Wand使用ImageMagick。代码类似于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)