これは、PILLOW の webp デコーダーのバグです (こちらを参照)。バージョン 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()