Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pillow built with libraqm insists it has no libraqm #4859

Closed
newfeatureengineer opened this issue Aug 13, 2020 · 55 comments
Closed

Pillow built with libraqm insists it has no libraqm #4859

newfeatureengineer opened this issue Aug 13, 2020 · 55 comments

Comments

@newfeatureengineer
Copy link

What did you do?

Built pillow from source with all dependencies and installed it using python setup.py install.

Running selftest.py works, and seems to corroborate that raqm is indeed installed.

What did you expect to happen?

I expected to be able to set text direction

What actually happened?

I got the following exception:

  File "C:\venv\lib\site-packages\pillow-7.2.0-py3.7-win-amd64.egg\PIL\ImageFont.py", line 262, in getsize
KeyError: 'setting text direction, language or font features is not supported without libraqm'

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.7.7
  • Pillow: 7.2.0
from PIL import ImageFont
font = ImageFont.truetype(r'C:\Windows\Fonts\Arial.ttf', 120)
size = font.getsize("hello world", 'rtl')
@radarhere radarhere changed the title pillow built from source to include libraqm still insists it has no libraqm. pillow built from source to include libraqm still insists it has no libraqm Aug 13, 2020
@radarhere
Copy link
Member

Hi. Would you mind trying this?

from PIL import ImageFont
font = ImageFont.truetype(r'C:\Windows\Fonts\Arial.ttf', 120, layout_engine=ImageFont.LAYOUT_RAQM)
size = font.getsize("hello world", 'rtl')

@radarhere radarhere changed the title pillow built from source to include libraqm still insists it has no libraqm Pillow built with libraqm insists it has no libraqm Aug 14, 2020
@nulano
Copy link
Contributor

nulano commented Aug 29, 2020

"Pillow built with Raqm" doesn't actually mean anything in the current version; Raqm is only linked at runtime. Raqm support can be loaded if Pillow is compiled with FreeType and libraqm.dll is found by the system functions.

In other words, to load Raqm you need to have libraqm.dll available in your PATH, the site-packages\PIL directory, or the Python directory (in decreasing order of good practice, but increasing order of convenience 😄 ).

@newfeatureengineer
Copy link
Author

@nulano Placing it in site-packages\PIL didn't work, but placing it in my root project directory (available in my PATH), it did.

Thanks :)

Is there a way to statically link libraqm to avoid the dll issue?

@nulano
Copy link
Contributor

nulano commented Sep 1, 2020

Is there a way to statically link libraqm to avoid the dll issue?

Not currently, but it shouldn't be too difficult to make a patch.

First you have to add #include <raqm.h> to src/_imagingft.c, then replace the setraqm function with code that links each required function statically, e.g. replace

    p_raqm.version_string = (t_raqm_version_string)GetProcAddress(p_raqm.raqm, "raqm_version_string");

with

    p_raqm.version_string = raqm_version_string;

and finally add raqm to setup.py; I think the following (untested) patch should be sufficient:

        features = [
            "zlib",
            "jpeg",
            "tiff",
            "freetype",
            "lcms",
            "webp",
            "webpmux",
            "jpeg2000",
            "imagequant",
            "xcb",
+           "raqm",
        ]
# -----
        if feature.want("xcb"):
            _dbg("Looking for xcb")
            if _find_include_file(self, "xcb/xcb.h"):
                if _find_library_file(self, "xcb"):
                    feature.xcb = "xcb"

+       if feature.want("raqm"):
+           _dbg("Looking for raqm")
+           if _find_include_file(self, "raqm.h"):
+               if _find_library_file(self, "libraqm"):
+                   feature.raqm = "libraqm"
# -----
        if feature.xcb:
            libs.append(feature.xcb)
            defs.append(("HAVE_XCB", None))
+       if feature.raqm:
+           libs.append(feature.raqm)
+           defs.append(("HAVE_RAQM", None))
        if sys.platform == "win32":

The reason this is not currently supported is that Raqm depends on FriBiDi which has an LGPL license. The current runtime linking mechanism allows Pillow to avoid any licensing issues, while linking statically could add LGPL requirements to using Pillow.

@radarhere
Copy link
Member

Is there anything left to do for this issue @newfeatureengineer ? It looks like it's been resolved to me.

@h4xw4r
Copy link

h4xw4r commented Jul 2, 2021

First you have to add #include <raqm.h> to src/_imagingft.c,

Where can I find the _imagingft.c please ?
I placed libraqm.dll everywhere possible with no luck.

edit: typo

@nulano
Copy link
Contributor

