2

pillow バージョン 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 画像を操作する場合にのみ発生します。pillow を 2.3.0 に更新しようとしましたが、それを行ったときに webp 画像をまったく読み取ることができず、「WEBP 不明な拡張子」という例外が発生しました。

4

2 に答える 2

3

これは、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()
于 2014-05-30T18:16:34.117 に答える
1

Pillow 2.3.0 では、変更ログを読み取る際のメモリ リークが修正されています。

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

私の知る限り、枕はOS WebPサポートに依存しています。

これを試してみましたか?https://stackoverflow.com/a/19861234/756056

于 2014-02-23T02:07:06.883 に答える