【问题标题】:Retrieve a list of supported read file extensions/formats检索支持的读取文件扩展名/格式列表
【发布时间】:2022-02-14 23:21:14
【问题描述】:

Here 是 Pillow 支持的所有图像格式的列表。由于我想在不打开图像的情况下检查应用程序中的格式,并且格式列表将来可能会发生变化,我想知道是否可以选择以编程方式检索支持的文件扩展名/格式列表?

我想做这样的事情:

>>> <import something from Pillow>
>>> formats = <call something from Pillow>
>>> formats
["jpg", "jpeg", "png", ...]

我对阅读格式特别感兴趣(所以不需要支持写作)。

【问题讨论】:

    标签: python python-3.x python-imaging-library image-formats


    【解决方案1】:

    在上一个答案中@Mark Setchell 的帮助下,并通过 Pillow features 模块进行了一些浏览,我生成了一个完整的代码,它为我提供了一组可以打开的所有图像文件扩展名枕头。

    from PIL import Image
    
    exts = Image.registered_extensions()
    supported_extensions = {ex for ex, f in exts.items() if f in Image.OPEN}
    

    【讨论】:

    • 我遇到了这个确切的问题,并且可以正常工作,请考虑接受您自己的回复
    【解决方案2】:

    你可以使用features:

    from PIL import features
    from io import StringIO
    
    # Create buffer for pilinfo() to write into rather than stdout
    buffer = StringIO()
    features.pilinfo(out=buffer)
    
    # Parse and analyse lines
    for line in buffer.getvalue().splitlines():
        print(line)
    

    样本输出

    --------------------------------------------------------------------
    Pillow 9.0.1
    Python 3.10.0 (v3.10.0:b494f5935c, Oct  4 2021, 14:59:19) [Clang 12.0.5 (clang-1205.0.22.11)]
    --------------------------------------------------------------------
    Python modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
    Binary modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
    --------------------------------------------------------------------
    --- PIL CORE support ok, compiled for 9.0.1
    --- TKINTER support ok, loaded 8.6
    --- FREETYPE2 support ok, loaded 2.11.1
    --- LITTLECMS2 support ok, loaded 2.13
    --- WEBP support ok, loaded 1.2.2
    --- WEBP Transparency support ok
    --- WEBPMUX support ok
    --- WEBP Animation support ok
    --- JPEG support ok, compiled for libjpeg-turbo 2.1.2
    --- OPENJPEG (JPEG2000) support ok, loaded 2.4.0
    --- ZLIB (PNG/ZIP) support ok, loaded 1.2.11
    --- LIBTIFF support ok, loaded 4.2.0
    *** RAQM (Bidirectional Text) support not installed
    *** LIBIMAGEQUANT (Quantization method) support not installed
    --- XCB (X protocol) support ok
    --------------------------------------------------------------------
    BLP
    Extensions: .blp
    Features: open
    --------------------------------------------------------------------
    BMP image/bmp
    Extensions: .bmp
    Features: open, save
    --------------------------------------------------------------------
    BUFR
    Extensions: .bufr
    Features: open, save
    --------------------------------------------------------------------
    CUR
    Extensions: .cur
    Features: open
    --------------------------------------------------------------------
    DCX
    Extensions: .dcx
    Features: open
    --------------------------------------------------------------------
    DDS
    Extensions: .dds
    Features: open, save
    --------------------------------------------------------------------
    DIB image/bmp
    Extensions: .dib
    Features: open, save
    --------------------------------------------------------------------
    EPS application/postscript
    Extensions: .eps, .ps
    Features: open, save
    --------------------------------------------------------------------
    FITS
    Extensions: .fit, .fits
    Features: open, save
    --------------------------------------------------------------------
    FLI
    Extensions: .flc, .fli
    Features: open
    --------------------------------------------------------------------
    FTEX
    Extensions: .ftc, .ftu
    Features: open
    --------------------------------------------------------------------
    GBR
    Extensions: .gbr
    Features: open
    --------------------------------------------------------------------
    GIF image/gif
    Extensions: .gif
    Features: open, save, save_all
    --------------------------------------------------------------------
    GRIB
    Extensions: .grib
    Features: open, save
    --------------------------------------------------------------------
    HDF5
    Extensions: .h5, .hdf
    Features: open, save
    --------------------------------------------------------------------
    ICNS image/icns
    Extensions: .icns
    Features: open, save
    --------------------------------------------------------------------
    ICO image/x-icon
    Extensions: .ico
    Features: open, save
    --------------------------------------------------------------------
    IM
    Extensions: .im
    Features: open, save
    --------------------------------------------------------------------
    IMT
    Features: open
    --------------------------------------------------------------------
    IPTC
    Extensions: .iim
    Features: open
    --------------------------------------------------------------------
    JPEG image/jpeg
    Extensions: .jfif, .jpe, .jpeg, .jpg
    Features: open, save
    --------------------------------------------------------------------
    JPEG2000 image/jp2
    Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
    Features: open, save
    --------------------------------------------------------------------
    MCIDAS
    Features: open
    --------------------------------------------------------------------
    MPEG video/mpeg
    Extensions: .mpeg, .mpg
    Features: open
    --------------------------------------------------------------------
    MSP
    Extensions: .msp
    Features: open, save, decode
    --------------------------------------------------------------------
    PCD
    Extensions: .pcd
    Features: open
    --------------------------------------------------------------------
    PCX image/x-pcx
    Extensions: .pcx
    Features: open, save
    --------------------------------------------------------------------
    PIXAR
    Extensions: .pxr
    Features: open
    --------------------------------------------------------------------
    PNG image/png
    Extensions: .apng, .png
    Features: open, save, save_all
    --------------------------------------------------------------------
    PPM image/x-portable-anymap
    Extensions: .pbm, .pgm, .pnm, .ppm
    Features: open, save
    --------------------------------------------------------------------
    PSD image/vnd.adobe.photoshop
    Extensions: .psd
    Features: open
    --------------------------------------------------------------------
    SGI image/sgi
    Extensions: .bw, .rgb, .rgba, .sgi
    Features: open, save
    --------------------------------------------------------------------
    SPIDER
    Features: open, save
    --------------------------------------------------------------------
    SUN
    Extensions: .ras
    Features: open
    --------------------------------------------------------------------
    TGA image/x-tga
    Extensions: .icb, .tga, .vda, .vst
    Features: open, save
    --------------------------------------------------------------------
    TIFF image/tiff
    Extensions: .tif, .tiff
    Features: open, save, save_all
    --------------------------------------------------------------------
    WEBP image/webp
    Extensions: .webp
    Features: open, save, save_all
    --------------------------------------------------------------------
    WMF
    Extensions: .emf, .wmf
    Features: open, save
    --------------------------------------------------------------------
    XBM image/xbm
    Extensions: .xbm
    Features: open, save
    --------------------------------------------------------------------
    XPM image/xpm
    Extensions: .xpm
    Features: open
    --------------------------------------------------------------------
    XVTHUMB
    Features: open
    --------------------------------------------------------------------
    

    还有:

    print(features.get_supported_codecs())
    ['jpg', 'jpg_2000', 'zlib', 'libtiff']
    

    您还可以从 Python 解释器外部进行检查,例如 this

    python3 -m PIL
    

    【讨论】:

    • 感谢您的回答并指导我使用features 模块。我制作了一个简单的代码 sn-p,它在单独的答案中给出了我需要的东西。你认为使用Image.OPENImage.registered_extensions() 有危险吗?
    • @Primoz 干得好 - 很高兴你成功了。至于它是否“危险”,我想这取决于您的环境、您的需求和您对风险的偏好。
    猜你喜欢
    • 2023-01-21
    • 2014-01-18
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多