数组转化为图像Converting between an image and raw bytes

import cv2
import numpy
import os

# Make an array of 120,000 random bytes.
randomByteArray = bytearray(os.urandom(120000))
flatNumpyArray = numpy.array(randomByteArray)

# Convert the array to make a 400x300 grayscale image.
grayImage = flatNumpyArray.reshape(300, 400)
cv2.imwrite('RandomGray.png', grayImage)

# Convert the array to make a 400x100 color image.
bgrImage = flatNumpyArray.reshape(100, 400, 3)
cv2.imwrite('RandomColor.png', bgrImage)

 运行脚本后会在脚本的路径中找到两个随机值构成的图像 RandomGray.png and RandomColor.png 

相关文章:

  • 2022-12-23
  • 2021-08-21
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-09-20
  • 2021-12-08
猜你喜欢
  • 2021-11-10
  • 2021-05-01
  • 2022-03-02
  • 2021-04-15
相关资源
相似解决方案