jbaiter / jpegtran-cffi

Fast, (mostly) lossless JPEG transformations with Python
http://jpegtran-cffi.readthedocs.org
MIT License
145 stars 23 forks source link

Cannot properly convert image to numpy 3D array #12

Closed alfredox10 closed 8 years ago

alfredox10 commented 9 years ago

I have started using a new library in my Python program to process images due to speed. I was using PIL but now I am using jpegtran. My previous code I would save the image to a numpy array and that worked perfectly fine, but now since the datatype is different I am having problems creating the same numpy array.

Old code:

 In[1]: import numpy
 In[2]: from PIL import Image
 In[3]: image = Image.open('43.jpg')
 In[4]: image
Out[4]: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3216x2136 at 0x7F34CC1876C8>
 In[5]: imgArray2 = numpy.asarray(image, dtype=numpy.float32)
 In[6]: imgArray2.shape
Out[6]: (2136, 3216, 3)

New code:

 In[1]: import numpy
 In[2]: from jpegtran import JPEGImage
 In[3]: imgTran = JPEGImage('43.jpg')
 In[4]: imgTran
Out[4]: <jpegtran.transform.JPEGImage at 0x7f34d58a9910>
 In[5]: imgArray = numpy.asarray(imgTran.data, dtype=numpy.float32)
 In[6]: imgArray.shape
Out[6]: (777062,)

I need the original shape (2136, 3216, 3) for it to work with the rest of my code.

jbaiter commented 8 years ago

This library is not intended for this purpose. The JPEG images are not decoded at any point, since the underlying native libraries directly work with the DECT coefficients in the files. To get a numpy array, please use another library that actually decodes the images, like Pillow or wand.