I've noticed that PNG images aren't displaying in Tkinter apps using ImageTk.PhotoImage in OS X Mavericks. But, GIFs and JPEGs display fine. There's no error printed or exception thrown and debugging the code shows the image is read and has the correct height & width. Here's a simplified example:
import Tkinter
from PIL import Image, ImageTk
logo_file = 'test.png'
#logo_file = 'test.gif'
class Application(Tkinter.Frame):
def __init__(self, master):
Tkinter.Frame.__init__(self, master)
self.master.minsize(width=512, height=256)
self.master.config()
self.pack()
self.main_frame = Tkinter.Frame()
self.some_image = ImageTk.PhotoImage(Image.open(logo_file))
some_label = Tkinter.Label(self.main_frame, image=self.some_image)
some_label.config()
some_label.pack(side='top')
self.main_frame.place(in_=self.master, anchor='c', relx=.5, rely=.5)
root = Tkinter.Tk()
app = Application(root)
app.mainloop()
If you use a GIF the image will be displayed, but using a PNG it will not. Again, this is only happening on OS X Mavericks, Mountain Lion works fine. I've tried re-installing (compiling PIL) with no luck, as well as trying a new virtualenv.
Is there perhaps some PNG attribute I need to set correctly when creating/saving the PNG? Or is this a bug in PIL or Tkinter or OS X?
Update to add some details
I'm using:
Python 2.7.5 (/usr/bin/python)
PIL 1.1.7 (compiled using pip)
This is on a machine that was just updated to Mavericks from Mountain Lion, and previously had PIL installed, and I haven't messed with the system Python shipped by Apple.
Update 2 Pillow setup summary
I installed Pillow 2.2.1 and it says it has PNG support:
--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version Pillow 2.2.1
platform darwin 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- TIFF G3/G4 (experimental) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
*** WEBP support not available
*** WEBPMUX support not available
--------------------------------------------------------------------
I also uninstalled and re-installed libpng using brew (libpng 1.5.14). I then re-installed Pillow to make sure it built with it, though I think it uses zlib.
Update 3 trying to build Python 2.7.5
Perhaps the issue is with zlib, trying to compile Python 2.7.5 I get this:
Python build finished, but the necessary bits to build these modules were not found:
_bsddb _sqlite3 _ssl
bsddb185 dbm dl
gdbm imageop linuxaudiodev
nis ossaudiodev readline
spwd sunaudiodev zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
Failed to build these modules:
_tkinter
I think your problem is that your PIL was built without PNG support, or with only partial PNG support, on your Mavericks machine.
If PIL can't find both libpng and libz where it wants to, you won't have complete PNG support. And after upgrading from Mountain Lion to Mavericks, this seems to sometimes be a problem.
This may not be the same problem you're having. For example, it could be down to Apple using a buggy version of Tcl/Tk again (as they infamously did in Snow Leopard). But it's definitely worth trying.
With Pillow, and some versions of old-school PIL (but really, if you're not using Pillow, you almost definitely should be), the end of the install process gives you a friendly "PIL SETUP SUMMARY" section that shows you all the important configuration stuff.
I had the exact same problem with Pillow 2.2.1 on a locally-built Python 3.3.2. I solved it by using Homebrew to install libpng, then rebuilding Pillow:
$ brew install libpng
$ pip-3.3 uninstall pillow
$ pip-3.3 install pillow
I was experiencing the same problem and I did the following. It seems to be fixed.
sudo unistall pillow
xcode-select --install
pip install pillow
It seems to be working great. I also installed all the items below, but it seems you have it already installed
brew install libtiff libjpeg libpng webp littlecms
Did you install Pillow dependencies?
pip uninstall Pillow
brew install libtiff libjpeg webp littlecms
pip install Pillow
You will need HomeBrew to execute the brew command.
I installed Pillow from homebrew (brew install Pillow), all necessary dependencies will be installed automatically. Problem solved.
Related
I need to be able to render text in python in various fonts and using various writing systems that use variable substitution of characters (Arabic, Hindi, Bengali). On a previous machine I had no issue doing this, but I just moved into a new machine with the same conda environment and it doesn't seme to work.
Mac OS 12.6
Python version 3.9.13
Libraqm version 0.9.0 (installed with brew)
Pillow version 9.2.0 (installed with pip)
Here's a minimal reproducible example. It should produce the Urdu word لڑكا – with the two groups of two letters attached – but instead it's producing something that looks like ا ک ڑ ل, with each letter disconnected. It's rendering the specific font correctly, so it's not a matter of the font not being found. When I run the last few lines of code checking for the 'raqm' feature, I get False.
from PIL import ImageFont
from PIL import ImageDraw
from PIL import Image
font = ImageFont.truetype('KawkabMono-Bold.ttf',12,layout_engine=ImageFont.Layout.RAQM)
img = Image.new('1', (200, 100), 'white')
d = ImageDraw.Draw(img)
d.text((100,50), 'لڑكا', fill='black', font=font)
img.save('test.png')
from PIL import features
features.check_feature(feature='raqm')
I've tried installing and reinstalling a few times, and building from different sources. My guess is that libraqm is not on the correct path for Pillow, but I'm not sure how to check or fix this.
I tried running the minimal reproducible text and expected to see a properly render, RTL text with the correct rendering of text. Instead, I got an image file of the text rendered left-to-right and disconnected.
I had the same issue and was able to resolve it by uninstalling Pillow
pip uninstall Pillow
Then reinstalling as follows
pip install --upgrade Pillow --global-option="build_ext" --global-option="--enable-raqm"
There is some more information on the build options here.
This assumes you have already installed libraqm with homebrew
brew install libraqm
I am unsure if this is necessary but you may also need these dependencies as mentioned by Mark Setchell
brew install freetype harfbuzz fribidi
On Mac Os 12.6.1 and Python 3.10.8 System Interpreter I was able to get libraqm to work with Pillow. Configuration follows:
Python Interpreter version 10.8 installed with Homebrew.
Libraqm version 0.9.0 installed with Homebrew.
Pillow version 9.3.0 installed with pip.
First, I tried using a python venv, and I got UserWarning: Raqm layout was requested, but Raqm is not available. Falling back to basic layout.
Next, I added the following to the code you posted: print(features.check("raqm")) and it printed False as expected.
I used my Python System Interpreter (3.10.8), installed Pillow and I was able to get libraqm to load with Pillow.
Image without libraqm. Image with libraqm
I added a fonts directory with the font specified on your snippet above. I loaded the font with the following code:
features.check_feature(feature='raqm')
print(features.check("raqm"))
custom_font = os.path.abspath(
os.path.join(
os.path.dirname(__file__), 'fonts/KawkabMono-Bold.ttf'
)
)
If the librqam library is not loaded by the python interpreter, then your ouput will be like you described it ( individual characters).
I think you additionally need fribidi and harfbuzz installed before PIL/Pillow:
brew install fribidi harfbuzz
Note that you can get PIL/Pillow's configuration, build settings, features and supported formats with:
python3 -m PIL
I'm doing some work on a vagrant box running Ubuntu 13.04 and python 3.3. I've installed Pillow and libjpeg-dev (installing the latter first, as recommended), and when I install (or re-install Pillow, I see this:
--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version Pillow 2.3.0
platform linux 3.3.1 (default, Sep 25 2013, 19:29:01)
[GCC 4.7.3]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
Great! JPEG encoder recognized. Except, when I run Pillow's selftest.py script, it fails a bunch of tests and starts off with this:
--------------------------------------------------------------------
Pillow 2.3.0 TEST SUMMARY
--------------------------------------------------------------------
Python modules loaded from /usr/local/lib/python3.3/dist-packages/PIL
Binary modules loaded from /usr/local/lib/python3.3/dist-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok
*** TKINTER support not installed
*** JPEG support not installed
--- ZLIB (PNG/ZIP) support ok
So now it's not recognizing the JPEG decoder. To make sure this isn't just a problem with selftest.py, I did my testing and sure enough I can make and manipulate .pngs but not .jpgs. I've searched around and tried some suggested solutions, including making a symbolic link to the jpg library, like so:
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
No dice. Anybody have an idea of something else I could try to get the jpeg decoder working? It really isn't practical for me to restrict the workflow entirely to .png.
And, this is why I need to wait 24 hours before posting questions. I needed to pay attention to the Pillow install path. It works if I create the symlink like so:
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/local/lib/
/usr/lib wasn't in the Pillow search path, but the above is.
I ported a web application to another machine, but am having a problem getting django easy thumbnails setup on another team member's machine (mac osx 10.6 snow leopard). I know it requires PIL, but am having problems getting it and perhaps its dependencies set up on the machine.
I read this post detailing how to install libjpeg on a mac, but still did not have luck. Afterwards, I tried to uninstall PIL and instead use Pillow, but still does not work:
(newenv)wsp049614wss:site-packages Admin$ pip uninstall pil
Successfully uninstalled PIL
(newenv)wsp049614wss:site-packages Admin$ pip install pillow
Downloading/unpacking pillow
Downloading Pillow-2.2.1.zip (2.2MB): 2.2MB downloaded
Running setup.py egg_info for package pillow
--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version Pillow 2.2.1
platform darwin 2.7.5 (default, Oct 27 2013, 12:25:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
*** TIFF G3/G4 (experimental) support not available
--- FREETYPE2 support available
*** LITTLECMS support not available
*** WEBP support not available
*** WEBPMUX support not available
--------------------------------------------------------------------
What am I missing? How can I get easy_thumbnails to work? I did not change anything in the codebase, and easy-thumbnails works on other machines. Thanks for any ideas!
Oh no - you have the dreaded libjpeg Snow leopard issue. Basically your standard pip install of PIL doesn't work, it messes up the libjpeg C lib. There are a million blogs on this issue, this one worked for me, but you'll have to hunt around trying out various fixes.
I've been using PSD-TOOLS to great effect on a 13" notebook for half a year now (Python 2.7.3). Just tried to do a fresh install on a Mac running 10.8.4.
I ran through the python setup at http://docs.python-guide.org/en/latest/starting/install/osx/ (Python version
Installed Pil using:
sudo pip install pil
All my Pil scripts work fine.
pip list
shows PIL (1.1.7) installed fine.
Then tried to install psd-tools using:
sudo pip install psd-tools
Appears to install fine but when I run my PSD scripts, get the following:
File "/Library/Python/2.7/site-packages/psd_tools/user_api/psd_image.py", line 91, in as_PIL
return self._psd._layer_as_PIL(self._index)
File "/Library/Python/2.7/site-packages/psd_tools/user_api/psd_image.py", line 243, in _layer_as_PIL
return pil_support.extract_layer_image(self.decoded_data, index)
File "/Library/Python/2.7/site-packages/psd_tools/user_api/pil_support.py", line 32, in extract_layer_image
decoded_data.header.depth, get_icc_profile(decoded_data))
File "/Library/Python/2.7/site-packages/psd_tools/user_api/pil_support.py", line 68, in _channels_data_to_PIL
raise Exception("This module requires PIL (or Pillow) installed.")
Exception: This module requires PIL (or Pillow) installed.
Anybody run into this issue?
I ran into this issue on OSX 10.9 with Python 2.7.5. (In a virtualenv)
I ended up going over https://pypi.python.org/pypi/Pillow/2.2.1 and installing some optional prerequisites for Pillow.
#I already had libjpeg but this is the line in their doc.
brew install libtiff libjpeg webp littlecms
#Then re-installed Pillow
pip install --force-reinstall --upgrade pillow
The result was the following features enabled
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- TIFF G3/G4 (experimental) support available
*** FREETYPE2 support not available
--- LITTLECMS support available
--- WEBP support available
--- WEBPMUX support available
And my small python test ran
from PIL import Image
from psd_tools import PSDImage
psd = PSDImage.load('test_data/1.psd')
merged_image = psd.as_PIL()
Not sure which of those requirements was responsible so if you are not interested in installing extra libraries you may have to do 1 at a time or dig into the source.
I am at wit's end. I have a working install of python 2.6.5 with numpy and scipy. I want to use it to do some simple PCA which requires importing images. Well, I figured PIL was the way to go for this. So, following a guide, I downloaded and installed libjpeg6-b. I then used the following commands
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
cp /usr/share/libtool/config/config.sub .
cp /usr/share/libtool/config/config.guess .
./configure --enable-shared --enable-static
make
I moved over to where I downloaded PIL 1.1.7 and did the following:
tar zxvf Imaging-1.1.7.tar.gz
cd Imaging-1.1.7
(edit the setup.py file to find libjpeg)
python setup.py build
python setup.py install
I then try to import _imaging and I get the famous ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL-1.1.7-py2.6-macosx-10.3-fat.egg/_imaging.so, 2): Symbol not found: _jpeg_resync_to_restart
Referenced from: /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL-1.1.7-py2.6-macosx-10.3-fat.egg/_imaging.so
Expected in: dynamic lookup error.
I tried most/all of the solutions out there already and haven't found much success. I ran otool on my _imaging.so after I restricted my architecture to i386 and got:
Thomas$ otool -L /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/_imaging.so:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
Furthermore, when I ran pip and got this output
--------------------------------------------------------------------
PIL 1.1.6 BUILD SUMMARY
--------------------------------------------------------------------
version 1.1.6
platform darwin 2.6.5 (r265:79359, Mar 24 2010, 01:32:55)
[GCC 4.0.1 (Apple Inc. build 5493)]
--------------------------------------------------------------------
--- TKINTER support ok
--- JPEG support ok
--- ZLIB (PNG/ZIP) support ok
--- FREETYPE2 support ok
--------------------------------------------------------------------
To check the build, run the selftest.py script.
for PIL 1.1.6.
I have tried switching to gcc 4.0 and compiling both libjpeg and PIL also.
Any help would be very much appreciated. Also, if you need any more information, please do not hesitate to ask.
Do you know Macports (or Fink)? The easiest way to install software and packages is via Macports. Alternatively you could have a look at the Portfiles of Macports and see how they are compiling those libs.
You can also use pip to install imaging
user easy_install to install pip
easy_install pip
pip install http://effbot.org/downloads/Imaging-1.1.6.tar.gz
Alternatively if this does not help you, I wrote an article on how to get PIL, libjpeg, _imaging to work with python 2.6 and snow leopard
http://appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-_jpeg_resync_to_restart/