-
Hi. I was wondering if there's a way of getting the drawing as a PNG, in three or four channels (RGB or RGBA) and then storing the values as an np array. It would be useful for those of us that also use OpenCV in conjunction with python. Many thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
cduck
Mar 5, 2024
Replies: 1 comment 1 reply
-
Yes, you can use any Python library that can load PNG images like this: Here's a demo: import drawsvg as draw
from imageio.v3 import imread, imwrite
from io import BytesIO
# Helper function
def display_array(img_array):
'''Displays a numpy array as an image in Jupyter.'''
with BytesIO() as f:
imwrite(f, img_array, extension='.png')
f.seek(0)
return draw.Raster(f.read()) # Make your drawing
d = draw.Drawing(100, 100)
d.append(draw.Circle(50, 50, 40, fill='red'))
d # Convert your drawing into a numpy array
# The array shape is (height, width, 3) if opaque else (height, width, 4)
png_data = d.rasterize().png_data
img_array = imread(png_data, extension='.png')
display_array(img_array) # Manipulate your numpy array
img_array[::5, :] = (0, 255, 0, 255)
img_array[:, ::33] = (0, 255, 0, 255)
display_array(img_array) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
cduck
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, you can use any Python library that can load PNG images like this:
imread(d.rasterize().png_data)
Here's a demo: