I would like to generate a numpy array of 200x200 elements in size and put into it a circle centered into 100,100 coordinates, radius 80 and stroke width of 3 pixels. How to do
Cairo is a modern, flexible and fast 2D graphics library. It has Python bindings and allows creating "surfaces" based on NumPy arrays:
import numpy
import cairo
import math
data = numpy.zeros((200, 200, 4), dtype=numpy.uint8)
surface = cairo.ImageSurface.create_for_data(
data, cairo.FORMAT_ARGB32, 200, 200)
cr = cairo.Context(surface)
# fill with solid white
cr.set_source_rgb(1.0, 1.0, 1.0)
cr.paint()
# draw red circle
cr.arc(100, 100, 80, 0, 2*math.pi)
cr.set_line_width(3)
cr.set_source_rgb(1.0, 0.0, 0.0)
cr.stroke()
# write output
print data[38:48, 38:48, 0]
surface.write_to_png("circle.png")
This code prints
[[255 255 255 255 255 255 255 255 132 1]
[255 255 255 255 255 255 252 101 0 0]
[255 255 255 255 255 251 89 0 0 0]
[255 255 255 255 249 80 0 0 0 97]
[255 255 255 246 70 0 0 0 116 254]
[255 255 249 75 0 0 0 126 255 255]
[255 252 85 0 0 0 128 255 255 255]
[255 103 0 0 0 118 255 255 255 255]
[135 0 0 0 111 255 255 255 255 255]
[ 1 0 0 97 254 255 255 255 255 255]]
showing some random fragment of the circle. It also creates this PNG: