首页 > 解决方案 > 如何在python中创建图像并保存

问题描述

我想创建一个图像并将其存储为使用 python 的文件。

我希望输出是具有黑色或白色色块的图像,并且图像必须具有 6x6 随机的白色或黑色色块块。

标签: python-3.xopencvjupyter-notebook

解决方案


您可以使用此代码来解决该问题:

#Import all libraries we will use
import random
import numpy as np
import cv2

#let's create a 6 x 6 matrix with all pixels in black color
img = np.zeros((6,6,3),np.uint8)

#let's use "for" cycle to change colorspace of pixel in a random way
for x in range(6):
    for y in range(6):
        #We use "0" for black color (do nothing) and "1" for white color (change pixel value to [255,255,255])
        value = random.randint(0,1)
        if value == 1:
            img[x,y] = [255,255,255]

#save our image as a "png" image
cv2.imwrite("6_x_6.png",img)


推荐阅读