Python ValueError:从bash管道读取png文件时嵌入了空字节 [英] Python ValueError: embedded null byte when reading png file from bash pipe

查看:261
本文介绍了Python ValueError:从bash管道读取png文件时嵌入了空字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from PIL import Image
from subprocess import Popen, PIPE

scr = Image.open(Popen.communicate(Popen(['import','-w','0x02a00001','png:-'], stdout=PIPE))[0])

错误:

  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2258, in open
    fp = builtins.open(filename, "rb")
ValueError: embedded null byte

推荐答案

首先尝试将原始数据加载到BytesIO容器中:

Try first to load raw data into a BytesIO container:

from io import BytesIO
from PIL import Image
from subprocess import Popen, PIPE

data = Popen.communicate(Popen(['import','-w','0x02a00001','png:-'], stdout=PIPE))[0]
scr = Image.open(BytesIO(data))

这篇关于Python ValueError:从bash管道读取png文件时嵌入了空字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