I would like to create following 2x2 image from numpy array using OpenCV.
I tried to create it with this code snippet.
import cv2
import numpy as np
blue = (255, 0, 0)
green = (0, 255, 0)
red = (0, 0, 255)
image = np.array([
[blue, green],
[red, blue]
])
cv2.imwrite('2x2.jpg', image)
I used BGR color format, yet when I view the output image, I see a colorless image.
What am I missing?
Environment
- OpenCV 4.5.1.48
- Python 3.9.1
- Windows 10



JPEGencoding is over compressing your original data. Try saving the file as aPNGinstead, withcv2.imwrite('2x2.png', image).IMWRITE_JPEG_LUMA_QUALITYandIMWRITE_JPEG_CHROMA_QUALITYand they are not equal, subsampling is turned off: github.com/opencv/opencv/blob/3.4/modules/imgcodecs/src/… | Unfortunately this needsJPEG_LIB_VERSION >= 70, and sadly the library shipped with OpenCV and used to build the standard releases (at least on Windows) is only version 62.Image.fromarray(image[...,::-1]).save('2x2_PIL.jpg', quality=100, subsampling=0)