Today when browsing Zhihu, I came across this interesting package in python where I can generate digital images by creating a matrix of RGB parameters. Here I created a very simple example to generate pure color image.

First install package opencv-python:

1
pip install opencv-python

Then import the cv2 module and use the imwrite function to create image with specified color.

1
2
3
4
5
6
7
8
9
10
11
12
import cv2
import numpy as np

def generate_imgage_hex(size, BGR):
return np.ones((size[0], size[1], 1)) * \
[int(BGR[4:6], 16),
int(BGR[2:4], 16),
int(BGR[0:2], 16)]

size = [1080, 1080]
color = "314159"
cv2.imwrite('image.png', generate_imgage_hex(size, color))

We have just created a image with color #594131 in each pixels.



image.png

The openCV package was first released in 2000, and currently supports algorithms related to computer vision and machine learning.




Reference: