这是 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()