Pycairo Scale Distortion
I am trying to draw a circle on a png rectangular background. Whenever I try to draw a circle, it stretches to the scale of my image - making it an ellipse instead of a circle.
Here's the setup:
#set background image
surface = cairo.ImageSurface.create_from_png (image)
context = cairo.Context (surface)
WIDTH = cairo.ImageSurface.get_width(surface)
HEIGHT = cairo.ImageSurface.get_height(surface)
context.scale (WIDTH, HEIGHT)
And here is the circular drawing function (x coordinate, y coordinate, radius, color value) and functions used for positioning:
def draw_node(cx,cy,r, rgb):
context.arc(x_scale(cx), y_scale(cy), r, 0, 2 * math.pi)
context.set_source_rgb(rgb[0]/255.0,rgb[1]/255.0,rgb[2]/255.0)
context.fill()
context.stroke()
def x_scale (x):
width = 1000.0
return (x + .5) / width
def y_scale (y):
height = 1000.0 * HEIGHT / WIDTH
return (y + .5) / height
I would be especially grateful for any advice on how to solve the system scaling issue, but I would also like some advice on how to draw a circle within my existing setup.
Many thanks.
source to share
there is probably no way to avoid context.arc
drawing the ellipse when your scaling is not symmetrical. with your function, x_scale (x)
you only scale the center of the circle where it should be.
As a workaround, I suggest scaling both directions by the same amount; sacrifice, therefore x_max
, y_max
will be equal 1
.
i would change your code:
context.scale(HEIGHT, HEIGHT)
print('surface = 1 x {:1.2f}'.format(WIDTH/HEIGHT))
def draw_node(cx,cy,r, rgb):
context.arc(cx, cy, r, 0, 2 * math.pi)
context.set_source_rgb(rgb[0]/255.0,rgb[1]/255.0,rgb[2]/255.0)
context.fill()
context.stroke()
and do not scale (or scale 1000
if that's what you prefer).
source to share