バイト配列を_Image.open
_とImage.verify()
で検証し、最初にディスクに書き込まずに、次にim = Image.open()
で開きます。 .readfrombuffer()
および.readfromstring()
メソッドを見ましたが、そこには画像のサイズが必要です(バイトストリームを画像に変換するときにのみ取得できます)。
私の読み取り関数は次のようになります。
_def readimage(path):
bytes = bytearray()
count = os.stat(path).st_size / 2
with open(path, "rb") as f:
print "file opened"
bytes = array('h')
bytes.fromfile(f, count)
return bytes
_
次に、基本的なテストとして、bytearrayをイメージに変換します。
_bytes = readimage(path+extension)
im = Image.open(StringIO(bytes))
im.save(savepath)
_
誰かが私が間違っていることを知っている場合、またはこれらのバイトを本当に助けになる画像に変換するよりエレガントな方法がある場合。
追伸:バイトを操作する(画像をグリッチする)ため、バイト配列が必要だと思いました。これは機能しましたが、ディスクに書き込まずにディスクからイメージファイルを再度開いて破損していないかどうかを確認せずにやりたいと思いました。
編集:それは私に与えるすべては_IOError: cannot identify image file
_です
bytearrays
で操作する場合は、io.BytesIO
を使用する必要があります。また、bytearray
に直接ファイルを読み込むことができます。
import os
import io
import Image
from array import array
def readimage(path):
count = os.stat(path).st_size / 2
with open(path, "rb") as f:
return bytearray(f.read())
bytes = readimage(path+extension)
image = Image.open(io.BytesIO(bytes))
image.save(savepath)