2014-02-05 46 views
1

我使用枕頭版本2.2.2將webp圖像轉換爲jpeg圖像。 webp圖像存儲在內存緩衝區中。我發現,當我嘗試tom打開webp圖像時,導致大量圖像的內存泄漏成爲一個真正的問題。使用帶有枕頭2.2.2或2.3.0的webp圖像

def webp_to_jpeg(raw_img): 
image = Image.open(StringIO.StringIO(raw_img)) 
buffer = StringIO.StringIO() 
image.save(buffer, "JPEG") 
return string_buffer.getvalue() 

此內存泄漏只發生在我使用webp圖像時。我嘗試將枕頭更新爲2.3.0,但是當我這樣做時,我根本無法讀取webp圖像,並且我得到以下異常「WEBP未知擴展名」

回答

3

這是PILLOW中的webp解碼器錯誤(請參閱here)。 它仍然在2.4.0版本中泄漏內存

我找到的唯一的解決方法是基於python-webm。這一個正在泄漏內存太多,但你能解決這個問題:

在encode.py,進口的libc free()函數:

from ctypes import CDLL, c_void_p 
libc = CDLL(find_library("c")) 
libc.free.argtypes = (c_void_p,) 
libc.free.restype = None 

然後修改_decode()釋放在WEBP解碼器分配的緩衝區。 DLL:

def _decode(data, decode_func, pixel_sz): 
    bitmap = None 
    width = c_int(-1) 
    height = c_int(-1) 
    size = len(data) 

    bitmap_p = decode_func(str(data), size, width, height) 
    if bitmap_p is not None: 
     # Copy decoded data into a buffer 
     width = width.value 
     height = height.value 
     size = width * height * pixel_sz 
     bitmap = create_string_buffer(size) 

     memmove(bitmap, bitmap_p, size) 

     #Free the wepb decoder buffer! 
     libc.free(bitmap_p) 

    return (bytearray(bitmap), width, height) 

要轉換RGB圖像WEBP:

from webm import decode 

def RGBwebp_to_jpeg(raw_img): 
    result = decode.DecodeRGB(raw_img) 
    if result is not None: 
     image = Image.frombuffer('RGB', (result.width, result.height), str(result.bitmap),'raw', 'RGB', 0, 1) 

     buffer = StringIO.StringIO() 
     image.save(buffer, "JPEG") 
     return buffer.getvalue() 
1

枕頭2.3.0修正了讀取change log

Fixed memory leak saving images as webp when webpmux is available [cezarsa] 

據我所知枕頭是依靠osp支持。

你試過這個嗎? https://stackoverflow.com/a/19861234/756056

+0

我已經WEBP-dev的安裝,因爲我menti oned在我的帖子枕頭2.2可以讀取和寫入webp圖像,但我注意到一個巨大的內存泄漏。隨着枕頭2.3,我得到了以下例外「WEBP未知延長」 – Ubaidah