nulano commented Jul 2, 2021

The changes from my comment above are now the default for source builds on Linux since Pillow 8.2.0. To enable Raqm for binary wheels, it is sufficient to install FriBiDi on Linux (Raqm is linked statically but runtime loading is used for its dependency FriBiDi in wheels).

For Windows wheels (source builds are not recommended) you should be able to enable Raqm by installing fribidi.dll into a system or Python directory, but I'm not sure this was enabled for the release. Installing from source on Windows is not recommended, but the instructions are here: https://github.com/python-pillow/Pillow/blob/master/winbuild/build.rst

@h4xw4r
Copy link

h4xw4r commented Jul 2, 2021

I have used it on Linux without any problems. Sadly the problem still exists on Windows and I'm not the only one.
checkout this post:
https://stackoverflow.com/questions/67805726/pillow-not-detecting-the-libraqm-dll-file/68183126#68183126
I couldn't find online libfribidi.dll is it a different library or just renaming fribidi.dll will do ?

@h4xw4r
Copy link

h4xw4r commented Jul 2, 2021

@radarhere Should I make a new Issue or this one can be changed from Closed to Open ?

@nulano
Copy link
Contributor

nulano commented Jul 2, 2021

I couldn't find online libfribidi.dll is it a different library or just renaming fribidi.dll will do ?

Actually, that's a typo. When trying to load Raqm's dependency FriBiDi, Pillow will attempt to open fribidi.dll, fribidi-0.dll or libfribidi-0.dll in that order. These are all different names for the same library, FriBiDi.

@h4xw4r
Copy link

h4xw4r commented Jul 2, 2021

I went out and tried this on Manjaro Linux with Wine set as Windows 10 and Windows 8.1. Still no luck getting it to work.

@nulano
Copy link
Contributor

nulano commented Jul 2, 2021

The Python 3.7 Pillow 8.3.0 release wheel doesn't detect Raqm/FriBiDi for me either, but the CI test run wheel does (from https://github.com/python-pillow/Pillow/actions/runs/988696864).

@radarhere radarhere reopened this Jul 3, 2021
@radarhere
Copy link
Member

@h4xw4r here is one of the wheels linked to from the previous comment. Installing this might solve your immediate problem - Pillow-8.3.0-cp37-cp37m-win_amd64.whl.zip

@nulano does the 8.2.0 release wheel work? Is this possibly just the same problem as #5573?

@maaldheefee
Copy link

Same issue with 8.2.0 also. I've tried a week ago and had to use WSL after trying every other option.

The wheel from @nulano's link worked finally.

@nulano
Copy link
Contributor

nulano commented Jul 4, 2021

@nulano does the 8.2.0 release wheel work? Is this possibly just the same problem as #5573?

8.2.0 had the same issue when I tested it right after the release.

The problem and fix is #5413 (comment)

@h4xw4r
Copy link

h4xw4r commented Jul 6, 2021

@h4xw4r here is one of the wheels linked to from the previous comment. Installing this might solve your immediate problem - Pillow-8.3.0-cp37-cp37m-win_amd64.whl.zip

It doesn't seem to work for me
pip install Pillow-8.3.0-cp37-cp37m-win_amd64.whl
spits out:
ERROR: Pillow-8.3.0-cp37-cp37m-win_amd64.whl is not a supported wheel on this platform

@nulano
Copy link
Contributor

nulano commented Jul 6, 2021

It doesn't seem to work for me ... is not a supported wheel on this platform

That wheel is for Python 3.7 64-bit, you are probably running a different version of Python.

I have checked the 8.3.1 wheel from cgohlke's website also works with Raqm/FriBiDi properly (the official release should be uploaded to PyPI later today I think, you can subscribe to #5578 to be notified).

@h4xw4r
Copy link

h4xw4r commented Jul 6, 2021

Thanks I just noticed the cp37-cp37m, stupid me.

subscribe to #5578 to be notified

Neat I will be waiting for it 👍

@radarhere
Copy link
Member

Pillow 8.3.1 has now been released and uploaded to PyPI.

@h4xw4r
Copy link

h4xw4r commented Jul 7, 2021

I installed Pillow 8.3.1 on my work computer and placed the DLL files at the folder of Python.exe it didn't work.
However, on my personal computer everything worked fine maybe because I placed the DLL files everywhere possible. I think it will be worth it to put the right way of doing this on Windows in the documentation.

@nulano
Copy link
Contributor

nulano commented Jul 7, 2021

I installed Pillow 8.3.1 on my work computer and placed the DLL files at the folder of Python.exe it didn't work.

