简体   繁体   中英

Python ValueError: embedded null byte when reading png file from bash pipe

from PIL import Image
from subprocess import Popen, PIPE

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

Error:

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM