Repository: kivy/python-for-android

Issue:
Upgrade Pillow recipe to a version newer than 8.4.0
Please upgrade the Pillow recipe to a version newer than 8.4.0.
While trying out two different versions (9.5.0 and 10.3.0) I'm getting following error:
```
.buildozer/android/platform/python-for-android/pythonforandroid/recipes/Pillow/patches/fix-setup.patch

  STDOUT:
patching file setup.py
Hunk #1 succeeded at 124 (offset -1 lines).
Hunk #2 FAILED at 411.
patch unexpectedly ends in middle of line
Hunk #3 FAILED at 471.
2 out of 3 hunks FAILED -- saving rejects to file setup.py.rej
```

That's when having `Pillow==9.5.0` or `Pillow==10.3.0` in the requirements.

The problem with the `8.4.0` is that it's old and obsolete. There are problems with BMP files which is rendering black images (fixed on version 9.x..). It's probably doing the same with other formats other than Jpeg and PNG. With `8.4.0` I'm also getting errors that `webp` isn't included.

I hope this problem will be solved as soon as possible!


Discussion:
Hi,

You need to analyze how the patchfile interacts with 8.4.0 and then create
a new patch for the version you want to try:
https://github.com/kivy/python-for-android/blob/develop/pythonforandroid/recipes/Pillow/patches/fix-setup.patch

You then need to make sure that using a new version of Pillow doesn't break
any of the packages depending on it:

matplotlib/__init__.py:    depends = ['kiwisolver', 'numpy', 'pillow',
'setuptools', 'freetype']

Regards,

Juan


On Thu, May 23, 2024 at 6:52 AM Mathias Lindström ***@***.***>
wrote:

> Please upgrade the Pillow recipe to a version newer than 8.4.0.
> While trying out two different versions (9.5.0 and 10.3.0) I'm getting
> following error:
>
> .buildozer/android/platform/python-for-android/pythonforandroid/recipes/Pillow/patches/fix-setup.patch
>
>   STDOUT:
> patching file setup.py
> Hunk #1 succeeded at 124 (offset -1 lines).
> Hunk #2 FAILED at 411.
> patch unexpectedly ends in middle of line
> Hunk #3 FAILED at 471.
> 2 out of 3 hunks FAILED -- saving rejects to file setup.py.rej
>
> That's when having Pillow==9.5.0 or Pillow==10.3.0 in the requirements.
>
> The problem with the 8.4.0 is that it's old and obsolete. There are
> problems with BMP files which is rendering black images (fixed on version
> 9.x..). It's probably doing the same with other formats other than Jpeg and
> PNG. With 8.4.0 I'm also getting errors that webp isn't included.
>
> I hope this problem will be solved as soon as possible!
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/kivy/python-for-android/issues/3019>, or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAS6BJOROW2VXDI2IDB6TFLZDXJ7TAVCNFSM6AAAAABIFPRAYSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGMYTENZSGAYDCNI>
> .
> You are receiving this because you are subscribed to this thread.Message
> ID: ***@***.***>
>


Resolving patch:
diff --git a/pythonforandroid/recipes/Pillow/__init__.py b/pythonforandroid/recipes/Pillow/__init__.py
--- a/pythonforandroid/recipes/Pillow/__init__.py
+++ b/pythonforandroid/recipes/Pillow/__init__.py
@@ -1,9 +1,9 @@
 from os.path import join
 
-from pythonforandroid.recipe import CompiledComponentsPythonRecipe
+from pythonforandroid.recipe import PyProjectRecipe
 
 
-class PillowRecipe(CompiledComponentsPythonRecipe):
+class PillowRecipe(PyProjectRecipe):
     """
     A recipe for Pillow (previously known as Pil).
 
@@ -23,67 +23,42 @@ class PillowRecipe(CompiledComponentsPythonRecipe):
         - libwebp: library to encode and decode images in WebP format.
     """
 
-    version = '8.4.0'
+    version = '10.3.0'
     url = 'https://github.com/python-pillow/Pillow/archive/{version}.tar.gz'
     site_packages_name = 'PIL'
