How do I install OpenJPEG on Windows and use it with Pillow?
Asked Answered
M

3

3

I would like to use the Python Pillow library to save 16 bit gray scale arrays in the jp2 ("JPEG 2000") format.

I have hit a brick wall in trying to install the required library OpenJPEG on my Windows machine. The documentation is not very clear... but I assumed that I needed to download the Win64 binaries and simply put them on my path ( which I did).

That done, I am still getting the following error when using Pillow 4.0.0 in Anaconda 4.3.0 on Windows.

IOError: encoder jpeg2k not available

Anyone out there successfully used Pillow to write and read JPEG 2000 files, I would sure appreciate some tips.

Edit:

Here is the code that fails:

import PIL
import numpy as np

arr = np.ones(dtype=np.uint16, shape=(100, 100))
im = PIL.Image.fromarray(arr)
im.save('arr.jp2')
Mita answered 12/2, 2017 at 6:56 Comment(4)
Please, provide your code, that fails. Error message says "decoder". Are you trying to open file? If yes, then, please, provide an example file, so anyone could test on it.Housebroken
Sorry, i was typing this from memory. It wasn't "decoder" error; it was "encoder." Also I have included the code snipped that fails. The documents for Pillow state that I need to have OpenJpeg installed... that's what I'm having the difficulty with.Mita
For me it fails on im = ... with an error "Cannot handle this datatype". What pillow version do you have (output form conda list)?Housebroken
Pillow 4.0.0. Yes that was a bug that was just recently fixed.Mita
H
1

I've just installed Pillow with an installer from here. I chose Pillow-4.0.0.win-amd64-py3.5.exe. During install it found conda's python and properly chose where to install (it installed to a root environment).

Code to test it works:

from PIL import Image
import numpy as np
arr = np.ones(dtype=np.uint16, shape=(100,100))
im = Image.fromarray(arr)
im.save('test.jp2') 

Note, that saved file has 8 bpp.

Housebroken answered 12/2, 2017 at 13:4 Comment(1)
So after all that it saved as 8 bit? Ughh. The whole point i am doing this is to save as 16 bit. :( That is disheartening. Thank you.Mita
A
0

Anaconda build Python using different version of the microsoft visual studio tools.

Each version of those tools has its own runtime, which is incompatible with other versions.

The Pillow library used compiled shared libraries. You will need to compile OpenJPEG with exactly the same version of the ms visual studio tools that was used to build Python and Pillow.

Adige answered 12/2, 2017 at 8:19 Comment(2)
So did u compile OpenJpeg too?Mita
@KeithL I don't use ms-windows.Adige
E
-1

For general reference.

The Windows equivalent to 'nix .so files have the extension .dll (sic - "Windows Binaries" - dynamic linked library); and yes, the file has to reside somewhere in the system PATH.

Being in the PATH allows Windows to find the file, but that's not enough. Windows has to be told what can be done with it; that it's a shareable library. That's done by:

1) Open a DOS Command Prompt in the (sub)directory where the binary is located; e.g. C:\LIBS

2) Run the command "regsvr32 filename.dll". This registers the .dll as a shared file (in Windows Registry), so that Windows knows how to load it into memory and let user applications access it.

You can actually run regsvr32 from any directory (it's a System file & should be somewhere in the C:\Windows\system32 directory; but it's more convenient to run in the same directory as the .dll because otherwise you have to prepend filename.dll with the entire directory tree from C:\ to where ever the file is located.

You can run "regsvr32" with no target filename to get a popup list of command-line switches which can be used.

Essayistic answered 17/7, 2019 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.