2

我有一些我想下载的 PNG 图像链接,“转换为缩略图”并使用 Python 和 Cairo 保存为 PDF。

现在,我有一个工作代码,但我不知道如何控制纸上的图像大小。有没有办法将 PyCairo Surface 的大小调整为我想要的尺寸(恰好比原来的小)?我希望将原始像素“缩小”到更高分辨率(在纸上)。

另外,我尝试Image.rescale()了 PIL 的函数,但它给了我一个 20x20 像素的输出(在 200x200 像素的原始图像中,这不是代码上的横幅示例)。我想要的是一个 200x200 像素的图像,绘制在纸上 20x20 平方毫米的正方形内(而不是我现在得到的 200x200 平方毫米)

我目前的代码是:

#!/usr/bin/python

import cairo, urllib, StringIO, Image   # could I do it without Image module?

paper_width = 210
paper_height = 297
margin = 20

point_to_milimeter = 72/25.4

pdfname = "out.pdf" 
pdf = cairo.PDFSurface(pdfname , paper_width*point_to_milimeter, paper_height*point_to_milimeter)
cr = cairo.Context(pdf)
cr.scale(point_to_milimeter, point_to_milimeter)

f=urllib.urlopen("http://cairographics.org/cairo-banner.png")
i=StringIO.StringIO(f.read())
im=Image.open(i)

# are these StringIO operations really necessary?

imagebuffer = StringIO.StringIO()  
im.save(imagebuffer, format="PNG")
imagebuffer.seek(0)
imagesurface = cairo.ImageSurface.create_from_png(imagebuffer)


### EDIT: best answer from Jeremy, and an alternate answer from mine:
best_answer = True      # put false to use my own alternate answer
if best_answer:
    cr.save()
    cr.scale(0.5, 0.5)
    cr.set_source_surface(imagesurface, margin, margin)
    cr.paint()
    cr.restore()
else:
    cr.set_source_surface(imagesurface, margin, margin)
    pattern = cr.get_source()
    scalematrix = cairo.Matrix()    # this can also be used to shear, rotate, etc.
    scalematrix.scale(2,2)          # matrix numbers seem to be the opposite - the greater the number, the smaller the source
    scalematrix.translate(-margin,-margin)  # this is necessary, don't ask me why - negative values!!
    pattern.set_matrix(scalematrix)  
    cr.paint()  

pdf.show_page()

请注意,漂亮的开罗横幅甚至不适合页面...理想的结果是我可以以用户空间单位(在本例中为毫米)控制此图像的宽度和高度,以创建漂亮的标题图像,例如。

感谢您的阅读和任何帮助或评论!!

4

3 回答 3

2

绘制图像时尝试缩放上下文。

例如

cr.save()    # push a new context onto the stack
cr.scale(0.5, 0.5)    # scale the context by (x, y)
cr.set_source_surface(imagesurface, margin, margin)
cr.paint()
cr.restore()    # pop the context

有关详细信息,请参阅:http ://cairographics.org/documentation/pycairo/2/reference/context.html。

于 2011-08-18T04:20:00.170 回答
1

这不是在回答问题,我只是想分享 heltonbiker 的当前代码,该代码已编辑为使用 Python 3.2 运行:

import cairo, urllib.request, io
from PIL import Image

paper_width = 210
paper_height = 297
margin = 20

point_to_millimeter = 72/25.4

pdfname = "out.pdf" 
pdf = cairo.PDFSurface( pdfname, 
                        paper_width*point_to_millimeter, 
                        paper_height*point_to_millimeter
                        )

cr = cairo.Context(pdf)
cr.scale(point_to_millimeter, point_to_millimeter)

# load image
f = urllib.request.urlopen("http://cairographics.org/cairo-banner.png")
i = io.BytesIO(f.read())
im = Image.open(i)
imagebuffer = io.BytesIO()  
im.save(imagebuffer, format="PNG")
imagebuffer.seek(0)
imagesurface = cairo.ImageSurface.create_from_png(imagebuffer)

cr.save()
cr.scale(0.5, 0.5)
cr.set_source_surface(imagesurface, margin, margin)
cr.paint()
cr.restore()

pdf.show_page()
于 2013-05-29T09:34:11.753 回答
0

Jeremy Flores 通过在将图像表面设置为源之前缩放目标表面很好地解决了我的问题。即使,也许有一天您实际上需要调整 Surface 的大小(或以任何方式对其进行转换),所以我将简要描述在我的替代答案中使用的基本原理(已包含在问题中),在彻底阅读文档后推断:

  1. 将您的表面设置为上下文的源 - 它隐式创建一个cairo.Pattern!!
  2. 用于Context.get_source()恢复模式;
  3. 创建一个cairo.Matrix
  4. 将此矩阵(及其所有变换)应用于模式;
  5. 画!

唯一的问题似乎是始终围绕原点进行的转换,因此必须先进行缩放和旋转,然后再进行到原点的互补平移(bleargh)。

于 2011-08-18T04:58:27.997 回答