-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Added alpha_quality argument when saving WebP images #7872
Conversation
3b6a530
to
64c8c27
Compare
I'm not 100% sure if this is actually working and it's a little difficult to tell as unknown save flags are silently dropped. I'm installing like:
and a grep of the PIL directory shows I run a small script like from PIL import Image
image = Image.open("t.png")
image.save("alpha_trans.webp", "WEBP", quality=60, method=6, exif=b"", alpha_quality=50) with something with significant alpha such as http://moya-discover-assets.moya.app/moya/icons/sport/sportsbetting.png results in output like: $ webpinfo -summary alpha_trans.webp
File: alpha_trans.webp
...
Chunk ALPH at offset 30, length 4960
Chunk VP8 at offset 4990, length 2874
... Whereas with cwebp: $ cwebp -alpha_q 50 -q 60 -m 6 t.png -o cwebp.webp
$ webpinfo -summary cwebp.webp
Chunk ALPH at offset 30, length 1952
Chunk VP8 at offset 1982, length 2874 So I suspect somehow the flag is not making it through to the underlying library or there is some other variable that also needs tweaking if not going for |
When I install this PR and run that code with that image, I get
So my theory would be this is working, but it's simply not installed correctly on your machine. If you run Would it help if I put together a wheel of this for you? What OS/Python version are you using? |
So here's basically the setup that I'm using: Dockerfile: FROM python:3.12-slim-bookworm
RUN apt update && \
DEBIAN_FRONTEND=noninteractive apt install --no-install-recommends --no-install-suggests -y nginx curl jq git openssh-client libcairo2 libnginx-mod-http-brotli-static libnginx-mod-http-brotli-filter && \
echo StrictHostKeyChecking=accept-new >> /etc/ssh/ssh_config
# Extra for installing custom pillow version which includes support for webp
# alpha compression. TODO: Remove this when pillow with
# https://github.com/python-pillow/Pillow/pull/7872 merged is released.
# This package list is taken from https://github.com/python-pillow/docker-images/blob/main/debian-12-bookworm-amd64/Dockerfile
RUN DEBIAN_FRONTEND=noninteractive apt install --no-install-recommends --no-install-suggests -y \
gcc \
cmake \
libfreetype6-dev \
libfribidi-dev \
libharfbuzz-dev \
libjpeg-turbo-progs \
libjpeg62-turbo-dev \
liblcms2-dev \
libopenjp2-7-dev \
libtiff5-dev \
libwebp-dev \
libssl-dev \
meson \
netpbm \
zlib1g-dev
WORKDIR /app
COPY requirements.txt ./
RUN pip3 install --no-cache-dir -r requirements.txt with requirements.txt having
I added the version printing to the test script and it's outputting the correct version as you say but still not compressing the alpha which seems really strange. I'd assume library version doesn't make much of a difference but just in case:
|
I ran the Docker scenario you described, and received the same filesize output whether I specified Then I tried changing the pip requirement to
and the output file size changed. I don't know why one requirement works and the other doesn't, but give that a try. |
Yes thank you this fixes it. Very strange - I'm sure the #commit approach worked with some other projects but it could perhaps be because of the dev on a different repo. All lgtm. |
Resolves #7868
Adds
alpha_quality
argument when saving WebP images, as per https://developers.google.com/speed/webp/docs/api