python - PIL: Convert Bytearray to Image -


i trying verify bytearray image.open , image.verify() without writing disk first , open im = image.open(). looked @ .readfrombuffer() , .readfromstring() method, there need size of image (which when converting bytestream image).

my read-function looks this:

def readimage(path): bytes = bytearray() count = os.stat(path).st_size / 2 open(path, "rb") f:     print "file opened"     bytes = array('h')     bytes.fromfile(f, count) return bytes 

then basic test try convert bytearray image:

bytes = readimage(path+extension) im = image.open(stringio(bytes)) im.save(savepath) 

if knows doing wrong or if there more elegant way convert bytes image that'd me.

p.s.: thought need bytearray because manipulations on bytes (glitch them images). did work, wanted without writing disk , opening imagefile disk again check if broken or not.

edit: gives me ioerror: cannot identify image file

if manipulate bytearrays, have use io.bytesio. can read file directly bytearray.

import os import io import image array import array  def readimage(path):     count = os.stat(path).st_size / 2     open(path, "rb") f:         return bytearray(f.read())  bytes = readimage(path+extension) image = image.open(io.bytesio(bytes)) image.save(savepath) 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -