How to create a white image in Python?

Question:

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]

imsave("Result.jpg",imga)

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.

Asked By: Lefix

||

Answers:

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.

Answered By: Sam Cantrell

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
Answered By: HYRY

Easy!
Check the below Code:

whiteFrame = 255 * np.ones((1000,1000,3), np.uint8)

255 is the color for filling the bytes.

1000, 1000 is the size of the image.

3 is the color channel for the image.

And unit8 is the type

Goodluck

Answered By: Etezadi
# Create an array with a required colours
# The colours are given in BGR [B, G, R]
# The array is created with values of ones, the size is (H, W, Channels)
# The format of the array is uint8
# This array needs to be converted to an image of type uint8

selectedColor = [75, 19, 77] * np.ones((640, 480, 3), np.uint8)
imgSelectedColor = np.uint8(np.absolute(selectedColor))
Answered By: user13151808

Just regarding the headline of this question, I did need a white image as well as a pillow input. And the solutions presented here did not work for me.

Therefore here a different way to generate white images for other purposes:

from PIL import Image
img = Image.new('RGB', (200, 50), color = (255,255,255))

Size and color may be changed in the 2nd and 3rd parameter of the Image.new()-function.

And if you want to write something on this image or save it, this would be example code for this.

from PIL import ImageFont, ImageDraw
fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 30)
ImageDraw.Draw(img).text((0,0), "hello world", font=fnt, fill=(0,0,0))
img.save('test.jpg')
Answered By: Thomas R

The headline is too broad and shows up at Google first. I needed a white image and used PIL and numpy. PILlow actually works well with numpy

import numpy as np
from PIL import Image
img = np.zeros([100,100,3],dtype=np.uint8)
img.fill(255) # numpy array!
im = Image.fromarray(img) #convert numpy array to image
im.save('whh.jpg')
Answered By: Omar Khan

Here’s a simple way to create a white image with a python one liner.

$ python3 -c "from PIL import Image;Image.new('RGB', (1900, 1080), color = (255,255,255)).save('Img.jpg')"

This will create a white image with a width of 1900 and hight of 1080.

Answered By: Sanaf
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.