我正在尝试将字节数组转换为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)
答案 0 :(得分:0)
如果不代表开销,可以使用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
如果数据已在[(蓝色,绿色,红色),(),...,()]中按字节顺序排列:
# Then to save:
cv2.imwrite('img1.png',reply)