Retrieve a list of supported read file extensions/formats
Asked Answered
P

3

6

Here is a list of all image formats that Pillow supports. Since I want to check formats in my application without opening the image and the list of formats will probably change in the future, I wonder if there is an option to retrieve a list of supported file extensions/formats programmatically?

I want to do something like this:

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

I am especially interested in reading formats (so writing does not need to be supported).

Pretor answered 14/2, 2022 at 13:53 Comment(0)
P
13

With the help of @Mark Setchell from the previous answer and with a bit of browsing through the Pillow features module, I produced a complete code that gives me a set of all image file extensions that can be opened by Pillow.

from PIL import Image

exts = Image.registered_extensions()
supported_extensions = {ex for ex, f in exts.items() if f in Image.OPEN}
Pretor answered 14/2, 2022 at 15:21 Comment(3)
I had this exact problem and this works correctly, consider accepting your own responseSapir
Beware that this method can be inaccurate if you imported pnginfo anywhere in your script before importing Image, as pnginfo will override it to only have PNG formatsAxilla
Keep in mind that although PIL can open some file types, it can't save to all file formats listed, and it can save to some formats which it can't open for reading. To find all the formats that PIL can open replace the Image.OPEN with Image.SAVE, and to find formats that can be opened but not saved (and vice versa) use this: can_open_but_cant_save = [i for i in openable if i not in savable] and can_save_but_cant_open = [i for i in savable if i not in openable]. (Example: you can't read pdfs but you can write pdfs; you can't write psds but you can read psds.)Phew
G
3

You can use 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)

Sample Output

--------------------------------------------------------------------
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
--------------------------------------------------------------------

There is also:

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

You can also check from outside of your Python interpreter like this:

python3 -m PIL
Garrison answered 14/2, 2022 at 14:3 Comment(2)
Thanks for the answer and guiding me to the features module. I produced a simple code snippet that gives me exactly what I need in a separate answer. Do you think it dangerous to use Image.OPEN or Image.registered_extensions()?Pretor
@Pretor Well done - glad you got it working. As regards it being "dangerous", I guess it depends on your environment, your needs and your appetite for risk.Garrison
B
-2

Thanks @primoz for the answer. I managed to get it working in Python/Flask using the following:

from PIL import Image

exts = Image.registered_extensions()
supported_extensions = [ex[1: for ex, f in exts.items() if f in Image.OPEN]

And then in the form:

picture = FileField('Update Profile Picture', validators=[FileAllowed(supported_extensions)])
Bigeye answered 24/9, 2022 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.