2016-09-21 31 views
0

我用兩個包 from PIL import Imagefrom resizeimage import resizeimage當我使用這個包,我應該調整後保存到其他圖像文件,但我需要像二進制後resize在不保存爲其他文件?如何在python上調整圖像大小後讀取二進制文件?

代碼:

with open(imgpath, 'r+b') as f: 
    with Image.open(f) as image: 
     cover = resizeimage.resize_cover(image, [100, 100]) 
      img = bytearray(cover.read()) 

我需要讀取二進制這樣的:bytearray(cover.read())此代碼不能正常工作, 如何閱讀後調整大小圖像的二進制?

+0

你的方法是'binary',但你的變量不是'binary'! **'StringIO'?** – dsgdfg

+0

請看看答案, –

回答

0

請使用此代碼。

basewidth = 100 
hsize = 100 
img = Image.open(imgpath) 
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) 
byte_io = io.BytesIO() 
img.save(byte_io, format='PNG') 
byte_io = byte_io.getvalue() 
print byte_io 
相關問題