0

webpイメージの種類を特定してフォーマットかどうかを判断したいのですがfile、イメージはインターネットからダウンロードしたバイナリとしてメモリに保存されているため、コマンドを使用することはできません。PILこれまでのところ、 libまたはimghdrlibでこれを行う方法が見つかりません

これが私がしたくないことです:

from PIL import Image
import imghdr

image_type = imghdr.what("test.webp")

if not image_type:
    print "err"
else:
    print image_type

# if the image is **webp** then I will convert it to 
# "jpeg", else I won't bother to do the converting job 
# because rerendering a image with JPG will cause information loss.

im = Image.open("test.webp").convert("RGB")
im.save("test.jpg","jpeg")

そして、これ"test.webp"が実際にwebp画像でvar image_typeある場合None、ライブラリがタイプをimghdr認識していないことを示すのはどれですか?Pythonで確実に画像であると判断できる方法はありますか?webpwebp


記録のために、私はpython 2.7を使用しています


4

1 に答える 1

3

imghdrモジュールはまだ webp 画像検出をサポートしていません。Python 3.5 に追加される予定です。

古いバージョンの Python に追加するのは簡単です。

import imghdr

try:
    imghdr.test_webp
except AttributeError:
    # add in webp test, see http://bugs.python.org/issue20197
    def test_webp(h, f):
        if h.startswith(b'RIFF') and h[8:12] == b'WEBP':
            return 'webp'

    imghdr.tests.append(test_webp)
于 2015-01-22T09:42:26.120 に答える