使用python将一系列图像放入一个A4页面 [英] put series of images into one A4 pages using python

查看:581
本文介绍了使用python将一系列图像放入一个A4页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列图形(.png文件),我想将其中四个放入A4页面,并在其余图形中继续这样做.使用Python可以做到吗?

I have a series of graphs (.png files) and I want to put four of them into an A4 page and continue doing this for the rest of my graphs. Is it possible to do it with Python?

推荐答案

如果您只是想问如何将四个图像平铺为一个较大的图像,那么使用大多数图像处理库就很容易做到这一点.

If you're just asking how to tile four images together into a larger image, this is easy to do with most image-processing libraries.

我将展示如何使用PIL/枕头:

I'll show how to do it using PIL/Pillow:

import sys
from PIL import Image

width, height = int(8.27 * 300), int(11.7 * 300) # A4 at 300dpi

images = sys.argv[1:]
groups = [images[i:i+4] for i in range(0, len(images), 4)]
for i, group in enumerate(groups):
    page = Image.new('RGB', (width, height), 'white')
    page.paste(Image.open(group[0]), box=(0, 0))
    page.paste(Image.open(group[1]), box=(int(width/2.+.5), 0))
    page.paste(Image.open(group[2]), box=(0, int(height/2.+.5)))
    page.paste(Image.open(group[3]), box=(int(width/2.+.5), int(height/2.+.5)))
    page.save('page{}.pdf'.format(i))

这仅是示例代码,而不是您的问题的完整解决方案.一些警告:

This is meant as sample code, not a complete solution to your problem. A few caveats:

  • 这会为每个页面生成一个单独的PDF文件.
  • 我将PNG文件放置在页面每个象限的左上角.我不知道您想把它们放在哪里,但这可能不完全是. (许多打印机不能一直打印到页边距,即使可以,也通常不希望它们打印.)
  • 我没有缩放图像,如果您的图形是100x100或2000x2000,这可能是个问题.
  • 我假设您想以300dpi打印.

这篇关于使用python将一系列图像放入一个A4页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