PIL: Convertir Bytearray a imagen
Estoy tratando de verificar un bytearray conImage.open
yImage.verify()
sin escribirlo en el disco primero y luego abrirlo conim = Image.open()
. Miré el.readfrombuffer()
y.readfromstring()
Método, pero ahí necesito el tamaño de la imagen (que solo podría obtener al convertir el bytestream en una imagen).
Mi función de lectura se ve así:
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
Luego, como prueba básica, trato de convertir el bytearray en una imagen:
bytes = readimage(path+extension)
im = Image.open(StringIO(bytes))
im.save(savepath)
Si alguien sabe lo que estoy haciendo mal o si hay una forma más elegante de convertir esos bytes en una imagen que realmente me ayudaría.
P.S .: Pensé que necesito el bytearray porque hago manipulaciones en los bytes (glitch ellos imágenes). Esto funcionó, pero quería hacerlo sin escribirlo en el disco y luego abrir el archivo de imagen del disco nuevamente para verificar si está roto o no.
Edit: todo lo que me da es unIOError: cannot identify image file