+    patches = ["setup.py.patch"]
     depends = ['png', 'jpeg', 'freetype', 'setuptools']
     opt_depends = ['libwebp']
-    patches = [join('patches', 'fix-setup.patch')]
 
-    call_hostpython_via_targetpython = False
-
-    def get_recipe_env(self, arch=None, with_flags_in_cc=True):
-        env = super().get_recipe_env(arch, with_flags_in_cc)
-
-        png = self.get_recipe('png', self.ctx)
-        png_lib_dir = join(png.get_build_dir(arch.arch), '.libs')
-        png_inc_dir = png.get_build_dir(arch)
+    def get_recipe_env(self, arch, **kwargs):
+        env = super().get_recipe_env(arch, **kwargs)
 
         jpeg = self.get_recipe('jpeg', self.ctx)
         jpeg_inc_dir = jpeg_lib_dir = jpeg.get_build_dir(arch.arch)
+        env["JPEG_ROOT"] = "{}:{}".format(jpeg_lib_dir, jpeg_inc_dir)
 
         freetype = self.get_recipe('freetype', self.ctx)
         free_lib_dir = join(freetype.get_build_dir(arch.arch), 'objs', '.libs')
         free_inc_dir = join(freetype.get_build_dir(arch.arch), 'include')
+        env["FREETYPE_ROOT"] = "{}:{}".format(free_lib_dir, free_inc_dir)
 
         # harfbuzz is a direct dependency of freetype and we need the proper
         # flags to successfully build the Pillow recipe, so we add them here.
         harfbuzz = self.get_recipe('harfbuzz', self.ctx)
         harf_lib_dir = join(harfbuzz.get_build_dir(arch.arch), 'src', '.libs')
         harf_inc_dir = harfbuzz.get_build_dir(arch.arch)
+        env["HARFBUZZ_ROOT"] = "{}:{}".format(harf_lib_dir, harf_inc_dir)
+
+        env["ZLIB_ROOT"] = f"{arch.ndk_lib_dir_versioned}:{self.ctx.ndk.sysroot_include_dir}"
 
         # libwebp is an optional dependency, so we add the
         # flags if we have it in our `ctx.recipe_build_order`
-        build_with_webp_support = 'libwebp' in self.ctx.recipe_build_order
-        if build_with_webp_support:
+        if 'libwebp' in self.ctx.recipe_build_order:
             webp = self.get_recipe('libwebp', self.ctx)
             webp_install = join(
                 webp.get_build_dir(arch.arch), 'installation'
             )
-
-        # Add libraries includes to CFLAGS
-        cflags = f' -I{png_inc_dir}'
-        cflags += f' -I{harf_inc_dir} -I{join(harf_inc_dir, "src")}'
-        cflags += f' -I{free_inc_dir}'
-        cflags += f' -I{jpeg_inc_dir}'
-        if build_with_webp_support:
-            cflags += f' -I{join(webp_install, "include")}'
-        cflags += f' -I{self.ctx.ndk.sysroot_include_dir}'
-
-        # Link the basic Pillow libraries...no need to add webp's libraries
-        # since it seems that the linkage is properly made without it :)
-        env['LIBS'] = ' -lpng -lfreetype -lharfbuzz -ljpeg -lturbojpeg -lm'
-
-        # Add libraries locations to LDFLAGS
-        env['LDFLAGS'] += f' -L{png_lib_dir}'
-        env['LDFLAGS'] += f' -L{free_lib_dir}'
-        env['LDFLAGS'] += f' -L{harf_lib_dir}'
-        env['LDFLAGS'] += f' -L{jpeg_lib_dir}'
-        if build_with_webp_support:
-            env['LDFLAGS'] += f' -L{join(webp_install, "lib")}'
-        env['LDFLAGS'] += f' -L{arch.ndk_lib_dir_versioned}'
-        if cflags not in env['CFLAGS']:
-            env['CFLAGS'] += cflags + " -lm"
+            env["WEBP_ROOT"] = f"{join(webp_install, 'lib')}:{join(webp_install, 'include')}"
         return env
 
 


Test patch: