【问题标题】:How can I convert an byte array to PNG image and save it to a specific folder?如何将字节数组转换为 PNG 图像并将其保存到特定文件夹?
【发布时间】:2018-05-11 20:58:19
【问题描述】:

我正在尝试将字节数组转换为PNG 图像并将其保存到特定文件夹。字节数组来自C#服务器,python客户端会收到并保存为PNG图片。

我尝试了以下方法:

reply = s.recv(4096)
image = Image.open(io.BytesIO(reply))
image.save("img1.png","PNG")

它给出以下错误:

Traceback (most recent call last):
File "C:\Users\imran.s\Desktop\UnityClient.py", line 46, in <module>
 image.save("img1.png","PNG")

File "C:\python64\lib\site-packages\PIL\Image.py", line 1895, in save
self.load()

File "C:\python64\lib\site-packages\PIL\ImageFile.py", line 233, in load
"(%d bytes not processed)" % len(b))

OSError: image file is truncated (3 bytes not processed)

【问题讨论】:

    标签: python sockets python-imaging-library python-sockets


    【解决方案1】:

    如果不代表开销,您可以使用 OpenCV 图像处理库。 OpenCV 处理图像打开和保存,如果需要,您可以对其进行处理。

    如果您的数据是 RGB 格式,您可以:

    import cv2
    
    # Loop to Load each pixel values in positions in array
    for ...
       image[x, y] = (0, 0, 255) # B, G, R : different order
    

    如果数据已经在 [(Blue, Green, Red), (), ... ,()] 中按字节排序:

    # Then to save: 
    cv2.imwrite('img1.png',reply)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多