将随机字节的bytearray转换成灰度图或者彩图。灰度图为单通道图,每个像素一个字节,8位,范围0-255,代表灰度值。灰度图是一个二维数组。彩图三通道,每个像素有三个字节,每个字节分别代表BLUE,GREEN,RED三种颜色的值,8位,范围0-255,彩图是一个三维数组。
在opencv3+python3中,cv2.imwrite()不能保存路径含中文的图片。
附代码:使用时修改路径即可
###########################################################
#
# Name: random picture
# Author: Yfj 401506102@qq.com
# Time: 2018 09 15
# Version: beta
# function: 生成随机图
#
###########################################################
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import os
import io
randomByteArray = bytearray(os.urandom(120000))
flatNumpyArray = np.array(randomByteArray)
#cv2.namedWindow("RandomGray",cv2.WINDOW_NORMAL)
#cv2.namedWindow("RandomColor",cv2.WINDOW_NORMAL)
grayImage = flatNumpyArray.reshape(300,400)
cv2.imwrite('E:/Python/Random_Picture/RandomGray.png',grayImage)
#cv2.imshow('RandomGray',grayImage)
bgrImage = flatNumpyArray.reshape(100,400,3)
cv2.imwrite('E:/Python/Random_Picture/RandomColor.png',bgrImage)
#cv2.imshow('RandomColor',bgrImage)
#cv2.waitKey(0)
#cv2.destroyAllWindows()