I need to overplot general coordinate grids on images in python. I can compute the pixel coordinates of the grid lines, so I just need a module capable of drawing them as dashed lines on top of an image. The image comes in the form of a numpy array, so I need to be able to convert between those and the image format used by the drawing library. It also needs to be reasonably fast.
Here's what I've tried:
wand
Recently got support for drawing dashed lines, and is numpy-compatible:
with Drawing() as draw:
draw.stroke_antialias = False
draw.stroke_dash_array = [1,3]
draw.stroke_color = Color("gray")
draw.fill_opacity = 0
points = calc_grid_points()
draw.polyline(points)
with Image(width=width, height=height) as img:
draw(img)
return np.fromstring(img.make_blob("RGBA"),np.uint8).reshape(img.height, img.width, 4)
However, drawing a few hundred dashed lines over a 2000x1000 image with this library takes 30s! And pretty much all that time is spent in draw(img). So unless I'm doing something terribly wrong here, wand is just too slow.
PIL
Python Image Library in general works fine, but it does not seem to support dashed lines. I've not seen anybody state it directly, but a google search just yields 2-3 people asking about it and not getting any answers. Non-dashed coordinate grids don't look very nice, and cover up to much of the image being plotted.
GD
gd supports dashed lines, but I didn't find an efficient way of converting its images to and from numpy arrays.
Matplotlib
Matplotlib has proper support for drawing coordinate grids and axes, but sadly it seems to be impossible to avoid repixelization. It insists on building its new set of pixels that never have a 1-to-1 mapping with the original pixels. That's fine for smooth images, but not for images with large changes from pixel to pixel.
Alternatives?
So I wonder, are there any other image libraries out there that would achieve this? Or have I overlooked features of the ones above that make them usable?
Aucun commentaire:
Enregistrer un commentaire