Opencv random bytes to generate grayscale images and BGR images

  1.  Create randomPic.py file
  2. import cv2
    import numpy
    import os
    
    #产生随机数组
    randomByteArray = bytearray(os.urandom(120000))
    flatNumpyArray = numpy.array(randomByteArray)
    
    #数组转换为一个300*400的灰度图像
    grayImage = flatNumpyArray.reshape(300,400)
    cv2.imwrite('RandomGray.png',grayImage)
    
    #数组转换为一个100*400的彩色图像
    bgrImage = flatNumpyArray.reshape(100,400,3)
    cv2.imwrite('RandomColor.png',bgrImage)
  3. Running the script will generate two random images, the image names RandomGray.png and RandomColor.png.

Guess you like

Origin blog.csdn.net/sinat_38685910/article/details/94441605