【问题标题】:How to install OpenJPEG on MacOS, for use with JP2 files and Pillow如何在 MacOS 上安装 OpenJPEG,用于 JP2 文件和 Pillow
【发布时间】:2022-02-08 00:30:36
【问题描述】:

有一些类似的问题here 尤其是here,但它们并没有深入我所寻求的细节。

我正在尝试使用枕头读取和编辑 JP2(又名 JPEG2000)文件。我的代码在下面,我想我还没有安装 OpenJPEG 库,因为我收到错误 module 'PIL._imaging' has no attribute 'jpeg2k_decoder'。所以,我相信我需要安装 OpenJPEG。因为我是系统安装(不包含在虚拟环境中)并且说明是零碎的,我希望获得更多信息。

Pillow's documentation 表示:

要启用 JPEG 2000 支持,您需要在构建 Python 图像库之前构建并安装 OpenJPEG 库(版本 2.0.0 或更高版本)。

OpenJPEG 的install instructions 不是很清楚,但似乎还需要安装cmake。我到处看到的一般建议是卸载枕头,安装 OpenJPEG 库,然后重新安装枕头。而且我看到人们经常不得不进行故障排除,因为枕头的重新安装有时不会选择新的库。

我希望那些在 Mac 上经历过这个过程的人可以就一些细节提出建议:

  1. 安装 OpenJPEG 似乎需要 cmake。 https://cmake.org/install/ 的说明是否足够?
  2. OpenJPEG instructions 似乎不完整。有没有人制定了更容易遵循的分步说明?

返回错误的代码:

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'

谢谢!

【问题讨论】:

    标签: python cmake python-imaging-library jpeg2000


    【解决方案1】:

    恕我直言,在 ma​​cOS(例如 OpenJPEG)下管理软件包的最简单方法是使用 homebrew

    你只需要:

    brew install openjpeg
    

    搜索包含“jp”的可用包:

    brew search jp  
    

    样本输出

    ==> Formulae
    jp              jpdfbookmarks   jpeg-archive    jpeg-xl ✔       jpegoptim       mjpegtools      openjpeg ✔      recoverjpeg
    jp2a            jpeg ✔          jpeg-turbo ✔    jpeginfo        jpegrescan      mozjpeg         pjproject
    

    通过以下方式获取有关包的信息:

    brew info openjpeg
    

    样本输出

    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)
    

    检查包中有哪些文件:

    brew ls openjpeg
    

    样本输出

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

    检查 PIL 的配置方式:

    python3 -m PIL        # or use "pythonXXX -m PIL" if that's how you start Python
    

    样本输出

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

    【讨论】:

    • 谢谢,虽然 OpenJPEG 不是 python 库。您引用的 python 库是一个包装器,安装它不会安装实际的 OpenJPEG。
    • 对不起,我误会了。看看我在 macOS 下构建 Pillow 的答案的结尾stackoverflow.com/a/61969447/2836621
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2020-11-07
    • 2017-06-10
    相关资源
    最近更新 更多