How do I generate an image in Python?

Question:

I can load an image and display it like this:

import cv2
img = cv2.imread("1.jpg")
cv2.imshow("Original", img)

For example, to get a white image, I can load any one, draw a white rectangle over it and display it.

However, what if I want to get a white image without first loading it? How to replace the load operation with the image object generation operation?

Answer:

Using the Computer Vision Library to generate images is … weird. Better use Pillow

from PIL import Image
img = Image.new('RGB', (250, 250), (255, 255, 255))
img.show()
Scroll to Top