python-3.x - 如何在python中创建图像并保存
问题描述
我想创建一个图像并将其存储为使用 python 的文件。
我希望输出是具有黑色或白色色块的图像,并且图像必须具有 6x6 随机的白色或黑色色块块。
解决方案
您可以使用此代码来解决该问题:
#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)
推荐阅读
- eclipse - Webdriver 是否支持在驱动程序实例中设置代理?无法使用所需功能在驱动程序中设置代理
- c# - Automapper 忽略嵌套集合属性
- haskell - 以区间为键的类地图容器和类 zip 组合操作
- c# - 并行代码中的多样化输出
- javascript - 如何在 for 循环中获取表格中的特定行高,最后我需要添加每个行高
- javascript - 从 this.props.children 中获取值
- cassandra - 当 read-your-own-writes 会失败?
- facebook - 获得 Facebook App 批准以进行注册、登录和分享
- linux - 仅当先前返回 0 时如何启动下一个 shell 脚本?
- vba - 使用 VBA 在 Word 中自动更改文档结构