python - Cairo 剪辑会产生不需要的边缘

标签 python cairo pycairo

我正在使用 pyCairo 在表面上绘制移动元素。 为了获得更好的性能,我尝试使用“剪辑”功能来重绘更大图像的更改部分。不幸的是,它会在图像上产生不需要的边缘。可以看到剪辑的边缘。是否可以避免这种行为?

enter image description here

import math
import cairo


def draw_stuff(ctx):
    """ clears background with solid black and then draws a circle"""
    ctx.set_source_rgb (0, 0, 0) # Solid color
    ctx.paint()

    ctx.arc (0.5, 0.5, 0.5, 0, 2*math.pi)
    ctx.set_source_rgb (0, 123, 0)
    ctx.fill()

WIDTH, HEIGHT = 256, 256

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)

ctx.scale (WIDTH, HEIGHT) # Normalizing the canvas

draw_stuff(ctx)

#Let's draw stuff again, this time only redrawing a small part of the image
ctx.save()
ctx.rectangle(0.2,0.2,0.2,0.2)
ctx.clip()
draw_stuff(ctx)
ctx.restore()

surface.write_to_png ("example.png") # Output to PNG

最佳答案

您应该将剪辑坐标舍入为整数(在设备空间中)。请参阅http://cairographics.org/FAQ/#clipping_performance

我不知道 Python API,我只是猜测它如何像 C API 一样工作,但它是这样的:

def snap_to_pixels(ctx, x, y):
    x, y = ctx.user_to_device(x, y)
    # No idea how to round an integer in python,
    # this would be round() in C
    # (Oh and perhaps you don't want this rounding, but
    # instead want to round the top-left corner of your
    # rectangle towards negative infinity and the bottom-right
    # corner towards positive infinity. That way the rectangle
    # would never become smaller to the rounding. But hopefully
    # this example is enough to get the idea.
    x = int(x + 0.5)
    y = int(x + 0.5)
    return ctx.device_to_user(x, y)

# Calculate the top-left and bottom-right corners of our rectangle
x1, y1 = 0.2, 0.2
x2, y2 = x1 + 0.2, y1 + 0.2
x1, y1 = snap_to_pixels(ctx, x1, y1)
x2, y2 = snap_to_pixels(ctx, x2, y2)
# Clip for this rectangle
ctx.rectangle(x1, y1, x2 - x1, y2 - y1)
ctx.clip()

关于python - Cairo 剪辑会产生不需要的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24633763/

相关文章:

python - 使用 Python 进行网络摄像头运动跟踪

c++ - 开罗图书馆和 Cmake

c - 绘制到 gtk 窗口

c++ - Gtk+ : How to set the cursor of a window from a Cairo context?

python - 添加包含 `cairo-gobject.pc' 的目录

python - 通过 SQLAlchemy 获取随机行

python - Opencv python 不正确的 block 大小

python - 移植到 python-gi 时未知的 Cairo 上下文字段

python - 在 Windows 上安装 PyCairo

Python 相当于 Perl $+ aka $LAST_PAREN_MATCH