I have a code:
JavaScript
x
30
30
1
import cv2
2
import numpy
3
4
background = numpy.zeros((1080, 1920, 3))
5
img = numpy.ones((255, 465, 3))
6
offset = numpy.array((12, 12))
7
8
x_offset = offset[0]
9
y_offset = offset[1]
10
11
img_w = img.shape[1]
12
img_h = img.shape[0]
13
14
background_w = background.shape[1]
15
background_h = background.shape[0]
16
17
x = x_offset
18
y = y_offset
19
for i in range(0, 16):
20
background[y:y + img.shape[0], x:x + img.shape[1]] = img
21
x += img_w + x_offset
22
if x > background_w - img_w:
23
x = x_offset
24
y += img_h + y_offset
25
26
cv2.imshow("test", background)
27
cv2.imwrite("background.jpg", background)
28
cv2.waitKey(0)
29
cv2.destroyAllWindows()
30
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)
JavaScript
1
2
1
cv2.imwrite("background.jpg", background * 255)
2
Alternatively you can create a “white” image with type uint8
:
img = numpy.ones((255, 465, 3))
JavaScript
1
2
1
img = numpy.ones((255, 465, 3), dtype = numpy.uint8) * 255
2