python - 从一个表面绘制到另一个表面

标签 python cairo

import cairo
import math

w = 2000
h = 2000

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx = cairo.Context(surface)
ctx.scale(w, h)

surface_path = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx_path = cairo.Context(surface_path)
ctx_path.scale(w, h)

surface_circle = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx_circle = cairo.Context(surface_circle)
ctx_circle.scale(w, h)

""" Lots of function calls that draw paths to surface_path and circles to surface_circle """

ctx.set_source_surface(surface_path, 0, 0)
ctx.paint()
ctx.set_source_surface(surface_circle, 0, 0)
ctx.paint()

surface_path.write_to_png("example.png")
surface_circle.write_to_png("example2.png")
surface.write_to_png("result.png")

Imgur link to saved images

我正在尝试将两个表面(一个带线,一个带圆)编译到一个单独的表面上,然后将其保存到一个文件中。

尽管遵循了文档建议的内容,但最终图像最终还是空白。我还尝试在 surface_pathsurface_circle 上调用 flush(),但这似乎什么也没做。

如何将 surface_circle(example2.png)中的图像信息合并到 surface_path(example.png)之上,然后将其输出到文件?

最佳答案

尝试在最后几次调用 paint() 之前调用 ctx.identity_matrix()。

就像现在一样,由于您调用了 ctx.scale(w, h),您只会得到那些其他表面的左上角像素,这些像素会按比例放大以填充所有目标表面。

关于python - 从一个表面绘制到另一个表面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21645781/

相关文章:

python - pandas_datareader.data 不返回从开始日期到结束日期的所有股票值

python - 如何在 Ubuntu 10.04 上为 python 3 安装 pycairo

graphics - 开罗文本旋转

python - 循环邻域 - 最小 Numpy

python - 查找整数列表中是否存在谷

python - "try: input() except KeyboardInterrupt:"有解决方法吗

开罗库的 C# 包装器

python - Laravel 应用程序使用 Symfony 在 LEMP 堆栈上执行 Python 脚本 - 权限被拒绝

c - 我应该怎么做才能在开罗加载图像?

opengl-es-2.0 - 用于在 1.12.14 中测试 cairo-gles 后端的 cairo 应用程序示例