Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.ones results in red, green, blue, and black bars on image

I have the following code:

from PIL import Image
import numpy as np

a = np.ones((512, 256, 3)).astype(int)*255
image = Image.fromarray(a, "RGB")
image.save("test.png", "PNG")

I would expect this to result in a white image of size 512x256. Instead, I get this:

Columns of color image

For some reason, this image is a series of bars rather than a solid color.

What am I doing wrong? Is it the format of the numpy array?

like image 297
Noah Crowley Avatar asked Jan 22 '26 03:01

Noah Crowley


1 Answers

Instead of astype(int) use astype(np.uint8) or even better

a = np.full((512, 256, 3), 255, dtype=np.uint8)

The stripes you are seeing are the result of int values reinterpreted as sequences of uint8s.

like image 165
Paul Panzer Avatar answered Jan 23 '26 21:01

Paul Panzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!