I have a code:
import cv2 import numpy background = numpy.zeros((1080, 1920, 3)) img = numpy.ones((255, 465, 3)) offset = numpy.array((12, 12)) x_offset = offset[0] y_offset = offset[1] img_w = img.shape[1] img_h = img.shape[0] background_w = background.shape[1] background_h = background.shape[0] x = x_offset y = y_offset for i in range(0, 16): background[y:y + img.shape[0], x:x + img.shape[1]] = img x += img_w + x_offset if x > background_w - img_w: x = x_offset y += img_h + y_offset cv2.imshow("test", background) cv2.imwrite("background.jpg", background) cv2.waitKey(0) cv2.destroyAllWindows()
that generates grid like this one:
So cv2.imshow
shows the grid but cv2.imwrite
writes only initial black background not the grid for some reason. How to fix that?
Advertisement
Answer
You need to scale the color channels:
cv2.imwrite("background.jpg", background)
cv2.imwrite("background.jpg", background * 255)
Alternatively you can create a “white” image with type uint8
:
img = numpy.ones((255, 465, 3))
img = numpy.ones((255, 465, 3), dtype = numpy.uint8) * 255