2014-07-08 1 views
2

표면에 움직이는 요소를 그리기 위해 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 
+0

당신의 스크린 샷을 제공 할 수 원치 않는 가장자리? – cpburnz

+0

StackOverflow를 사용하려면 이미지를 게시하기 전에 10 개의 평판이 있어야합니다. 위의 코드는 검정색 배경에 큰 녹색 원을 출력합니다. "클립"이후의 그림이 나타나는 지점에서 검은 색 가장자리가 좁은 원치 않는 직사각형을 만듭니다. – zidik

답변

2

당신은 당신의 cliping 반올림한다는 (디바이스 공간에서) 정수로 조정합니다. http://cairographics.org/FAQ/#clipping_performance

내가 파이썬 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() 
+0

반올림 정말 트릭을 했어 - 감사합니다! – zidik