Reputation: 333
I am trying to implement image compression for my web application. While the compression works fine for PNG images (actual compression is negligable but no error) it fails for JPG and JPEG images.
Running the Pillow tests results in the following output:
Pillow 4.1.0 TEST SUMMARY
--------------------------------------------------------------------
Python modules loaded from /usr/local/lib/python2.7/dist-packages/PIL
Binary modules loaded from /usr/local/lib/python2.7/dist-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok
*** TKINTER support not installed
--- FREETYPE2 support ok
*** LITTLECMS2 support not installed
*** WEBP support not installed
--- JPEG support ok
*** OPENJPEG (JPEG2000) support not installed
--- ZLIB (PNG/ZIP) support ok
*** LIBTIFF support not installed
--------------------------------------------------------------------
Running selftest:
--- 58 tests passed.
It all seems OK but when running my sample code I get the following error:
Wrong JPEG library version: library is 62, caller expects 90
Traceback (most recent call last):
File "test.py", line 10, in <module>
im.save("compressed.jpeg", format="JPEG", quality=90, optimize=True)
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1675, in save
save_handler(self, fp, filename)
File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 708, in _save
ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 486, in _save
raise IOError("encoder error %d when writing image file" % s)
IOError: encoder error -2 when writing image file
I have checked online for Jessie libjpeg packages and 62 seems to be the latest and at this point I am not sure if what I am trying to do is even possible. While this problem seems to be quite common I have failed to find a solution to it.
The code that I am trying to run is as follows:
from PIL import Image, ImageFile
import sys
ImageFile.LOAD_TRUNCATED_IMAGES = True
im = Image.open("original.jpg")
im.save("compressed.jpg", format="JPEG", quality=90, optimize=True)
print "Done ..."
If I try to convert it as:
from PIL import Image, ImageFile
import sys
ImageFile.LOAD_TRUNCATED_IMAGES = True
im = Image.open("original.jpg")
im.save("compressed.png", format="PNG", quality=90, optimize=True)
print "Done ..."
it doesn't produce an error but just makes the image black.
Running both versions of the code on Windows10 yields propper results (image nicely compressed with very little quality loss)
Sytem specs:
Any help would be greatly appreciated.
Upvotes: 5
Views: 1992
Reputation: 11
Not sure if this will help, but I was having a similar issue. The only thing that worked for me was forcing pip to build pillow from source after installing the dev version of the needed libraries (my code was editing a jpg and adding a label using a custom font). This was on a ARM based embedded device running Ubuntu Linux using Python 3.7.3
apt-get install -y libjpeg-dev libfreetype6-dev
pip3 install pillow --global-option="build_ext" --global-option="--enable-jpeg" --global-option="--enable-freetype"
Upvotes: 1