This works for me. The installation docs link to this Microsoft Docs page in the paragraph for Raqm, which lists the application directory as the first checked option (i.e. next to Python.exe). Did you perhaps install a 32-bit fribidi.dll for a 64-bit Python or vice-versa?

@h4xw4r
Copy link

h4xw4r commented Jul 9, 2021

Did you perhaps install a 32-bit fribidi.dll for a 64-bit Python or vice-versa?

I don't think I did that wrong. However, just to be sure, I made a virtual environment with python and put the DLL files next Python.exe and it still didn't work.

@nulano
Copy link
Contributor

nulano commented Jul 9, 2021

Did you perhaps install a 32-bit fribidi.dll for a 64-bit Python or vice-versa?

I don't think I did that wrong. However, just to be sure, I made a virtual environment with python and put the DLL files next Python.exe and it still didn't work.

I haven't tried it, but I'm pretty sure you can't put the DLL next to python.exe in a virtual environment as the python.exe is just a link to the system directory of the Python install. See sys.executable, I'm pretty sure the DLL must be in the same directory, or one of the other options specified by Microsoft. The other options are the system and Windows directories, any directory in the PATH variable, or the current working directory at the time PIL.ImageFont is first imported (the search order varies by Python version, see the link in my previous comment for the two orders).

@monzzzz
Copy link

monzzzz commented May 24, 2024

Hello, I have this problem too on Mac. Can someone help me, please? I'm using Pillow version 9.5.0 because the newest version doesn't support some specific functions I need to use.

@radarhere
Copy link
Member

How did you install Pillow? What is the output of python3 -m PIL?

Have you tried installing fribidi?

https://pillow.readthedocs.io/en/stable/installation/basic-installation.html

Raqm support requires FriBiDi to be installed separately

@monzzzz
Copy link

monzzzz commented May 24, 2024

This is what was displayed when I ran python3 -m PIL

*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.4.0
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 3.0.3
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.12
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok

and this is what I fused and installed

Pillow-10.0.0-cp312-cp312-macosx_11_0_universal2.whl

@nulano
Copy link
Contributor

nulano commented May 24, 2024

--- RAQM (Bidirectional Text) support ok, loaded 0.10.1

This means raqm is working for you.

@monzzzz
Copy link

monzzzz commented May 24, 2024

Ohh, but when I tried to run template.py in Synthtiger library, I still got 'setting text direction, language or font features is not supported without libraqm'

@monzzzz
Copy link

monzzzz commented May 24, 2024

Now, I think the problem is that I can't use the function getsize after Pillow version 9.2.0. Do you know if there is any way I can fix this or do I have to manually change to getbbox?

@monzzzz
Copy link

monzzzz commented May 25, 2024

My PIL library features contain raqm, but the core object in the C file of the library doesn't have raqm

@radarhere
Copy link
Member

Now, I think the problem is that I can't use the function getsize after Pillow version 9.2.0.

Technically, it's only deprecated after 9.2.0. It was removed in 10.0.0.

Do you know if there is any way I can fix this or do I have to manually change to getbbox?

You will have to change your code to use getbbox(). You can read about how to do this at https://pillow.readthedocs.io/en/stable/deprecations.html#font-size-and-offset-methods

@radarhere
Copy link
Member

My PIL library features contain raqm, but the core object in the C file of the library doesn't have raqm

Considering that python3 -m PIL uses

>>> from PIL import _imagingft
>>> _imagingft.HAVE_RAQM
True

to check for raqm, what you're describing sounds unusual. Could you use code to depict what you're seeing?

@monzzzz
Copy link

monzzzz commented May 25, 2024

I tried it. I got False

@radarhere
Copy link
Member

You previously reported the output of python3 -m PIL. When you're running your code, what command are you using? Are you running python3, or something else? We occasionally have people running two different versions of Python.

@monzzzz
Copy link

monzzzz commented May 25, 2024

Wait. Sorry, I actually got true

@radarhere
Copy link
Member

the core object in the C file of the library doesn't have raqm

How do you know this?

@monzzzz
Copy link

monzzzz commented May 25, 2024

I edited in PIL/ImageFont.py file in my Python environment by adding:

if (core.HAVE_RAQM):
  print(True)
else:
  print(False)

, and it gave me False when I ran it

@radarhere
Copy link
Member

core in ImageFont is

from . import _imagingft as core

So you're reporting that

from PIL import _imagingft
print(_imagingft.HAVE_RAQM)

is true, but

from PIL import _imagingft as core
print(core.HAVE_RAQM)

