Pillow not recognizing Libraqm installation on Mac OS - python

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

Related

PyOpenGL-3.1.5-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform

For a Python project I need PyOpenGL. I have installed it with the PyCharm IDE.
When I ran an OpenGL program to test the installation, the following error message came up:
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling.
I searched in the www for possible solutions and I've found some at StackOverflow. I tried all of them and none worked.
Then I've found that blog here:
https://block.arch.ethz.ch/blog/2016/10/pyopengl-glut-error/
I followed all instructions and got the following error-message in PyCharm IDE:
(virtual-environment) C:\Users\Rainer\PycharmProjects\MatchMover>pip install C:\Users\Rainer\Desktop\PyOpenGL-3.1.5-cp36-cp36m-win_amd64.whl
PyOpenGL-3.1.5-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.
You are using pip version 9.0.1, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
The pip package has already been updated to the version 20.0.2 manually, but is not in sync with PyCharm IDE.
I'm using a Windows 7 computer, Python 3.6.3, PyOpenGL-3.1.5-cp36-cp36m-win_amd64.whl and as OpenGL-library I use freeglut.
Here is the test program (taken from: https://codeloop.org/python-opengl-programming-creating-window/):
main.py
from OpenGL.GL import *
from OpenGL.GLUT import *
width, height = 500, 400
def draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glutSwapBuffers()
glutInit()
glutInitDisplayMode(GLUT_RGBA )
glutInitWindowSize(width, height)
glutInitWindowPosition(200, 200)
window = glutCreateWindow("Opengl Window In Python")
glutDisplayFunc(draw)
glutIdleFunc(draw)
glutMainLoop()
Two common sources of this error are that…
the package expects a different system type (32-bit vs 64-bit).
system doesn’t have or can’t identify the necessary version of Python i.e. someone only have a 3.x version installed, but the package requires Python 2.7.
But as you stated that, you have python 3.6 so I will put solution for first option.
The fix is either to download the other version (32-bit if you downloaded 64-bit and vice versa) or change the wheel’s file name if you know you need the version you have.

open a .jpg image in python using matplotlib.image in python 3.6

I am trying to open a JPG image using matplotlib in Python. Editor 'Spyder', Python3.6, WIndows 7
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Read in the image and print some stats
image = mpimg.imread(r'C:\Users\xxx\Python Code\mountain.jpg')
print('This image is: ',type(image),
'with dimensions:', image.shape)
But I am getting the following error... It says that except '.png' no other image format is supported.
Error :--
image = mpimg.imread(r'C:\Users\xxx\Python Code\mountain.jpg')
File "C:\temp\Continuum\anaconda3\lib\site-packages\matplotlib\image.py",
line 1284, in imread
'more images' % list(handlers))
ValueError: Only know how to handle extensions: ['png']; with Pillow
installed matplotlib can handle more images.
I went through various documentations. Which says that, in order to open a '.jpg' image, 'Pillow' must be installed.
If native matplotlib call fails to open a image then it automatically falls back on 'pillow'. (correct me if I am wrong)
So I installed 'Pillow'. But I am still getting the error.
Can you tell me what am I Missing ? (Strange thing is this same code is running in another computer. I have no way to verify what library is installed in that machine)
Matplotlib requires PIL(Python Imaging Library) to work with .jpg format. To use it you need to install Pillow (which is the fork of PIL).
Installation Using PIP
pip install pillow
or
pip3 install pillow
Installation Using Conda
conda install pillow
You have to install PIL. Make sure you are using anaconda python distro. Go to this link or write this command to install PIL directly
conda install -c anaconda pillow

After a clean install of OpenCV via pip it throws an ImportError: DLL load failed

After installing OpenCV via pip on Windows 10 with:
pip install opencv-python
I can not import the module. When executing the command:
import cv2
I get the error:
File "C:\ProgramData\Anaconda3\lib\site-packages\cv2__init__.py",
line 7, in
from . import cv2
ImportError: DLL load failed:...
If I look into the file throwing the code, it looks like the following:
import sys
import os
# FFMPEG dll is not found on Windows without this
os.environ["PATH"] += os.pathsep + os.path.dirname(os.path.realpath(__file__))
from . import cv2
sys.modules['cv2'] = cv2
So I guess it is ffmpeg which is missing. Thus I installed ffmpeg like described here: http://www.wikihow.com/Install-FFmpeg-on-Windows
Thus, ffmpeg is in my path. However, the error message still occurs. I also tried to install the ffmpeg via pip with
pip install ffmpeg-normalize
But this did not help either.
The opencv-python Windows packages ship with FFmpeg by default. You can have a look at C:\ProgramData\Anaconda3\lib\site-packages\cv2 and you should find FFmpeg DLL there. You don't have to install it separately.
The real problem lies most probably in Anaconda because they are not shipping python3.dll with their distribution. This is required by PEP 384. Related Anaconda issue is here: https://github.com/ContinuumIO/anaconda-issues/issues/1394
To fix this, you will have to copy python3.dll from a CPython installer package and place it to PATH. The CPython version must match your Anaconda version. Easiest way is to copy the file to some place which is already in PATH. This could be for example C:\Anaconda3 if that's where your Anaconda installation is located.
If the above does not work, make sure that you have Visual C++ redistributable 2015 installed: https://www.microsoft.com/en-us/download/details.aspx?id=48145

Pyinstaller troubles with Pillow

I'm trying to use pyinstaller, on OSX Mavericks, with one a Python script. Pyinstaller compiles and packs along until it finds an error with the Pillow library.
ImportError: dlopen(/Users/Rodolphe/.python-eggs/Pillow-2.2.1-py2.7-macosx-10.9-intel.egg tmp/PIL/_imaging.so, 2): Library not loaded: /usr/local/lib/libjpeg.8.dylib
Referenced from: /Users/Rodolphe/.python-eggs/Pillow-2.2.1-py2.7-macosx-10.9-intel.egg-tmp/PIL/_imaging.so
Reason: Incompatible library version: _imaging.so requires version 13.0.0 or later, but libjpeg.8.dylib provides version 9.0.0
It looks like Pyinstaller and Pillow are not getting along (it seems to be a known fact: http://www.pyinstaller.org/ticket/745 ). Is there a way around it? Should I try uninstalling Pillow and installing Pil instead (I chose Pillow out of discouragement from Pil's reluctancy to install on Mavericks, to begin with)? Or maybe update libjpeg.8.dylib to version 13.0.0 as it seems to be the problem?
[EDIT]
I have updated libjepeg thanks to the advice from #mimi.vx I now have another error at the very end of the process:
IOError: [Errno 20] Not a directory: '/Library/Python/2.7/site-packages/Pillow-2.2.1-py2.7-macosx-10.9-intel.egg/PIL/_imaging.so'
I tried reinstalling Pillow so Pyinstaller would maybe get the directory, but no luck yet...
[RE-EDIT]
Using py2app instead of pyinstaller worked for me!
Yes, first try update libjpeg
Good help for OS X can be found in this blog http://brantsteen.com/blog/python-27-libjpeg-pil-on-osx/
libjpg latest source is here www.ijg.org
And Pillow recommended install on OS X is described in pypi.python.org/pypi/Pillow/2.2.1
It is over homebrew $ brew install libtiff libjpeg webp littlecms
Pyinstaller was behaving strangely while importing PIL. I tried using py2app ( http://pythonhosted.org/py2app/ ) instead, and it worked like a charm. No lib ray import trouble or anything. I'd recommend it.

PNG display in PIL broken on OS X Mavericks?

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.

Categories

Resources