Home > Software design >  Draw a bitmap with python from a numpy matrix
Draw a bitmap with python from a numpy matrix

Time:10-03

I tried to draw a simple 10x10 black image with a single red pixel at the 8,8 coordinate.

This was my attempt:

from PIL import Image
import numpy as np

foo = np.zeros([10,10,3])
foo[8,8] = [255,0,0] # Draw a red pixel
img = Image.fromarray(foo, 'RGB')
img.save('out.png')

Sadly for some reason the entire image stays black (enlarged):

enter image description here

When printing "foo" the [255,0,0] entry is there, so I am confused on what is wrong here.

CodePudding user response:

You need to specify the data type of the numpy array to int8.

So you would have

foo = np.zeros([10,10,3], dtype=np.int8)
  • Related