Take the 2-minute tour ×

Upon doing my homework, I stumbled across a problem concerning Python and image manipulation. I must say, using the Image lib is not an option. So here it is

from scipy.misc import imread,imsave
from numpy import zeros

imga = zeros([100,100,3])
h = len(imga)
w = len(imga[0])

for y in range(h):
    for x in range(w):
        imga[y,x] = [255,255,255]


I would assume it makes my picture white, but it turns it black, and I have no idea why It's not about the code (and I know it looks very ugly). Its just about the fact, that it is a black image.

share|improve this question
add comment

2 Answers

Every color in an image is represented by one byte. So to create an image array, you should set it's dtype to uint8.

And, you don't need for-loop to set every elements to 255, you can use fill() method or slice index:

import numpy as np
img = np.zeros([100,100,3],dtype=np.uint8)
img.fill(255) # or img[:] = 255
share|improve this answer
Ok since you mentioned that. How do I use slicing combined with conditions. For example if the y<50 and x<50 I want to set it to 255 otherwise to 0. –  Lefix May 6 '12 at 17:40
add comment

When creating imga, you need to set the unit type. Specifically, change the following line of code:

imga = zeros([100,100,3], dtype=np.uint8)

And, add the following to your imports:

import numpy as np

That gives a white image on my machine.

share|improve this answer
Thank you very much. Worked like charm –  Lefix May 5 '12 at 21:48
You're welcome. Since you're new here, please don't forget to mark the answer accepted which helped most in solving the problem. See also [meta.stackexchange.com/questions/5234/… –  Sam Cantrell May 5 '12 at 22:55
add comment

Your Answer


