[英]Using webp images with Pillow 2.2.2 or 2.3.0
我正在使用枕头版本2.2.2将webp图像转换为jpeg图像。 webp图像存储在内存缓冲区中。 我发现当尝试打开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未知扩展名”
这是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()
枕头2.3.0修复了读取更改日志时的一些内存泄漏:
Fixed memory leak saving images as webp when webpmux is available [cezarsa]
据我所知,枕头是靠os webp支持的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.