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