web-dev-qa-db-ja.com

Python ValueError:bashパイプからpngファイルを読み取るときに埋め込まれたnullバイト

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
14
Tengu

まず、生データをBytesIOコンテナにロードしてみます。

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))
23
Tiger-222