Reputation: 375
There are some similar questions here and especially here, but they don't go into quite the detail I'm seeking.
I'm attempting to read and edit JP2 (aka, JPEG2000) files using pillow. My code is below, and I suppose I don't yet have the OpenJPEG library installed, because I get the error module 'PIL._imaging' has no attribute 'jpeg2k_decoder'
. So, I believe that I neeed to install OpenJPEG. Because I it's a system install (not contained within a virtual environment) and the instructions are piecemeal, I'm hoping to get more info.
Pillow's documentation indicates:
To enable JPEG 2000 support, you need to build and install the OpenJPEG library, version 2.0.0 or higher, before building the Python Imaging Library.
And OpenJPEG's install instructions aren't very clear, but seems to also require installation of cmake. The general advice I see everywhere is to uninstall pillow, install the OpenJPEG library, and then reinstall pillow. And I see that people often have to troubleshoot because pillow's reinstall doesn't pick up the new library sometimes.
I'm hoping that those who have been through the process on a Mac can advise on some details:
Code that returns the error:
from PIL import Image
import urllib.request
from io import BytesIO
url = 'https://chroniclingamerica.loc.gov/data/batches/dlc_golf_ver01/data/sn84025891/100493731/1901082401/0132.jp2'
img_raw = requests.get(url)
img = Image.open(BytesIO(img_raw.content))
display(img)
------ Outputs an error including: -------
AttributeError: module 'PIL._imaging' has no attribute 'jpeg2k_decoder'
Thanks!
Upvotes: 0
Views: 1541
Reputation: 207903
IMHO, the easiest way to manage packages under macOS, such as OpenJPEG, is with homebrew.
All you need is:
brew install openjpeg
Search for available packages containing "jp" with:
brew search jp
Sample Output
==> Formulae
jp jpdfbookmarks jpeg-archive jpeg-xl ✔ jpegoptim mjpegtools openjpeg ✔ recoverjpeg
jp2a jpeg ✔ jpeg-turbo ✔ jpeginfo jpegrescan mozjpeg pjproject
Get info about a package with:
brew info openjpeg
Sample Output
openjpeg: stable 2.4.0 (bottled), HEAD
Library for JPEG-2000 image manipulation
https://www.openjpeg.org/
/opt/homebrew/Cellar/openjpeg/2.4.0 (523 files, 13.6MB) *
Poured from bottle on 2021-11-25 at 16:35:14
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjpeg.rb
License: BSD-2-Clause
==> Dependencies
Build: cmake ✘, doxygen ✘
Required: libpng ✔, libtiff ✔, little-cms2 ✔
==> Options
--HEAD
Install HEAD version
==> Analytics
install: 82,101 (30 days), 247,510 (90 days), 1,217,811 (365 days)
install-on-request: 1,835 (30 days), 5,246 (90 days), 20,835 (365 days)
build-error: 0 (30 days)
Check what files are in a package with:
brew ls openjpeg
Sample Output
/opt/homebrew/Cellar/openjpeg/2.4.0/bin/opj_compress
/opt/homebrew/Cellar/openjpeg/2.4.0/bin/opj_decompress
/opt/homebrew/Cellar/openjpeg/2.4.0/bin/opj_dump
/opt/homebrew/Cellar/openjpeg/2.4.0/include/openjpeg-2.4/ (3 files)
/opt/homebrew/Cellar/openjpeg/2.4.0/lib/libopenjp2.2.4.0.dylib
/opt/homebrew/Cellar/openjpeg/2.4.0/lib/openjpeg-2.4/ (3 files)
/opt/homebrew/Cellar/openjpeg/2.4.0/lib/pkgconfig/libopenjp2.pc
/opt/homebrew/Cellar/openjpeg/2.4.0/lib/ (3 other files)
/opt/homebrew/Cellar/openjpeg/2.4.0/share/doc/ (497 files)
/opt/homebrew/Cellar/openjpeg/2.4.0/share/man/ (4 files)
Check how PIL is configured with:
python3 -m PIL # or use "pythonXXX -m PIL" if that's how you start Python
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
--------------------------------------------------------------------
Upvotes: 1