Who8MyLunch / CharPyLS

JPEG-LS for Python via CharLS C++ Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example code exception

colbro opened this issue · comments

Environment: Centos 6 (64 bit) VM in VirtualBox 4.3.6 on Windows 7

Just installed CharLS and CharPyLS.
Copied the test/gray_raw.png to test/image.png

[root@dev01 jpeg_ls]# python2.7 example.py
Traceback (most recent call last):
File "example.py", line 15, in
data_buffer = jpeg_ls.encode(data_image)
File "/usr/local/lib/python2.7/site-packages/CharPyLS-1.0.0-py2.7-linux-x86_64.egg/jpeg_ls/CharLS.py", line 47, in encode
data_buffer = _CharLS.encode(data_image)
File "_CharLS.pyx", line 119, in _CharLS.encode (jpeg_ls/_CharLS.cpp:1721)
raise Exception('Invalid input data type %s' % data_image.dtype)
Exception: Invalid input data type object

Hey, thanks for finding a bug! I am currently out of town on vacation, so most likely I won't have time to investigate until Monday.

Hi Pierre

Thank you for the quick and very polite response!
I am currently upgrading some software we have that decodes the Japanese MTSAT weather satellite imagery (that is encoded with the lossless JPEG algorithm).
The plan is to replace the bespoke C implementation we got going many years ago with Jpeg_LS and merge that into our Python code.
The data is in unsigned 16 bit pixels. Does your interface allow me to pass 16 bit encoded data?

Cheers
Colin Brown

From: Pierre V. Villeneuve [mailto:notifications@github.com]
Sent: Saturday, 11 January 2014 11:48 a.m.
To: Who8MyLunch/CharPyLS
Cc: Colin Brown
Subject: Re: [CharPyLS] Example code exception (#1)

Hey, thanks for finding a bug! I am currently out of town on vacation, so most likely I won't have time to investigate until Monday.


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-32074619.

This email and any attachments may contain confidential information. If you
are not the intended recipient, your use or communication of the information
is strictly prohibited. If you have received this message in error please
notify MetService immediately.

Hi @colbro,

This JPEG_LS implementation should work perfectly fine with unsigned 16-bit data.

I have started looking into what might be causing this problem. I have replicated the steps you have outlined, but I don't see any error on my end. The one odd thing I see in the error message you copied/pasted is the part right at the end:

File "CharLS.pyx", line 119, in _CharLS.encode (jpegls/CharLS.cpp:1721)
raise Exception('Invalid input data type %s' % dataimage.dtype)
Exception: Invalid input data type object

The input data is expected to be np.uint8 or np.uint16, but your error message shows dtype = object`.

I have updated the files _CharLS.pyx and example.py with more descriptive error and informational messages. Would you mind trying again and reporting back what you see?

Hi Pierre

I have found the cause of the issue. By default the PIL, PIL.Image installation on Linux did not link in the decoder that PIL requires for PNG files (see below). It is not nice that the np.asarray does not raise the same error as trying to save the image does! Maybe this should be raised with the numpy team? As it stands now data_io.py is "broken".

Cheers
Colin

Python 2.7.5 (default, Nov 25 2013, 21:55:41)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> import PIL.Image
>>> img = PIL.Image.open('test/image.png')
>>> print img.format, img.size, img.mode
PNG (2592, 1944) L
>>> import numpy as np
>>> data = np.asarray(img)
>>> type(data)
<type 'numpy.ndarray'>
>>> print('Shape: {:s}'.format(data.shape))
Shape: ()
>>> print('Shape: {:s}'.format(data.dtype))
Shape: object
>>> img.save('x.png')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 1406, in save
self.load()
File "/usr/local/lib/python2.7/site-packages/PIL/ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder zip not available
>>>

After adding libs and re-installing Pillow example code works now (but maybe you should have a copy of image.png in test already) ;-)

[root@dev01 jpeg_ls]# cp test/gray_raw.png test/image.png
cp: overwrite `test/image.png'? y
[root@dev01 jpeg_ls]# python example.py

Data properties
Type: uint8
Shape: (1944, 2592)

Size of uncompressed image data: 5038848
Size of PNG encoded data file: 2409950
Size of JPEG-LS encoded data: 2088357

Restored data is identical to original?: True
[root@dev01 jpeg_ls]#

The file example.py is updated to point to an existing png file under the test folder.