2016-07-28 179 views
1

在C#中,我可以使用Bitmap.lockbits()來訪問位圖作爲字節數組。如何在PIL中做到這一點?我嘗試過Image.write(),但是它將一個完整格式的圖像寫入了一個流。轉換PIL圖像爲bytearray

+0

也許'Image.tobytes()'? –

+0

@StefanPochmann,請參閱['''tobytes'''](https://pillow.readthedocs.io/en/3.3.x/reference/Image.html#PIL.Image.Image.tobytes)方法中的警告。 – wwii

+0

@wwii那是怎麼回事? –

回答

4
from io import BytesIO 
from PIL import Image 

with BytesIO() as output: 
    with Image.open(path_to_image) as img: 
     img.save(output, 'BMP') 
    data = output.getvalue()