is false.

This doesn't make sense?

@monzzzz
Copy link

monzzzz commented May 25, 2024

Since I used this command to run synthtiger -o results -w 4 -v examples/synthtiger/template.py SynthTiger config.yaml, I'm not sure if there is any issue associated with it. When I directly ran the checking file by using the python3 command, there was no problem, and it printed True while when I used this command, it gave me False.

@radarhere
Copy link
Member

radarhere commented May 25, 2024

Ok, so it does sound like you're using a different version of Python for synthtiger.

Since you're open to modifying Pillow code, could you edit ImageFont.py, insert import sys; print(sys.executable), and then run synthtiger.

That should print something like '/Applications/Xcode.app/Contents/Developer/usr/bin/python3'

Then, using the path that was printed, run /Applications/Xcode.app/Contents/Developer/usr/bin/python3 -m PIL. Could you report to us what command you ran, and the output? That should tell us the status of Pillow within this other Python installation.

@monzzzz
Copy link

monzzzz commented May 25, 2024

I got this /opt/anaconda3/bin/python which means that synthtiger ran on the anaconda not my virtual env, right?, and I also got this /opt/anaconda3/bin/python: No module named Pillow

@radarhere
Copy link
Member

Yes, nothing in that path says 'venv', so it wouldn't be inside your virtual environment.

It's not clear to me why synthtiger was complaining about raqm, and not about Pillow not being installed in the first place. When you've been modifying ImageFont.py, and then running synthtiger, where is ImageFont.py located?

I don't know if your goal is to run synthtiger inside your virtual environment though. If so, there's no point to continuing to debug this other installation.

@nulano
Copy link
Contributor

nulano commented May 25, 2024

IIRC sys.executable can be unreliable to detect virtual environments, maybe check also sys.prefix?

@monzzzz
Copy link

monzzzz commented May 25, 2024

It's not clear to me why synthtiger was complaining about raqm, and not about Pillow not being installed in the first place

Now, I installed Pillow, and when I ran /opt/anaconda3/bin/python, I got

--- PIL CORE support ok, compiled for 10.3.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.12
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for 9.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.4.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.1
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed

Is there any way that I can install raqm in conda env?

@monzzzz
Copy link

monzzzz commented May 25, 2024

IIRC sys.executable can be unreliable to detect virtual environments, maybe check also sys.prefix?

I also got /opt/anaconda3

@nulano
Copy link
Contributor

nulano commented May 25, 2024

Which version of the Pillow package did you install (default, conda-forge, something else)? I think conda list should show it, but not sure.

@monzzzz
Copy link

monzzzz commented May 25, 2024

I got this after running conda list pillow

# Name                    Version                   Build  Channel
pillow                    9.5.0                    pypi_0    pypi

@nulano
Copy link
Contributor

nulano commented May 25, 2024

--- PIL CORE support ok, compiled for 10.3.0

pillow 9.5.0 pypi_0 pypi

This makes no sense, you can't have both 10.3.0 and 9.5.0 installed at the same time.
Can you also provide the start of the output of python -m PIL (i.e. the part listing various paths)?

@monzzzz
Copy link

monzzzz commented May 25, 2024

If I run from base env, I got this

Pillow 10.3.0
Python 3.11.7 (main, Dec 15 2023, 12:09:04) [Clang 14.0.6 ]
--------------------------------------------------------------------
Python executable is /opt/anaconda3/bin/python
System Python files loaded from /opt/anaconda3
--------------------------------------------------------------------
Python Pillow modules loaded from /opt/anaconda3/lib/python3.11/site-packages/PIL
Binary Pillow modules loaded from /opt/anaconda3/lib/python3.11/site-packages/PIL

I'm not sure if they are in a different environment.

@monzzzz
Copy link

monzzzz commented May 25, 2024

I just switched to the environment in which Pillow 10.3.0 is installed. When I run the synthtiger command, I got the getsize error instead of raqm

width = font.getsize(text, direction=direction)[0]
            ^^^^^^^^^^^^
AttributeError: 'FreeTypeFont' object has no attribute 'getsize'

Does this mean I have to replace getsize by getbbox in every function in the synthtiger library?

@radarhere
Copy link
Member

If synthtiger is where that line of code resides, then yes.

clovaai/synthtiger#66 is the open issue in synthtiger for fixing this properly.

@monzzzz
Copy link

monzzzz commented May 26, 2024

I got it. Thank you so much for your help!!!!!!

@python-pillow python-pillow locked as resolved and limited conversation to collaborators May 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants