diff --git a/.ci/install.sh b/.ci/install.sh index 2178c6646..52b821417 100755 --- a/.ci/install.sh +++ b/.ci/install.sh @@ -51,10 +51,10 @@ pushd depends && ./install_webp.sh && popd pushd depends && ./install_imagequant.sh && popd # raqm -pushd depends && ./install_raqm.sh && popd +pushd depends && sudo ./install_raqm.sh && popd # libavif -pushd depends && ./install_libavif.sh && popd +pushd depends && sudo ./install_libavif.sh && popd # extra test images pushd depends && ./install_extra_test_images.sh && popd diff --git a/.ci/requirements-cibw.txt b/.ci/requirements-cibw.txt index 823671828..56517374f 100644 --- a/.ci/requirements-cibw.txt +++ b/.ci/requirements-cibw.txt @@ -1 +1 @@ -cibuildwheel==3.1.2 +cibuildwheel==3.2.1 diff --git a/.ci/requirements-mypy.txt b/.ci/requirements-mypy.txt index 99eac6027..447856433 100644 --- a/.ci/requirements-mypy.txt +++ b/.ci/requirements-mypy.txt @@ -1,4 +1,4 @@ -mypy==1.17.0 +mypy==1.18.2 IceSpringPySideStubs-PyQt6 IceSpringPySideStubs-PySide6 ipython diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 626824f38..cf917407c 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -32,12 +32,12 @@ jobs: name: Docs steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false - name: Set up Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: "3.x" cache: pip diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 8e789a734..2addbaf67 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,7 +20,7 @@ jobs: name: Lint steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false @@ -33,7 +33,7 @@ jobs: lint-pre-commit- - name: Set up Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: "3.x" cache: pip diff --git a/.github/workflows/macos-install.sh b/.github/workflows/macos-install.sh index 94e3d5d08..b114d4a23 100755 --- a/.github/workflows/macos-install.sh +++ b/.github/workflows/macos-install.sh @@ -2,9 +2,6 @@ set -e -if [[ "$ImageOS" == "macos13" ]]; then - brew uninstall gradle maven -fi brew install \ aom \ dav1d \ diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index 61ccf58e2..1b0c3c654 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -22,7 +22,7 @@ jobs: steps: - name: "Check issues" - uses: actions/stale@v9 + uses: actions/stale@v10 with: repo-token: ${{ secrets.GITHUB_TOKEN }} only-labels: "Awaiting OP Action" diff --git a/.github/workflows/test-docker.yml b/.github/workflows/test-docker.yml index 0b90732eb..30e5c494d 100644 --- a/.github/workflows/test-docker.yml +++ b/.github/workflows/test-docker.yml @@ -47,6 +47,8 @@ jobs: centos-stream-10-amd64, debian-12-bookworm-x86, debian-12-bookworm-amd64, + debian-13-trixie-x86, + debian-13-trixie-amd64, fedora-41-amd64, fedora-42-amd64, gentoo, @@ -66,7 +68,7 @@ jobs: name: ${{ matrix.docker }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false diff --git a/.github/workflows/test-mingw.yml b/.github/workflows/test-mingw.yml index 5a83c16c3..6c4206083 100644 --- a/.github/workflows/test-mingw.yml +++ b/.github/workflows/test-mingw.yml @@ -45,7 +45,7 @@ jobs: steps: - name: Checkout Pillow - uses: actions/checkout@v4 + uses: actions/checkout@v5 with: persist-credentials: false diff --git a/.github/workflows/test-valgrind-memory.yml b/.github/workflows/test-valgrind-memory.yml index e6a5f6e77..0f36fe30d 100644 --- a/.github/workflows/test-valgrind-memory.yml +++ b/.github/workflows/test-valgrind-memory.yml @@ -41,7 +41,7 @@ jobs: name: ${{ matrix.docker }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false diff --git a/.github/workflows/test-valgrind.yml b/.github/workflows/test-valgrind.yml index 8818b3b23..30caa0d4e 100644 --- a/.github/workflows/test-valgrind.yml +++ b/.github/workflows/test-valgrind.yml @@ -39,7 +39,7 @@ jobs: name: ${{ matrix.docker }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml index c80bb6eb6..f6a7dd46b 100644 --- a/.github/workflows/test-windows.yml +++ b/.github/workflows/test-windows.yml @@ -47,19 +47,19 @@ jobs: steps: - name: Checkout Pillow - uses: actions/checkout@v4 + uses: actions/checkout@v5 with: persist-credentials: false - name: Checkout cached dependencies - uses: actions/checkout@v4 + uses: actions/checkout@v5 with: persist-credentials: false repository: python-pillow/pillow-depends path: winbuild\depends - name: Checkout extra test images - uses: actions/checkout@v4 + uses: actions/checkout@v5 with: persist-credentials: false repository: python-pillow/test-images @@ -67,7 +67,7 @@ jobs: # sets env: pythonLocation - name: Set up Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} allow-prereleases: true @@ -97,8 +97,8 @@ jobs: choco install nasm --no-progress echo "C:\Program Files\NASM" >> $env:GITHUB_PATH - choco install ghostscript --version=10.5.1 --no-progress - echo "C:\Program Files\gs\gs10.05.1\bin" >> $env:GITHUB_PATH + choco install ghostscript --version=10.6.0 --no-progress + echo "C:\Program Files\gs\gs10.06.0\bin" >> $env:GITHUB_PATH # Install extra test images xcopy /S /Y Tests\test-images\* Tests\images diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c075f04d7..b52000a27 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -57,7 +57,7 @@ jobs: - { python-version: "3.14t", disable-gil: true } - { python-version: "3.13t", disable-gil: true } # Intel - - { os: "macos-13", python-version: "3.10" } + - { os: "macos-15-intel", python-version: "3.10" } exclude: - { os: "macos-latest", python-version: "3.10" } @@ -65,12 +65,12 @@ jobs: name: ${{ matrix.os }} Python ${{ matrix.python-version }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} allow-prereleases: true @@ -111,7 +111,7 @@ jobs: GHA_PYTHON_VERSION: ${{ matrix.python-version }} - name: Register gcc problem matcher - if: "matrix.os == 'ubuntu-latest' && matrix.python-version == '3.13'" + if: "matrix.os == 'ubuntu-latest' && matrix.python-version == '3.14'" run: echo "::add-matcher::.github/problem-matchers/gcc.json" - name: Build diff --git a/.github/workflows/wheels-dependencies.sh b/.github/workflows/wheels-dependencies.sh index d58c65126..7d6eb8681 100755 --- a/.github/workflows/wheels-dependencies.sh +++ b/.github/workflows/wheels-dependencies.sh @@ -93,16 +93,20 @@ ARCHIVE_SDIR=pillow-depends-main # Package versions for fresh source builds. Version numbers with "Patched" # annotations have a source code patch that is required for some platforms. If # you change those versions, ensure the patch is also updated. -FREETYPE_VERSION=2.13.3 -HARFBUZZ_VERSION=11.2.1 +if [[ -n "$IOS_SDK" ]]; then + FREETYPE_VERSION=2.13.3 +else + FREETYPE_VERSION=2.14.1 +fi +HARFBUZZ_VERSION=12.1.0 LIBPNG_VERSION=1.6.50 -JPEGTURBO_VERSION=3.1.1 -OPENJPEG_VERSION=2.5.3 +JPEGTURBO_VERSION=3.1.2 +OPENJPEG_VERSION=2.5.4 XZ_VERSION=5.8.1 -TIFF_VERSION=4.7.0 +ZSTD_VERSION=1.5.7 +TIFF_VERSION=4.7.1 LCMS2_VERSION=2.17 -ZLIB_VERSION=1.3.1 -ZLIB_NG_VERSION=2.2.4 +ZLIB_NG_VERSION=2.2.5 LIBWEBP_VERSION=1.6.0 BZIP2_VERSION=1.0.8 LIBXCB_VERSION=1.17.0 @@ -165,7 +169,7 @@ function build_brotli { local out_dir=$(fetch_unpack https://github.com/google/brotli/archive/v$BROTLI_VERSION.tar.gz brotli-$BROTLI_VERSION.tar.gz) (cd $out_dir \ && cmake -DCMAKE_INSTALL_PREFIX=$BUILD_PREFIX -DCMAKE_INSTALL_LIBDIR=$BUILD_PREFIX/lib -DCMAKE_INSTALL_NAME_DIR=$BUILD_PREFIX/lib $HOST_CMAKE_FLAGS . \ - && make install) + && make -j4 install) touch brotli-stamp } @@ -249,18 +253,26 @@ function build_libavif { cp $WORKDIR/meson-cross.txt $out_dir/crossfile-apple.meson fi - (cd $out_dir && make install) + (cd $out_dir && make -j4 install) touch libavif-stamp } +function build_zstd { + if [ -e zstd-stamp ]; then return; fi + local out_dir=$(fetch_unpack https://github.com/facebook/zstd/releases/download/v$ZSTD_VERSION/zstd-$ZSTD_VERSION.tar.gz) + (cd $out_dir \ + && make -j4 install) + touch zstd-stamp +} + function build { build_xz if [ -z "$IS_ALPINE" ] && [ -z "$SANITIZER" ] && [ -z "$IS_MACOS" ]; then yum remove -y zlib-devel fi - if [[ -n "$IS_MACOS" ]] && [[ "$MACOSX_DEPLOYMENT_TARGET" == "10.10" || "$MACOSX_DEPLOYMENT_TARGET" == "10.13" ]]; then - build_new_zlib + if [[ -n "$IS_MACOS" ]]; then + CFLAGS="$CFLAGS -headerpad_max_install_names" build_zlib_ng else build_zlib_ng fi @@ -285,6 +297,7 @@ function build { --with-jpeg-include-dir=$BUILD_PREFIX/include --with-jpeg-lib-dir=$BUILD_PREFIX/lib \ --disable-webp --disable-libdeflate --disable-zstd else + build_zstd build_tiff fi @@ -309,6 +322,10 @@ function build { if [[ -n "$IS_MACOS" ]]; then # Custom freetype build + if [[ -z "$IOS_SDK" ]]; then + build_simple sed 4.9 https://mirrors.middlendian.com/gnu/sed + fi + build_simple freetype $FREETYPE_VERSION https://download.savannah.gnu.org/releases/freetype tar.gz --with-harfbuzz=no else build_freetype diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 5cc4f0355..21ea79553 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -39,6 +39,7 @@ concurrency: cancel-in-progress: true env: + EXPECTED_DISTS: 91 FORCE_COLOR: 1 jobs: @@ -52,21 +53,21 @@ jobs: include: - name: "macOS 10.10 x86_64" platform: macos - os: macos-13 + os: macos-15-intel cibw_arch: x86_64 - build: "cp3{9,10,11}*" + build: "cp3{10,11}*" macosx_deployment_target: "10.10" - name: "macOS 10.13 x86_64" platform: macos - os: macos-13 + os: macos-15-intel cibw_arch: x86_64 - build: "cp3{12,13,14}*" + build: "cp3{12,13}*" macosx_deployment_target: "10.13" - name: "macOS 10.15 x86_64" platform: macos - os: macos-13 + os: macos-15-intel cibw_arch: x86_64 - build: "pp3*" + build: "{cp314,pp3}*" macosx_deployment_target: "10.15" - name: "macOS arm64" platform: macos @@ -103,15 +104,15 @@ jobs: cibw_arch: arm64_iphonesimulator - name: "iOS x86_64 simulator" platform: ios - os: macos-13 + os: macos-15-intel cibw_arch: x86_64_iphonesimulator steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false submodules: true - - uses: actions/setup-python@v5 + - uses: actions/setup-python@v6 with: python-version: "3.x" @@ -153,18 +154,18 @@ jobs: - cibw_arch: ARM64 os: windows-11-arm steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false - name: Checkout extra test images - uses: actions/checkout@v4 + uses: actions/checkout@v5 with: persist-credentials: false repository: python-pillow/test-images path: Tests\test-images - - uses: actions/setup-python@v5 + - uses: actions/setup-python@v6 with: python-version: "3.x" @@ -231,15 +232,15 @@ jobs: path: winbuild\build\bin\fribidi* sdist: - if: github.event_name != 'schedule' + if: github.event_name != 'schedule' || github.repository_owner == 'python-pillow' runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 with: persist-credentials: false - name: Set up Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: "3.x" @@ -250,15 +251,33 @@ jobs: name: dist-sdist path: dist/*.tar.gz + count-dists: + needs: [build-native-wheels, windows, sdist] + runs-on: ubuntu-latest + name: Count dists + steps: + - uses: actions/download-artifact@v5 + with: + pattern: dist-* + path: dist + merge-multiple: true + - name: "What did we get?" + run: | + ls -alR + echo "Number of dists, should be $EXPECTED_DISTS:" + files=$(ls dist 2>/dev/null | wc -l) + echo $files + [ "$files" -eq $EXPECTED_DISTS ] || exit 1 + scientific-python-nightly-wheels-publish: if: github.repository_owner == 'python-pillow' && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') - needs: [build-native-wheels, windows] + needs: count-dists runs-on: ubuntu-latest name: Upload wheels to scientific-python-nightly-wheels steps: - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@v5 with: - pattern: dist-* + pattern: dist-!(sdist)* path: dist merge-multiple: true - name: Upload wheels to scientific-python-nightly-wheels @@ -269,7 +288,7 @@ jobs: pypi-publish: if: github.repository_owner == 'python-pillow' && github.event_name == 'push' && startsWith(github.ref, 'refs/tags') - needs: [build-native-wheels, windows, sdist] + needs: count-dists runs-on: ubuntu-latest name: Upload release to PyPI environment: @@ -278,7 +297,7 @@ jobs: permissions: id-token: write steps: - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@v5 with: pattern: dist-* path: dist diff --git a/.github/zizmor.yml b/.github/zizmor.yml index 5bdc48c30..b56709781 100644 --- a/.github/zizmor.yml +++ b/.github/zizmor.yml @@ -1,5 +1,5 @@ # Configuration for the zizmor static analysis tool, run via pre-commit in CI -# https://woodruffw.github.io/zizmor/configuration/ +# https://docs.zizmor.sh/configuration/ rules: unpinned-uses: config: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 75c7d3632..ab0153687 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,12 +1,12 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.12.2 + rev: v0.13.3 hooks: - id: ruff-check args: [--exit-non-zero-on-fix] - repo: https://github.com/psf/black-pre-commit-mirror - rev: 25.1.0 + rev: 25.9.0 hooks: - id: black @@ -24,7 +24,7 @@ repos: exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.gd$|\.opt$|\.patch$) - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v20.1.7 + rev: v21.1.2 hooks: - id: clang-format types: [c] @@ -36,7 +36,7 @@ repos: - id: rst-backticks - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v5.0.0 + rev: v6.0.0 hooks: - id: check-executables-have-shebangs - id: check-shebang-scripts-are-executable @@ -51,14 +51,14 @@ repos: exclude: ^.github/.*TEMPLATE|^Tests/(fonts|images)/|\.patch$ - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 0.33.2 + rev: 0.34.0 hooks: - id: check-github-workflows - id: check-readthedocs - id: check-renovate - - repo: https://github.com/woodruffw/zizmor-pre-commit - rev: v1.11.0 + - repo: https://github.com/zizmorcore/zizmor-pre-commit + rev: v1.14.2 hooks: - id: zizmor @@ -68,7 +68,7 @@ repos: - id: sphinx-lint - repo: https://github.com/tox-dev/pyproject-fmt - rev: v2.6.0 + rev: v2.7.0 hooks: - id: pyproject-fmt @@ -79,7 +79,7 @@ repos: additional_dependencies: [trove-classifiers>=2024.10.12] - repo: https://github.com/tox-dev/tox-ini-fmt - rev: 1.5.0 + rev: 1.6.0 hooks: - id: tox-ini-fmt diff --git a/Tests/createfontdatachunk.py b/Tests/createfontdatachunk.py old mode 100755 new mode 100644 index 41c76f87e..0a3fdb809 --- a/Tests/createfontdatachunk.py +++ b/Tests/createfontdatachunk.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 from __future__ import annotations import base64 diff --git a/Tests/helper.py b/Tests/helper.py index e0dc8a9d4..dbdd30b42 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -175,6 +175,14 @@ def skip_unless_feature(feature: str) -> pytest.MarkDecorator: return pytest.mark.skipif(not features.check(feature), reason=reason) +def has_feature_version(feature: str, required: str) -> bool: + version = features.version(feature) + assert version is not None + version_required = parse_version(required) + version_available = parse_version(version) + return version_available >= version_required + + def skip_unless_feature_version( feature: str, required: str, reason: str | None = None ) -> pytest.MarkDecorator: diff --git a/Tests/images/colr_bungee.png b/Tests/images/colr_bungee.png index b10a60be0..9ec6b1182 100644 Binary files a/Tests/images/colr_bungee.png and b/Tests/images/colr_bungee.png differ diff --git a/Tests/images/colr_bungee_mask.png b/Tests/images/colr_bungee_mask.png index f13e17677..79106b639 100644 Binary files a/Tests/images/colr_bungee_mask.png and b/Tests/images/colr_bungee_mask.png differ diff --git a/Tests/images/colr_bungee_older.png b/Tests/images/colr_bungee_older.png new file mode 100644 index 000000000..b10a60be0 Binary files /dev/null and b/Tests/images/colr_bungee_older.png differ diff --git a/Tests/images/crash-5762152299364352.fli b/Tests/images/crash-5762152299364352.fli index 944fe0b56..d7588eea8 100644 Binary files a/Tests/images/crash-5762152299364352.fli and b/Tests/images/crash-5762152299364352.fli differ diff --git a/Tests/images/frame_size.mpo b/Tests/images/frame_size.mpo new file mode 100644 index 000000000..ee5c6cdf7 Binary files /dev/null and b/Tests/images/frame_size.mpo differ diff --git a/Tests/images/sugarshack_frame_size.mpo b/Tests/images/sugarshack_frame_size.mpo deleted file mode 100644 index abff98ea5..000000000 Binary files a/Tests/images/sugarshack_frame_size.mpo and /dev/null differ diff --git a/Tests/images/timeout-9139147ce93e20eb14088fe238e541443ffd64b3.fli b/Tests/images/timeout-9139147ce93e20eb14088fe238e541443ffd64b3.fli index ce4607d2d..73da81dcb 100644 Binary files a/Tests/images/timeout-9139147ce93e20eb14088fe238e541443ffd64b3.fli and b/Tests/images/timeout-9139147ce93e20eb14088fe238e541443ffd64b3.fli differ diff --git a/Tests/images/timeout-bff0a9dc7243a8e6ede2408d2ffa6a9964698b87.fli b/Tests/images/timeout-bff0a9dc7243a8e6ede2408d2ffa6a9964698b87.fli index 77a94b87a..abe642e6a 100644 Binary files a/Tests/images/timeout-bff0a9dc7243a8e6ede2408d2ffa6a9964698b87.fli and b/Tests/images/timeout-bff0a9dc7243a8e6ede2408d2ffa6a9964698b87.fli differ diff --git a/Tests/test_arro3.py b/Tests/test_arro3.py new file mode 100644 index 000000000..60955cfdb --- /dev/null +++ b/Tests/test_arro3.py @@ -0,0 +1,275 @@ +from __future__ import annotations + +import json +from typing import Any, NamedTuple + +import pytest + +from PIL import Image + +from .helper import ( + assert_deep_equal, + assert_image_equal, + hopper, + is_big_endian, +) + +TYPE_CHECKING = False +if TYPE_CHECKING: + from arro3 import compute # type: ignore [import-not-found] + from arro3.core import ( # type: ignore [import-not-found] + Array, + DataType, + Field, + fixed_size_list_array, + ) +else: + arro3 = pytest.importorskip("arro3", reason="Arro3 not installed") + from arro3 import compute + from arro3.core import Array, DataType, Field, fixed_size_list_array + +TEST_IMAGE_SIZE = (10, 10) + + +def _test_img_equals_pyarray( + img: Image.Image, arr: Any, mask: list[int] | None, elts_per_pixel: int = 1 +) -> None: + assert img.height * img.width * elts_per_pixel == len(arr) + px = img.load() + assert px is not None + if elts_per_pixel > 1 and mask is None: + # have to do element-wise comparison when we're comparing + # flattened r,g,b,a to a pixel. + mask = list(range(elts_per_pixel)) + for x in range(0, img.size[0], int(img.size[0] / 10)): + for y in range(0, img.size[1], int(img.size[1] / 10)): + if mask: + pixel = px[x, y] + assert isinstance(pixel, tuple) + for ix, elt in enumerate(mask): + if elts_per_pixel == 1: + assert pixel[ix] == arr[y * img.width + x].as_py()[elt] + else: + assert ( + pixel[ix] + == arr[(y * img.width + x) * elts_per_pixel + elt].as_py() + ) + else: + assert_deep_equal(px[x, y], arr[y * img.width + x].as_py()) + + +def _test_img_equals_int32_pyarray( + img: Image.Image, arr: Any, mask: list[int] | None, elts_per_pixel: int = 1 +) -> None: + assert img.height * img.width * elts_per_pixel == len(arr) + px = img.load() + assert px is not None + if mask is None: + # have to do element-wise comparison when we're comparing + # flattened rgba in an uint32 to a pixel. + mask = list(range(elts_per_pixel)) + for x in range(0, img.size[0], int(img.size[0] / 10)): + for y in range(0, img.size[1], int(img.size[1] / 10)): + pixel = px[x, y] + assert isinstance(pixel, tuple) + arr_pixel_int = arr[y * img.width + x].as_py() + arr_pixel_tuple = ( + arr_pixel_int % 256, + (arr_pixel_int // 256) % 256, + (arr_pixel_int // 256**2) % 256, + (arr_pixel_int // 256**3), + ) + if is_big_endian(): + arr_pixel_tuple = arr_pixel_tuple[::-1] + + for ix, elt in enumerate(mask): + assert pixel[ix] == arr_pixel_tuple[elt] + + +fl_uint8_4_type = DataType.list(Field("_", DataType.uint8()).with_nullable(False), 4) + + +@pytest.mark.parametrize( + "mode, dtype, mask", + ( + ("L", DataType.uint8(), None), + ("I", DataType.int32(), None), + ("F", DataType.float32(), None), + ("LA", fl_uint8_4_type, [0, 3]), + ("RGB", fl_uint8_4_type, [0, 1, 2]), + ("RGBA", fl_uint8_4_type, None), + ("RGBX", fl_uint8_4_type, None), + ("CMYK", fl_uint8_4_type, None), + ("YCbCr", fl_uint8_4_type, [0, 1, 2]), + ("HSV", fl_uint8_4_type, [0, 1, 2]), + ), +) +def test_to_array(mode: str, dtype: DataType, mask: list[int] | None) -> None: + img = hopper(mode) + + # Resize to non-square + img = img.crop((3, 0, 124, 127)) + assert img.size == (121, 127) + + arr = Array(img) + _test_img_equals_pyarray(img, arr, mask) + assert arr.type == dtype + + reloaded = Image.fromarrow(arr, mode, img.size) + assert_image_equal(img, reloaded) + + +def test_lifetime() -> None: + # valgrind shouldn't error out here. + # arrays should be accessible after the image is deleted. + + img = hopper("L") + + arr_1 = Array(img) + arr_2 = Array(img) + + del img + + assert compute.sum(arr_1).as_py() > 0 + del arr_1 + + assert compute.sum(arr_2).as_py() > 0 + del arr_2 + + +def test_lifetime2() -> None: + # valgrind shouldn't error out here. + # img should remain after the arrays are collected. + + img = hopper("L") + + arr_1 = Array(img) + arr_2 = Array(img) + + assert compute.sum(arr_1).as_py() > 0 + del arr_1 + + assert compute.sum(arr_2).as_py() > 0 + del arr_2 + + img2 = img.copy() + px = img2.load() + assert px # make mypy happy + assert isinstance(px[0, 0], int) + + +class DataShape(NamedTuple): + dtype: DataType + # Strictly speaking, elt should be a pixel or pixel component, so + # list[uint8][4], float, int, uint32, uint8, etc. But more + # correctly, it should be exactly the dtype from the line above. + elt: Any + elts_per_pixel: int + + +UINT_ARR = DataShape( + dtype=fl_uint8_4_type, + elt=[1, 2, 3, 4], # array of 4 uint8 per pixel + elts_per_pixel=1, # only one array per pixel +) + +UINT = DataShape( + dtype=DataType.uint8(), + elt=3, # one uint8, + elts_per_pixel=4, # but repeated 4x per pixel +) + +UINT32 = DataShape( + dtype=DataType.uint32(), + elt=0xABCDEF45, # one packed int, doesn't fit in a int32 > 0x80000000 + elts_per_pixel=1, # one per pixel +) + +INT32 = DataShape( + dtype=DataType.uint32(), + elt=0x12CDEF45, # one packed int + elts_per_pixel=1, # one per pixel +) + + +@pytest.mark.parametrize( + "mode, data_tp, mask", + ( + ("L", DataShape(DataType.uint8(), 3, 1), None), + ("I", DataShape(DataType.int32(), 1 << 24, 1), None), + ("F", DataShape(DataType.float32(), 3.14159, 1), None), + ("LA", UINT_ARR, [0, 3]), + ("LA", UINT, [0, 3]), + ("RGB", UINT_ARR, [0, 1, 2]), + ("RGBA", UINT_ARR, None), + ("CMYK", UINT_ARR, None), + ("YCbCr", UINT_ARR, [0, 1, 2]), + ("HSV", UINT_ARR, [0, 1, 2]), + ("RGB", UINT, [0, 1, 2]), + ("RGBA", UINT, None), + ("CMYK", UINT, None), + ("YCbCr", UINT, [0, 1, 2]), + ("HSV", UINT, [0, 1, 2]), + ), +) +def test_fromarray(mode: str, data_tp: DataShape, mask: list[int] | None) -> None: + (dtype, elt, elts_per_pixel) = data_tp + + ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] + if dtype == fl_uint8_4_type: + tmp_arr = Array(elt * (ct_pixels * elts_per_pixel), type=DataType.uint8()) + arr = fixed_size_list_array(tmp_arr, 4) + else: + arr = Array([elt] * (ct_pixels * elts_per_pixel), type=dtype) + img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) + + _test_img_equals_pyarray(img, arr, mask, elts_per_pixel) + + +@pytest.mark.parametrize( + "mode, mask", + ( + ("LA", [0, 3]), + ("RGB", [0, 1, 2]), + ("RGBA", None), + ("CMYK", None), + ("YCbCr", [0, 1, 2]), + ("HSV", [0, 1, 2]), + ), +) +@pytest.mark.parametrize("data_tp", (UINT32, INT32)) +def test_from_int32array(mode: str, mask: list[int] | None, data_tp: DataShape) -> None: + (dtype, elt, elts_per_pixel) = data_tp + + ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] + arr = Array([elt] * (ct_pixels * elts_per_pixel), type=dtype) + img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) + + _test_img_equals_int32_pyarray(img, arr, mask, elts_per_pixel) + + +@pytest.mark.parametrize( + "mode, metadata", + ( + ("LA", ["L", "X", "X", "A"]), + ("RGB", ["R", "G", "B", "X"]), + ("RGBX", ["R", "G", "B", "X"]), + ("RGBA", ["R", "G", "B", "A"]), + ("CMYK", ["C", "M", "Y", "K"]), + ("YCbCr", ["Y", "Cb", "Cr", "X"]), + ("HSV", ["H", "S", "V", "X"]), + ), +) +def test_image_metadata(mode: str, metadata: list[str]) -> None: + img = hopper(mode) + + arr = Array(img) + + assert arr.type.value_field + assert arr.type.value_field.metadata + assert arr.type.value_field.metadata[b"image"] + + parsed_metadata = json.loads(arr.type.value_field.metadata[b"image"].decode("utf8")) + + assert "bands" in parsed_metadata + assert parsed_metadata["bands"] == metadata diff --git a/Tests/test_file_avif.py b/Tests/test_file_avif.py index 3fac51ac6..727191153 100644 --- a/Tests/test_file_avif.py +++ b/Tests/test_file_avif.py @@ -14,6 +14,7 @@ import pytest from PIL import ( AvifImagePlugin, + GifImagePlugin, Image, ImageDraw, ImageFile, @@ -220,6 +221,7 @@ class TestFileAvif: def test_background_from_gif(self, tmp_path: Path) -> None: with Image.open("Tests/images/chi.gif") as im: original_value = im.convert("RGB").getpixel((1, 1)) + assert isinstance(original_value, tuple) # Save as AVIF out_avif = tmp_path / "temp.avif" @@ -232,6 +234,7 @@ class TestFileAvif: with Image.open(out_gif) as reread: reread_value = reread.convert("RGB").getpixel((1, 1)) + assert isinstance(reread_value, tuple) difference = sum([abs(original_value[i] - reread_value[i]) for i in range(3)]) assert difference <= 6 @@ -240,6 +243,7 @@ class TestFileAvif: with Image.open("Tests/images/chi.gif") as im: im.save(temp_file) with Image.open(temp_file) as im: + assert isinstance(im, AvifImagePlugin.AvifImageFile) assert im.n_frames == 1 def test_invalid_file(self) -> None: @@ -598,10 +602,12 @@ class TestAvifAnimation: """ with Image.open(TEST_AVIF_FILE) as im: + assert isinstance(im, AvifImagePlugin.AvifImageFile) assert im.n_frames == 1 assert not im.is_animated with Image.open("Tests/images/avif/star.avifs") as im: + assert isinstance(im, AvifImagePlugin.AvifImageFile) assert im.n_frames == 5 assert im.is_animated @@ -612,11 +618,13 @@ class TestAvifAnimation: """ with Image.open("Tests/images/avif/star.gif") as original: + assert isinstance(original, GifImagePlugin.GifImageFile) assert original.n_frames > 1 temp_file = tmp_path / "temp.avif" original.save(temp_file, save_all=True) with Image.open(temp_file) as im: + assert isinstance(im, AvifImagePlugin.AvifImageFile) assert im.n_frames == original.n_frames # Compare first frame in P mode to frame from original GIF @@ -636,6 +644,7 @@ class TestAvifAnimation: def check(temp_file: Path) -> None: with Image.open(temp_file) as im: + assert isinstance(im, AvifImagePlugin.AvifImageFile) assert im.n_frames == 4 # Compare first frame to original @@ -708,6 +717,7 @@ class TestAvifAnimation: ) with Image.open(temp_file) as im: + assert isinstance(im, AvifImagePlugin.AvifImageFile) assert im.n_frames == 5 assert im.is_animated @@ -737,6 +747,7 @@ class TestAvifAnimation: ) with Image.open(temp_file) as im: + assert isinstance(im, AvifImagePlugin.AvifImageFile) assert im.n_frames == 5 assert im.is_animated diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index 746b2e180..c1c430aa5 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -6,6 +6,8 @@ from pathlib import Path import pytest from PIL import BmpImagePlugin, Image, _binary +from PIL._binary import o16le as o16 +from PIL._binary import o32le as o32 from .helper import ( assert_image_equal, @@ -114,7 +116,7 @@ def test_save_float_dpi(tmp_path: Path) -> None: def test_load_dib() -> None: - # test for #1293, Imagegrab returning Unsupported Bitfields Format + # test for #1293, ImageGrab returning Unsupported Bitfields Format with Image.open("Tests/images/clipboard.dib") as im: assert im.format == "DIB" assert im.get_format_mimetype() == "image/bmp" @@ -219,6 +221,18 @@ def test_rle8_eof(file_name: str, length: int) -> None: im.load() +def test_unsupported_bmp_bitfields_layout() -> None: + fp = io.BytesIO( + o32(40) # header size + + b"\x00" * 10 + + o16(1) # bits + + o32(3) # BITFIELDS compression + + b"\x00" * 32 + ) + with pytest.raises(OSError, match="Unsupported BMP bitfields layout"): + Image.open(fp) + + def test_offset() -> None: # This image has been hexedited # to exclude the palette size from the pixel data offset diff --git a/Tests/test_file_cur.py b/Tests/test_file_cur.py index dbf1b866d..4b3e3afcb 100644 --- a/Tests/test_file_cur.py +++ b/Tests/test_file_cur.py @@ -1,8 +1,13 @@ from __future__ import annotations +from io import BytesIO + import pytest from PIL import CurImagePlugin, Image +from PIL._binary import o8 +from PIL._binary import o16le as o16 +from PIL._binary import o32le as o32 TEST_FILE = "Tests/images/deerstalker.cur" @@ -17,6 +22,24 @@ def test_sanity() -> None: assert im.getpixel((16, 16)) == (84, 87, 86, 255) +def test_largest_cursor() -> None: + magic = b"\x00\x00\x02\x00" + sizes = ((1, 1), (8, 8), (4, 4)) + data = magic + o16(len(sizes)) + for w, h in sizes: + image_offset = 6 + len(sizes) * 16 if (w, h) == max(sizes) else 0 + data += o8(w) + o8(h) + o8(0) * 10 + o32(image_offset) + data += ( + o32(12) # header size + + o16(8) # width + + o16(16) # height + + o16(0) # planes + + o16(1) # bits + ) + with Image.open(BytesIO(data)) as im: + assert im.size == (8, 8) + + def test_invalid_file() -> None: invalid_file = "Tests/images/flower.jpg" @@ -26,6 +49,7 @@ def test_invalid_file() -> None: no_cursors_file = "Tests/images/no_cursors.cur" cur = CurImagePlugin.CurImageFile(TEST_FILE) + assert cur.fp is not None cur.fp.close() with open(no_cursors_file, "rb") as cur.fp: with pytest.raises(TypeError): diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index d94de7287..b50915f28 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -197,6 +197,14 @@ def test_load_long_binary_data(prefix: bytes) -> None: assert img.format == "EPS" +def test_begin_binary() -> None: + with open("Tests/images/eps/binary_preview_map.eps", "rb") as fp: + data = bytearray(fp.read()) + data[76875 : 76875 + 11] = b"%" * 11 + with Image.open(io.BytesIO(data)) as img: + assert img.size == (399, 480) + + @mark_if_feature_version( pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing" ) diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index 0fadd01d0..13c6a4323 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -48,6 +48,7 @@ def test_sanity() -> None: def test_prefix_chunk(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(ImageFile, "LOAD_TRUNCATED_IMAGES", True) with Image.open(animated_test_file_with_prefix_chunk) as im: + assert isinstance(im, FliImagePlugin.FliImageFile) assert im.mode == "P" assert im.size == (320, 200) assert im.format == "FLI" @@ -55,6 +56,7 @@ def test_prefix_chunk(monkeypatch: pytest.MonkeyPatch) -> None: assert im.is_animated palette = im.getpalette() + assert palette is not None assert palette[3:6] == [255, 255, 255] assert palette[381:384] == [204, 204, 12] assert palette[765:] == [252, 0, 0] diff --git a/Tests/test_file_gbr.py b/Tests/test_file_gbr.py index 1b834cd3c..b8851d82b 100644 --- a/Tests/test_file_gbr.py +++ b/Tests/test_file_gbr.py @@ -1,8 +1,10 @@ from __future__ import annotations +from io import BytesIO + import pytest -from PIL import GbrImagePlugin, Image +from PIL import GbrImagePlugin, Image, _binary from .helper import assert_image_equal_tofile @@ -31,8 +33,49 @@ def test_multiple_load_operations() -> None: assert_image_equal_tofile(im, "Tests/images/gbr.png") -def test_invalid_file() -> None: - invalid_file = "Tests/images/flower.jpg" +def create_gbr_image(info: dict[str, int] = {}, magic_number=b"") -> BytesIO: + return BytesIO( + b"".join( + _binary.o32be(i) + for i in [ + info.get("header_size", 20), + info.get("version", 1), + info.get("width", 1), + info.get("height", 1), + info.get("color_depth", 1), + ] + ) + + magic_number + ) - with pytest.raises(SyntaxError): + +def test_invalid_file() -> None: + for f in [ + create_gbr_image({"header_size": 0}), + create_gbr_image({"width": 0}), + create_gbr_image({"height": 0}), + ]: + with pytest.raises(SyntaxError, match="not a GIMP brush"): + GbrImagePlugin.GbrImageFile(f) + + invalid_file = "Tests/images/flower.jpg" + with pytest.raises(SyntaxError, match="Unsupported GIMP brush version"): GbrImagePlugin.GbrImageFile(invalid_file) + + +def test_unsupported_gimp_brush() -> None: + f = create_gbr_image({"color_depth": 2}) + with pytest.raises(SyntaxError, match="Unsupported GIMP brush color depth: 2"): + GbrImagePlugin.GbrImageFile(f) + + +def test_bad_magic_number() -> None: + f = create_gbr_image({"version": 2}, magic_number=b"badm") + with pytest.raises(SyntaxError, match="not a GIMP brush, bad magic number"): + GbrImagePlugin.GbrImageFile(f) + + +def test_L() -> None: + f = create_gbr_image() + with Image.open(f) as im: + assert im.mode == "L" diff --git a/Tests/test_file_gd.py b/Tests/test_file_gd.py index 806532c17..8a49fd4fa 100644 --- a/Tests/test_file_gd.py +++ b/Tests/test_file_gd.py @@ -1,5 +1,7 @@ from __future__ import annotations +from io import BytesIO + import pytest from PIL import GdImageFile, UnidentifiedImageError @@ -16,6 +18,14 @@ def test_sanity() -> None: assert_image_similar_tofile(im.convert("RGB"), "Tests/images/hopper.jpg", 14) +def test_transparency() -> None: + with open(TEST_GD_FILE, "rb") as fp: + data = bytearray(fp.read()) + data[7:11] = b"\x00\x00\x00\x05" + with GdImageFile.open(BytesIO(data)) as im: + assert im.info["transparency"] == 5 + + def test_bad_mode() -> None: with pytest.raises(ValueError): GdImageFile.open(TEST_GD_FILE, "bad mode") diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index f5c2f360c..acf79374e 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -293,6 +293,7 @@ def test_roundtrip_save_all(tmp_path: Path) -> None: im.save(out, save_all=True) with Image.open(out) as reread: + assert isinstance(reread, GifImagePlugin.GifImageFile) assert reread.n_frames == 5 @@ -1374,6 +1375,7 @@ def test_palette_save_all_P(tmp_path: Path) -> None: with Image.open(out) as im: # Assert that the frames are correct, and each frame has the same palette + assert isinstance(im, GifImagePlugin.GifImageFile) assert_image_equal(im.convert("RGB"), frames[0].convert("RGB")) assert im.palette is not None assert im.global_palette is not None diff --git a/Tests/test_file_iptc.py b/Tests/test_file_iptc.py index 3c4c892c8..5a8aaa3ef 100644 --- a/Tests/test_file_iptc.py +++ b/Tests/test_file_iptc.py @@ -2,6 +2,8 @@ from __future__ import annotations from io import BytesIO +import pytest + from PIL import Image, IptcImagePlugin, TiffImagePlugin, TiffTags from .helper import assert_image_equal, hopper @@ -9,21 +11,78 @@ from .helper import assert_image_equal, hopper TEST_FILE = "Tests/images/iptc.jpg" +def create_iptc_image(info: dict[str, int] = {}) -> BytesIO: + def field(tag, value): + return bytes((0x1C,) + tag + (0, len(value))) + value + + data = field((3, 60), bytes((info.get("layers", 1), info.get("component", 0)))) + data += field((3, 120), bytes((info.get("compression", 1),))) + if "band" in info: + data += field((3, 65), bytes((info["band"] + 1,))) + data += field((3, 20), b"\x01") # width + data += field((3, 30), b"\x01") # height + data += field( + (8, 10), + bytes((info.get("data", 0),)), + ) + + return BytesIO(data) + + def test_open() -> None: expected = Image.new("L", (1, 1)) - f = BytesIO( - b"\x1c\x03<\x00\x02\x01\x00\x1c\x03x\x00\x01\x01\x1c\x03\x14\x00\x01\x01" - b"\x1c\x03\x1e\x00\x01\x01\x1c\x08\n\x00\x01\x00" - ) + f = create_iptc_image() with Image.open(f) as im: - assert im.tile == [("iptc", (0, 0, 1, 1), 25, "raw")] + assert im.tile == [("iptc", (0, 0, 1, 1), 25, ("raw", None))] assert_image_equal(im, expected) with Image.open(f) as im: assert im.load() is not None +def test_field_length() -> None: + f = create_iptc_image() + f.seek(28) + f.write(b"\xff") + with pytest.raises(OSError, match="illegal field length in IPTC/NAA file"): + with Image.open(f): + pass + + +@pytest.mark.parametrize("layers, mode", ((3, "RGB"), (4, "CMYK"))) +def test_layers(layers: int, mode: str) -> None: + for band in range(-1, layers): + info = {"layers": layers, "component": 1, "data": 5} + if band != -1: + info["band"] = band + f = create_iptc_image(info) + with Image.open(f) as im: + assert im.mode == mode + + data = [0] * layers + data[max(band, 0)] = 5 + assert im.getpixel((0, 0)) == tuple(data) + + +def test_unknown_compression() -> None: + f = create_iptc_image({"compression": 2}) + with pytest.raises(OSError, match="Unknown IPTC image compression"): + with Image.open(f): + pass + + +def test_getiptcinfo() -> None: + f = create_iptc_image() + with Image.open(f) as im: + assert IptcImagePlugin.getiptcinfo(im) == { + (3, 60): b"\x01\x00", + (3, 120): b"\x01", + (3, 20): b"\x01", + (3, 30): b"\x01", + } + + def test_getiptcinfo_jpg_none() -> None: # Arrange with hopper() as im: diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 51d518ae5..96e7f4239 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -330,8 +330,10 @@ class TestFileJpeg: # Reading with Image.open("Tests/images/exif_gps.jpg") as im: - exif = im._getexif() - assert exif[gps_index] == expected_exif_gps + assert isinstance(im, JpegImagePlugin.JpegImageFile) + exif_data = im._getexif() + assert exif_data is not None + assert exif_data[gps_index] == expected_exif_gps # Writing f = tmp_path / "temp.jpg" @@ -340,8 +342,10 @@ class TestFileJpeg: hopper().save(f, exif=exif) with Image.open(f) as reloaded: - exif = reloaded._getexif() - assert exif[gps_index] == expected_exif_gps + assert isinstance(reloaded, JpegImagePlugin.JpegImageFile) + exif_data = reloaded._getexif() + assert exif_data is not None + assert exif_data[gps_index] == expected_exif_gps def test_empty_exif_gps(self) -> None: with Image.open("Tests/images/empty_gps_ifd.jpg") as im: @@ -368,6 +372,7 @@ class TestFileJpeg: exifs = [] for i in range(2): with Image.open("Tests/images/exif-200dpcm.jpg") as im: + assert isinstance(im, JpegImagePlugin.JpegImageFile) exifs.append(im._getexif()) assert exifs[0] == exifs[1] @@ -401,13 +406,17 @@ class TestFileJpeg: } with Image.open("Tests/images/exif_gps.jpg") as im: + assert isinstance(im, JpegImagePlugin.JpegImageFile) exif = im._getexif() + assert exif is not None for tag, value in expected_exif.items(): assert value == exif[tag] def test_exif_gps_typeerror(self) -> None: with Image.open("Tests/images/exif_gps_typeerror.jpg") as im: + assert isinstance(im, JpegImagePlugin.JpegImageFile) + # Should not raise a TypeError im._getexif() @@ -487,7 +496,9 @@ class TestFileJpeg: def test_exif(self) -> None: with Image.open("Tests/images/pil_sample_rgb.jpg") as im: + assert isinstance(im, JpegImagePlugin.JpegImageFile) info = im._getexif() + assert info is not None assert info[305] == "Adobe Photoshop CS Macintosh" def test_get_child_images(self) -> None: @@ -690,11 +701,13 @@ class TestFileJpeg: def test_save_multiple_16bit_qtables(self) -> None: with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im: + assert isinstance(im, JpegImagePlugin.JpegImageFile) im2 = self.roundtrip(im, qtables="keep") assert im.quantization == im2.quantization def test_save_single_16bit_qtable(self) -> None: with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im: + assert isinstance(im, JpegImagePlugin.JpegImageFile) im2 = self.roundtrip(im, qtables={0: im.quantization[0]}) assert len(im2.quantization) == 1 assert im2.quantization[0] == im.quantization[0] @@ -898,7 +911,10 @@ class TestFileJpeg: # in contrast to normal 8 with Image.open("Tests/images/exif-ifd-offset.jpg") as im: # Act / Assert - assert im._getexif()[306] == "2017:03:13 23:03:09" + assert isinstance(im, JpegImagePlugin.JpegImageFile) + exif = im._getexif() + assert exif is not None + assert exif[306] == "2017:03:13 23:03:09" def test_multiple_exif(self) -> None: with Image.open("Tests/images/multiple_exif.jpg") as im: diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 958e2749f..4908496cf 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -355,6 +355,27 @@ class TestFileLibTiff(LibTiffTestCase): # Should not segfault im.save(outfile) + def test_whitepoint_tag( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path + ) -> None: + monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True) + + out = tmp_path / "temp.tif" + hopper().save(out, tiffinfo={318: (0.3127, 0.3289)}) + + with Image.open(out) as reloaded: + assert isinstance(reloaded, TiffImagePlugin.TiffImageFile) + assert reloaded.tag_v2[318] == pytest.approx((0.3127, 0.3289)) + + # Save tag by default + out = tmp_path / "temp2.tif" + with Image.open("Tests/images/rdf.tif") as im: + im.save(out) + + with Image.open(out) as reloaded: + assert isinstance(reloaded, TiffImagePlugin.TiffImageFile) + assert reloaded.tag_v2[318] == pytest.approx((0.3127, 0.3289999)) + def test_xmlpacket_tag( self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: @@ -365,8 +386,7 @@ class TestFileLibTiff(LibTiffTestCase): with Image.open(out) as reloaded: assert isinstance(reloaded, TiffImagePlugin.TiffImageFile) - if 700 in reloaded.tag_v2: - assert reloaded.tag_v2[700] == b"xmlpacket tag" + assert reloaded.tag_v2[700] == b"xmlpacket tag" def test_int_dpi(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: # issue #1765 diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 9262e6ca7..f947d1419 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -104,25 +104,27 @@ def test_exif(test_file: str) -> None: def test_frame_size() -> None: - # This image has been hexedited to contain a different size - # in the SOF marker of the second frame - with Image.open("Tests/images/sugarshack_frame_size.mpo") as im: - assert im.size == (640, 480) + with Image.open("Tests/images/frame_size.mpo") as im: + assert im.size == (56, 70) + im.load() im.seek(1) - assert im.size == (680, 480) + assert im.size == (349, 434) + im.load() im.seek(0) - assert im.size == (640, 480) + assert im.size == (56, 70) def test_ignore_frame_size() -> None: # Ignore the different size of the second frame # since this is not a "Large Thumbnail" image with Image.open("Tests/images/ignore_frame_size.mpo") as im: + assert isinstance(im, MpoImagePlugin.MpoImageFile) assert im.size == (64, 64) im.seek(1) + assert im.mpinfo is not None assert ( im.mpinfo[0xB002][1]["Attribute"]["MPType"] == "Multi-Frame Image: (Disparity)" @@ -155,6 +157,7 @@ def test_reload_exif_after_seek() -> None: @pytest.mark.parametrize("test_file", test_files) def test_mp(test_file: str) -> None: with Image.open(test_file) as im: + assert isinstance(im, MpoImagePlugin.MpoImageFile) mpinfo = im._getmp() assert mpinfo is not None assert mpinfo[45056] == b"0100" @@ -165,6 +168,7 @@ def test_mp_offset() -> None: # This image has been manually hexedited to have an IFD offset of 10 # in APP2 data, in contrast to normal 8 with Image.open("Tests/images/sugarshack_ifd_offset.mpo") as im: + assert isinstance(im, MpoImagePlugin.MpoImageFile) mpinfo = im._getmp() assert mpinfo is not None assert mpinfo[45056] == b"0100" @@ -182,6 +186,7 @@ def test_mp_no_data() -> None: @pytest.mark.parametrize("test_file", test_files) def test_mp_attribute(test_file: str) -> None: with Image.open(test_file) as im: + assert isinstance(im, MpoImagePlugin.MpoImageFile) mpinfo = im._getmp() assert mpinfo is not None for frame_number, mpentry in enumerate(mpinfo[0xB002]): diff --git a/Tests/test_file_pcd.py b/Tests/test_file_pcd.py index 81a316fc1..15dd7f116 100644 --- a/Tests/test_file_pcd.py +++ b/Tests/test_file_pcd.py @@ -1,10 +1,17 @@ from __future__ import annotations +from io import BytesIO + +import pytest + from PIL import Image +from .helper import assert_image_equal + def test_load_raw() -> None: with Image.open("Tests/images/hopper.pcd") as im: + assert im.size == (768, 512) im.load() # should not segfault. # Note that this image was created with a resized hopper @@ -15,3 +22,18 @@ def test_load_raw() -> None: # target = hopper().resize((768,512)) # assert_image_similar(im, target, 10) + + +@pytest.mark.parametrize("orientation", (1, 3)) +def test_rotated(orientation: int) -> None: + with open("Tests/images/hopper.pcd", "rb") as fp: + data = bytearray(fp.read()) + data[2048 + 1538] = orientation + f = BytesIO(data) + with Image.open(f) as im: + assert im.size == (512, 768) + + with Image.open("Tests/images/hopper.pcd") as expected: + assert_image_equal( + im, expected.rotate(90 if orientation == 1 else 270, expand=True) + ) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 0a51fd493..dc1077fed 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -229,7 +229,9 @@ class TestFilePng: assert_image(im, "RGBA", (162, 150)) # image has 124 unique alpha values - assert len(im.getchannel("A").getcolors()) == 124 + colors = im.getchannel("A").getcolors() + assert colors is not None + assert len(colors) == 124 def test_load_transparent_rgb(self) -> None: test_file = "Tests/images/rgb_trns.png" @@ -241,7 +243,9 @@ class TestFilePng: assert_image(im, "RGBA", (64, 64)) # image has 876 transparent pixels - assert im.getchannel("A").getcolors()[0][0] == 876 + colors = im.getchannel("A").getcolors() + assert colors is not None + assert colors[0][0] == 876 def test_save_p_transparent_palette(self, tmp_path: Path) -> None: in_file = "Tests/images/pil123p.png" @@ -262,7 +266,9 @@ class TestFilePng: assert_image(im, "RGBA", (162, 150)) # image has 124 unique alpha values - assert len(im.getchannel("A").getcolors()) == 124 + colors = im.getchannel("A").getcolors() + assert colors is not None + assert len(colors) == 124 def test_save_p_single_transparency(self, tmp_path: Path) -> None: in_file = "Tests/images/p_trns_single.png" @@ -285,7 +291,9 @@ class TestFilePng: assert im.getpixel((31, 31)) == (0, 255, 52, 0) # image has 876 transparent pixels - assert im.getchannel("A").getcolors()[0][0] == 876 + colors = im.getchannel("A").getcolors() + assert colors is not None + assert colors[0][0] == 876 def test_save_p_transparent_black(self, tmp_path: Path) -> None: # check if solid black image with full transparency @@ -313,7 +321,9 @@ class TestFilePng: assert im.info["transparency"] == 255 im_rgba = im.convert("RGBA") - assert im_rgba.getchannel("A").getcolors()[0][0] == num_transparent + colors = im_rgba.getchannel("A").getcolors() + assert colors is not None + assert colors[0][0] == num_transparent test_file = tmp_path / "temp.png" im.save(test_file) @@ -324,7 +334,9 @@ class TestFilePng: assert_image_equal(im, test_im) test_im_rgba = test_im.convert("RGBA") - assert test_im_rgba.getchannel("A").getcolors()[0][0] == num_transparent + colors = test_im_rgba.getchannel("A").getcolors() + assert colors is not None + assert colors[0][0] == num_transparent def test_save_rgb_single_transparency(self, tmp_path: Path) -> None: in_file = "Tests/images/caption_6_33_22.png" @@ -671,6 +683,9 @@ class TestFilePng: im.save(out, bits=4, save_all=save_all) with Image.open(out) as reloaded: + assert isinstance(reloaded, PngImagePlugin.PngImageFile) + assert reloaded.png is not None + assert reloaded.png.im_palette is not None assert len(reloaded.png.im_palette[1]) == 48 def test_plte_length(self, tmp_path: Path) -> None: @@ -681,6 +696,9 @@ class TestFilePng: im.save(out) with Image.open(out) as reloaded: + assert isinstance(reloaded, PngImagePlugin.PngImageFile) + assert reloaded.png is not None + assert reloaded.png.im_palette is not None assert len(reloaded.png.im_palette[1]) == 3 def test_getxmp(self) -> None: @@ -702,13 +720,17 @@ class TestFilePng: def test_exif(self) -> None: # With an EXIF chunk with Image.open("Tests/images/exif.png") as im: - exif = im._getexif() - assert exif[274] == 1 + assert isinstance(im, PngImagePlugin.PngImageFile) + exif_data = im._getexif() + assert exif_data is not None + assert exif_data[274] == 1 # With an ImageMagick zTXt chunk with Image.open("Tests/images/exif_imagemagick.png") as im: - exif = im._getexif() - assert exif[274] == 1 + assert isinstance(im, PngImagePlugin.PngImageFile) + exif_data = im._getexif() + assert exif_data is not None + assert exif_data[274] == 1 # Assert that info still can be extracted # when the image is no longer a PngImageFile instance @@ -717,8 +739,10 @@ class TestFilePng: # With a tEXt chunk with Image.open("Tests/images/exif_text.png") as im: - exif = im._getexif() - assert exif[274] == 1 + assert isinstance(im, PngImagePlugin.PngImageFile) + exif_data = im._getexif() + assert exif_data is not None + assert exif_data[274] == 1 # With XMP tags with Image.open("Tests/images/xmp_tags_orientation.png") as im: @@ -740,8 +764,10 @@ class TestFilePng: im.save(test_file, exif=im.getexif()) with Image.open(test_file) as reloaded: - exif = reloaded._getexif() - assert exif[274] == 1 + assert isinstance(reloaded, PngImagePlugin.PngImageFile) + exif_data = reloaded._getexif() + assert exif_data is not None + assert exif_data[274] == 1 @mark_if_feature_version( pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing" diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 68f2f9468..598e9a445 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -92,6 +92,13 @@ def test_16bit_pgm() -> None: assert_image_equal_tofile(im, "Tests/images/16_bit_binary_pgm.tiff") +def test_p4_save(tmp_path: Path) -> None: + with Image.open("Tests/images/hopper_1bit.pbm") as im: + filename = tmp_path / "temp.pbm" + im.save(filename) + assert_image_equal_tofile(im, filename) + + def test_16bit_pgm_write(tmp_path: Path) -> None: with Image.open("Tests/images/16_bit_binary.pgm") as im: filename = tmp_path / "temp.pgm" @@ -134,6 +141,12 @@ def test_pfm_big_endian(tmp_path: Path) -> None: assert_image_equal_tofile(im, filename) +def test_save_unsupported_mode(tmp_path: Path) -> None: + im = hopper("P") + with pytest.raises(OSError, match="cannot write mode P as PPM"): + im.save(tmp_path / "out.ppm") + + @pytest.mark.parametrize( "data", [ diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index bd39de2e1..bb8d3eefc 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -274,13 +274,17 @@ def test_save_l_transparency(tmp_path: Path) -> None: in_file = "Tests/images/la.tga" with Image.open(in_file) as im: assert im.mode == "LA" - assert im.getchannel("A").getcolors()[0][0] == num_transparent + colors = im.getchannel("A").getcolors() + assert colors is not None + assert colors[0][0] == num_transparent out = tmp_path / "temp.tga" im.save(out) with Image.open(out) as test_im: assert test_im.mode == "LA" - assert test_im.getchannel("A").getcolors()[0][0] == num_transparent + colors = test_im.getchannel("A").getcolors() + assert colors is not None + assert colors[0][0] == num_transparent assert_image_equal(im, test_im) diff --git a/Tests/test_file_wal.py b/Tests/test_file_wal.py index b15d79d61..549d47054 100644 --- a/Tests/test_file_wal.py +++ b/Tests/test_file_wal.py @@ -1,5 +1,7 @@ from __future__ import annotations +from io import BytesIO + from PIL import WalImageFile from .helper import assert_image_equal_tofile @@ -13,12 +15,22 @@ def test_open() -> None: assert im.format_description == "Quake2 Texture" assert im.mode == "P" assert im.size == (128, 128) + assert "next_name" not in im.info assert isinstance(im, WalImageFile.WalImageFile) assert_image_equal_tofile(im, "Tests/images/hopper_wal.png") +def test_next_name() -> None: + with open(TEST_FILE, "rb") as fp: + data = bytearray(fp.read()) + data[56:60] = b"Test" + f = BytesIO(data) + with WalImageFile.open(f) as im: + assert im.info["next_name"] == b"Test" + + def test_load() -> None: with WalImageFile.open(TEST_FILE) as im: px = im.load() diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index 503761374..600448fb9 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -4,13 +4,13 @@ from collections.abc import Generator from pathlib import Path import pytest -from packaging.version import parse as parse_version -from PIL import GifImagePlugin, Image, WebPImagePlugin, features +from PIL import GifImagePlugin, Image, WebPImagePlugin from .helper import ( assert_image_equal, assert_image_similar, + has_feature_version, is_big_endian, skip_unless_feature, ) @@ -53,11 +53,8 @@ def test_write_animation_L(tmp_path: Path) -> None: im.load() assert_image_similar(im, orig.convert("RGBA"), 32.9) - if is_big_endian(): - version = features.version_module("webp") - assert version is not None - if parse_version(version) < parse_version("1.2.2"): - pytest.skip("Fails with libwebp earlier than 1.2.2") + if is_big_endian() and not has_feature_version("webp", "1.2.2"): + pytest.skip("Fails with libwebp earlier than 1.2.2") orig.seek(orig.n_frames - 1) im.seek(im.n_frames - 1) orig.load() @@ -81,11 +78,8 @@ def test_write_animation_RGB(tmp_path: Path) -> None: assert_image_equal(im, frame1.convert("RGBA")) # Compare second frame to original - if is_big_endian(): - version = features.version_module("webp") - assert version is not None - if parse_version(version) < parse_version("1.2.2"): - pytest.skip("Fails with libwebp earlier than 1.2.2") + if is_big_endian() and not has_feature_version("webp", "1.2.2"): + pytest.skip("Fails with libwebp earlier than 1.2.2") im.seek(1) im.load() assert_image_equal(im, frame2.convert("RGBA")) diff --git a/Tests/test_file_webp_metadata.py b/Tests/test_file_webp_metadata.py index 7543d22da..3de412b83 100644 --- a/Tests/test_file_webp_metadata.py +++ b/Tests/test_file_webp_metadata.py @@ -22,11 +22,13 @@ except ImportError: def test_read_exif_metadata() -> None: file_path = "Tests/images/flower.webp" with Image.open(file_path) as image: + assert isinstance(image, WebPImagePlugin.WebPImageFile) assert image.format == "WEBP" exif_data = image.info.get("exif", None) assert exif_data exif = image._getexif() + assert exif is not None # Camera make assert exif[271] == "Canon" diff --git a/Tests/test_font_crash.py b/Tests/test_font_crash.py index b82340ef7..54bd2d183 100644 --- a/Tests/test_font_crash.py +++ b/Tests/test_font_crash.py @@ -9,7 +9,8 @@ from .helper import skip_unless_feature class TestFontCrash: def _fuzz_font(self, font: ImageFont.FreeTypeFont) -> None: - # from fuzzers.fuzz_font + # Copy of the code from fuzz_font() in Tests/oss-fuzz/fuzzers.py + # that triggered a problem when fuzzing font.getbbox("ABC") font.getmask("test text") with Image.new(mode="RGBA", size=(200, 200)) as im: diff --git a/Tests/test_image.py b/Tests/test_image.py index 83b027aa2..ac30f785c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -19,6 +19,7 @@ from PIL import ( ImageDraw, ImageFile, ImagePalette, + ImageShow, UnidentifiedImageError, features, ) @@ -283,33 +284,6 @@ class TestImage: assert item is not None assert item != num - def test_expand_x(self) -> None: - # Arrange - im = hopper() - orig_size = im.size - xmargin = 5 - - # Act - im = im._expand(xmargin) - - # Assert - assert im.size[0] == orig_size[0] + 2 * xmargin - assert im.size[1] == orig_size[1] + 2 * xmargin - - def test_expand_xy(self) -> None: - # Arrange - im = hopper() - orig_size = im.size - xmargin = 5 - ymargin = 3 - - # Act - im = im._expand(xmargin, ymargin) - - # Assert - assert im.size[0] == orig_size[0] + 2 * xmargin - assert im.size[1] == orig_size[1] + 2 * ymargin - def test_getbands(self) -> None: # Assert assert hopper("RGB").getbands() == ("R", "G", "B") @@ -388,6 +362,37 @@ class TestImage: assert img_colors is not None assert sorted(img_colors) == expected_colors + def test_alpha_composite_la(self) -> None: + # Arrange + expected_colors = sorted( + [ + (3300, (255, 255)), + (1156, (170, 192)), + (1122, (128, 255)), + (1089, (0, 0)), + (1122, (255, 128)), + (1122, (0, 128)), + (1089, (0, 255)), + ] + ) + + dst = Image.new("LA", size=(100, 100), color=(0, 255)) + draw = ImageDraw.Draw(dst) + draw.rectangle((0, 33, 100, 66), fill=(0, 128)) + draw.rectangle((0, 67, 100, 100), fill=(0, 0)) + src = Image.new("LA", size=(100, 100), color=(255, 255)) + draw = ImageDraw.Draw(src) + draw.rectangle((33, 0, 66, 100), fill=(255, 128)) + draw.rectangle((67, 0, 100, 100), fill=(255, 0)) + + # Act + img = Image.alpha_composite(dst, src) + + # Assert + img_colors = img.getcolors() + assert img_colors is not None + assert sorted(img_colors) == expected_colors + def test_alpha_inplace(self) -> None: src = Image.new("RGBA", (128, 128), "blue") @@ -922,6 +927,17 @@ class TestImage: reloaded_exif.load(exif.tobytes()) assert reloaded_exif.get_ifd(0x8769) == exif.get_ifd(0x8769) + def test_delete_ifd_tag(self) -> None: + with Image.open("Tests/images/flower.jpg") as im: + exif = im.getexif() + exif.get_ifd(0x8769) + assert 0x8769 in exif + del exif[0x8769] + + reloaded_exif = Image.Exif() + reloaded_exif.load(exif.tobytes()) + assert 0x8769 not in reloaded_exif + def test_exif_load_from_fp(self) -> None: with Image.open("Tests/images/flower.jpg") as im: data = im.info["exif"] @@ -1005,6 +1021,13 @@ class TestImage: with pytest.warns(DeprecationWarning, match="Image.Image.get_child_images"): assert im.get_child_images() == [] + def test_show(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(ImageShow, "_viewers", []) + + im = Image.new("RGB", (1, 1)) + with pytest.warns(DeprecationWarning, match="Image._show"): + Image._show(im) + @pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0))) def test_zero_tobytes(self, size: tuple[int, int]) -> None: im = Image.new("RGB", size) @@ -1076,6 +1099,12 @@ class TestImage: assert im.palette is not None assert im.palette.colors[(27, 35, 6, 214)] == 24 + def test_merge_pa(self) -> None: + p = hopper("P") + a = Image.new("L", p.size) + pa = Image.merge("PA", (p, a)) + assert p.getpalette() == pa.getpalette() + def test_constants(self) -> None: for enum in ( Image.Transpose, diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index ecbce3d6f..abb22f949 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -101,9 +101,8 @@ def test_fromarray_strides_without_tobytes() -> None: self.__array_interface__ = arr_params with pytest.raises(ValueError): - wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1)}) - with pytest.warns(DeprecationWarning, match="'mode' parameter"): - Image.fromarray(wrapped, "L") + wrapped = Wrapper({"shape": (1, 1), "strides": (1, 1), "typestr": "|u1"}) + Image.fromarray(wrapped, "L") def test_fromarray_palette() -> None: @@ -112,9 +111,16 @@ def test_fromarray_palette() -> None: a = numpy.array(i) # Act - with pytest.warns(DeprecationWarning, match="'mode' parameter"): - out = Image.fromarray(a, "P") + out = Image.fromarray(a, "P") # Assert that the Python and C palettes match assert out.palette is not None assert len(out.palette.colors) == len(out.im.getpalette()) / 3 + + +def test_deprecation() -> None: + a = numpy.array(im.convert("L")) + with pytest.warns( + DeprecationWarning, match="'mode' parameter for changing data types" + ): + Image.fromarray(a, "1") diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 33f844437..8d0ef4b22 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -97,6 +97,13 @@ def test_opaque() -> None: assert_image_equal(alpha, solid) +def test_rgba() -> None: + with Image.open("Tests/images/transparent.png") as im: + assert im.mode == "RGBA" + + assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5) + + def test_rgba_p() -> None: im = hopper("RGBA") im.putalpha(hopper("L")) @@ -107,11 +114,19 @@ def test_rgba_p() -> None: assert_image_similar(im, comparable, 20) -def test_rgba() -> None: - with Image.open("Tests/images/transparent.png") as im: - assert im.mode == "RGBA" +def test_rgba_pa() -> None: + im = hopper("RGBA").convert("PA").convert("RGB") + expected = hopper("RGB") - assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5) + assert_image_similar(im, expected, 9.3) + + +def test_pa() -> None: + im = hopper().convert("PA") + + palette = im.palette + assert palette is not None + assert palette.colors != {} def test_trns_p(tmp_path: Path) -> None: diff --git a/Tests/test_image_paste.py b/Tests/test_image_paste.py index 2cff6c893..37e4df103 100644 --- a/Tests/test_image_paste.py +++ b/Tests/test_image_paste.py @@ -124,6 +124,21 @@ class TestImagingPaste: im = im.crop((12, 23, im2.width + 12, im2.height + 23)) assert_image_equal(im, im2) + @pytest.mark.parametrize("y", [10, -10]) + @pytest.mark.parametrize("mode", ["L", "RGB"]) + @pytest.mark.parametrize("mask_mode", ["", "1", "L", "LA", "RGBa"]) + def test_image_self(self, y: int, mode: str, mask_mode: str) -> None: + im = getattr(self, "gradient_" + mode) + mask = Image.new(mask_mode, im.size, 0xFFFFFFFF) if mask_mode else None + + im_self = im.copy() + im_self.paste(im_self, (0, y), mask) + + im_copy = im.copy() + im_copy.paste(im_copy.copy(), (0, y), mask) + + assert_image_equal(im_self, im_copy) + @pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"]) def test_image_mask_1(self, mode: str) -> None: im = Image.new(mode, (200, 200), "white") diff --git a/Tests/test_image_putpalette.py b/Tests/test_image_putpalette.py index f2c447f71..661764b60 100644 --- a/Tests/test_image_putpalette.py +++ b/Tests/test_image_putpalette.py @@ -62,6 +62,7 @@ def test_putpalette_with_alpha_values() -> None: expected = im.convert("RGBA") palette = im.getpalette() + assert palette is not None transparency = im.info.pop("transparency") palette_with_alpha_values = [] diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index 6d313cb8c..e8b783ff3 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -1,11 +1,16 @@ from __future__ import annotations import pytest -from packaging.version import parse as parse_version -from PIL import Image, features +from PIL import Image -from .helper import assert_image_similar, hopper, is_ppc64le, skip_unless_feature +from .helper import ( + assert_image_similar, + has_feature_version, + hopper, + is_ppc64le, + skip_unless_feature, +) def test_sanity() -> None: @@ -23,11 +28,8 @@ def test_sanity() -> None: @skip_unless_feature("libimagequant") def test_libimagequant_quantize() -> None: image = hopper() - if is_ppc64le(): - version = features.version_feature("libimagequant") - assert version is not None - if parse_version(version) < parse_version("4"): - pytest.skip("Fails with libimagequant earlier than 4.0.0 on ppc64le") + if is_ppc64le() and not has_feature_version("libimagequant", "4"): + pytest.skip("Fails with libimagequant earlier than 4.0.0 on ppc64le") converted = image.quantize(100, Image.Quantize.LIBIMAGEQUANT) assert converted.mode == "P" assert_image_similar(converted.convert("RGB"), image, 15) @@ -116,6 +118,15 @@ def test_quantize_kmeans(method: Image.Quantize) -> None: im.quantize(kmeans=-1, method=method) +@skip_unless_feature("libimagequant") +def test_resize() -> None: + im = hopper().resize((100, 100)) + converted = im.quantize(100, Image.Quantize.LIBIMAGEQUANT) + colors = converted.getcolors() + assert colors is not None + assert len(colors) == 100 + + def test_colors() -> None: im = hopper() colors = 2 diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 406d965b4..790acee2a 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -1494,7 +1494,9 @@ def test_default_font_size() -> None: def draw_text() -> None: draw.text((0, 0), text, font_size=16) - assert_image_equal_tofile(im, "Tests/images/imagedraw_default_font_size.png") + assert_image_similar_tofile( + im, "Tests/images/imagedraw_default_font_size.png", 1 + ) check(draw_text) @@ -1513,7 +1515,9 @@ def test_default_font_size() -> None: def draw_multiline_text() -> None: draw.multiline_text((0, 0), text, font_size=16) - assert_image_equal_tofile(im, "Tests/images/imagedraw_default_font_size.png") + assert_image_similar_tofile( + im, "Tests/images/imagedraw_default_font_size.png", 1 + ) check(draw_multiline_text) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index d4dfb1b6d..7dfb3abf9 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -164,6 +164,11 @@ class TestImageFile: with pytest.raises(OSError): p.close() + def test_negative_offset(self) -> None: + with Image.open("Tests/images/raw_negative_stride.bin") as im: + with pytest.raises(ValueError, match="Tile offset cannot be negative"): + im.load() + def test_no_format(self) -> None: buf = BytesIO(b"\x00" * 255) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 4565d35ba..39ee9b9c9 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -19,6 +19,7 @@ from .helper import ( assert_image_equal, assert_image_equal_tofile, assert_image_similar_tofile, + has_feature_version, is_win32, skip_unless_feature, skip_unless_feature_version, @@ -492,6 +493,11 @@ def test_stroke_mask() -> None: assert mask.getpixel((42, 5)) == 255 +def test_load_invalid_file() -> None: + with pytest.raises(SyntaxError, match="Not a PILfont file"): + ImageFont.load("Tests/images/1_trns.png") + + def test_load_when_image_not_found() -> None: with tempfile.NamedTemporaryFile(delete=False) as tmp: pass @@ -549,7 +555,7 @@ def test_default_font() -> None: draw.text((10, 60), txt, font=larger_default_font) # Assert - assert_image_equal_tofile(im, "Tests/images/default_font_freetype.png") + assert_image_similar_tofile(im, "Tests/images/default_font_freetype.png", 0.13) @pytest.mark.parametrize("mode", ("", "1", "RGBA")) @@ -1055,7 +1061,10 @@ def test_colr(layout_engine: ImageFont.Layout) -> None: d.text((15, 5), "Bungee", font=font, embedded_color=True) - assert_image_similar_tofile(im, "Tests/images/colr_bungee.png", 21) + if has_feature_version("freetype2", "2.14.0"): + assert_image_similar_tofile(im, "Tests/images/colr_bungee.png", 6.1) + else: + assert_image_similar_tofile(im, "Tests/images/colr_bungee_older.png", 21) @skip_unless_feature_version("freetype2", "2.10.0") @@ -1071,7 +1080,7 @@ def test_colr_mask(layout_engine: ImageFont.Layout) -> None: d.text((15, 5), "Bungee", "black", font=font) - assert_image_similar_tofile(im, "Tests/images/colr_bungee_mask.png", 22) + assert_image_similar_tofile(im, "Tests/images/colr_bungee_mask.png", 14.1) def test_woff2(layout_engine: ImageFont.Layout) -> None: diff --git a/Tests/test_imagefontctl.py b/Tests/test_imagefontctl.py index 5954de874..633f6756b 100644 --- a/Tests/test_imagefontctl.py +++ b/Tests/test_imagefontctl.py @@ -7,6 +7,7 @@ from PIL import Image, ImageDraw, ImageFont from .helper import ( assert_image_equal_tofile, assert_image_similar_tofile, + has_feature_version, skip_unless_feature, ) @@ -104,11 +105,9 @@ def test_text_direction_ttb() -> None: im = Image.new(mode="RGB", size=(100, 300)) draw = ImageDraw.Draw(im) - try: - draw.text((0, 0), "English あい", font=ttf, fill=500, direction="ttb") - except ValueError as ex: - if str(ex) == "libraqm 0.7 or greater required for 'ttb' direction": - pytest.skip("libraqm 0.7 or greater not available") + if not has_feature_version("raqm", "0.7"): + pytest.skip("libraqm 0.7 or greater not available") + draw.text((0, 0), "English あい", font=ttf, fill=500, direction="ttb") target = "Tests/images/test_direction_ttb.png" assert_image_similar_tofile(im, target, 2.8) @@ -119,19 +118,17 @@ def test_text_direction_ttb_stroke() -> None: im = Image.new(mode="RGB", size=(100, 300)) draw = ImageDraw.Draw(im) - try: - draw.text( - (27, 27), - "あい", - font=ttf, - fill=500, - direction="ttb", - stroke_width=2, - stroke_fill="#0f0", - ) - except ValueError as ex: - if str(ex) == "libraqm 0.7 or greater required for 'ttb' direction": - pytest.skip("libraqm 0.7 or greater not available") + if not has_feature_version("raqm", "0.7"): + pytest.skip("libraqm 0.7 or greater not available") + draw.text( + (27, 27), + "あい", + font=ttf, + fill=500, + direction="ttb", + stroke_width=2, + stroke_fill="#0f0", + ) target = "Tests/images/test_direction_ttb_stroke.png" assert_image_similar_tofile(im, target, 19.4) @@ -186,7 +183,7 @@ def test_x_max_and_y_offset() -> None: draw.text((0, 0), "لح", font=ttf, fill=500) target = "Tests/images/test_x_max_and_y_offset.png" - assert_image_similar_tofile(im, target, 0.5) + assert_image_similar_tofile(im, target, 3.8) def test_language() -> None: @@ -219,14 +216,9 @@ def test_getlength( im = Image.new(mode, (1, 1), 0) d = ImageDraw.Draw(im) - try: - assert d.textlength(text, ttf, direction) == expected - except ValueError as ex: - if ( - direction == "ttb" - and str(ex) == "libraqm 0.7 or greater required for 'ttb' direction" - ): - pytest.skip("libraqm 0.7 or greater not available") + if direction == "ttb" and not has_feature_version("raqm", "0.7"): + pytest.skip("libraqm 0.7 or greater not available") + assert d.textlength(text, ttf, direction) == expected @pytest.mark.parametrize("mode", ("L", "1")) @@ -242,17 +234,12 @@ def test_getlength_combine(mode: str, direction: str, text: str) -> None: ttf = ImageFont.truetype("Tests/fonts/NotoSans-Regular.ttf", 48) - try: - target = ttf.getlength("ii", mode, direction) - actual = ttf.getlength(text, mode, direction) + if direction == "ttb" and not has_feature_version("raqm", "0.7"): + pytest.skip("libraqm 0.7 or greater not available") + target = ttf.getlength("ii", mode, direction) + actual = ttf.getlength(text, mode, direction) - assert actual == target - except ValueError as ex: - if ( - direction == "ttb" - and str(ex) == "libraqm 0.7 or greater required for 'ttb' direction" - ): - pytest.skip("libraqm 0.7 or greater not available") + assert actual == target @pytest.mark.parametrize("anchor", ("lt", "mm", "rb", "sm")) @@ -265,11 +252,9 @@ def test_anchor_ttb(anchor: str) -> None: d = ImageDraw.Draw(im) d.line(((0, 200), (200, 200)), "gray") d.line(((100, 0), (100, 400)), "gray") - try: - d.text((100, 200), text, fill="black", anchor=anchor, direction="ttb", font=f) - except ValueError as ex: - if str(ex) == "libraqm 0.7 or greater required for 'ttb' direction": - pytest.skip("libraqm 0.7 or greater not available") + if not has_feature_version("raqm", "0.7"): + pytest.skip("libraqm 0.7 or greater not available") + d.text((100, 200), text, fill="black", anchor=anchor, direction="ttb", font=f) assert_image_similar_tofile(im, path, 1) # fails at 5 @@ -310,10 +295,12 @@ combine_tests = ( # this tests various combining characters for anchor alignment and clipping @pytest.mark.parametrize( - "name, text, anchor, dir, epsilon", combine_tests, ids=[r[0] for r in combine_tests] + "name, text, anchor, direction, epsilon", + combine_tests, + ids=[r[0] for r in combine_tests], ) def test_combine( - name: str, text: str, dir: str | None, anchor: str | None, epsilon: float + name: str, text: str, direction: str | None, anchor: str | None, epsilon: float ) -> None: path = f"Tests/images/test_combine_{name}.png" f = ImageFont.truetype("Tests/fonts/NotoSans-Regular.ttf", 48) @@ -322,11 +309,9 @@ def test_combine( d = ImageDraw.Draw(im) d.line(((0, 200), (400, 200)), "gray") d.line(((200, 0), (200, 400)), "gray") - try: - d.text((200, 200), text, fill="black", anchor=anchor, direction=dir, font=f) - except ValueError as ex: - if str(ex) == "libraqm 0.7 or greater required for 'ttb' direction": - pytest.skip("libraqm 0.7 or greater not available") + if direction == "ttb" and not has_feature_version("raqm", "0.7"): + pytest.skip("libraqm 0.7 or greater not available") + d.text((200, 200), text, fill="black", anchor=anchor, direction=direction, font=f) assert_image_similar_tofile(im, path, epsilon) diff --git a/Tests/test_imagefontpil.py b/Tests/test_imagefontpil.py index 3eb98d379..8c1cb3f58 100644 --- a/Tests/test_imagefontpil.py +++ b/Tests/test_imagefontpil.py @@ -30,6 +30,14 @@ def test_default_font(font: ImageFont.ImageFont) -> None: assert_image_equal_tofile(im, "Tests/images/default_font.png") +def test_invalid_mode() -> None: + font = ImageFont.ImageFont() + fp = BytesIO() + with Image.open("Tests/images/hopper.png") as im: + with pytest.raises(TypeError, match="invalid font image mode"): + font._load_pilfont_data(fp, im) + + def test_without_freetype() -> None: original_core = ImageFont.core if features.check_module("freetype2"): diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index 515e29cea..ca192a809 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -7,7 +7,7 @@ import pytest from PIL import Image, ImageMorph, _imagingmorph -from .helper import assert_image_equal_tofile, hopper +from .helper import assert_image_equal_tofile, hopper, timeout_unless_slower_valgrind def string_to_img(image_string: str) -> Image.Image: @@ -266,16 +266,18 @@ def test_unknown_pattern() -> None: ImageMorph.LutBuilder(op_name="unknown") -def test_pattern_syntax_error() -> None: +@pytest.mark.parametrize( + "pattern", ("a pattern with a syntax error", "4:(" + "X" * 30000) +) +@timeout_unless_slower_valgrind(1) +def test_pattern_syntax_error(pattern: str) -> None: # Arrange lb = ImageMorph.LutBuilder(op_name="corner") - new_patterns = ["a pattern with a syntax error"] + new_patterns = [pattern] lb.add_patterns(new_patterns) # Act / Assert - with pytest.raises( - Exception, match='Syntax error in pattern "a pattern with a syntax error"' - ): + with pytest.raises(Exception, match='Syntax error in pattern "'): lb.build_lut() diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 9f2fd5ba2..27ac6f308 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -186,6 +186,21 @@ def test_palette(mode: str) -> None: ) +def test_rgba_palette() -> None: + im = Image.new("P", (1, 1)) + + red = (255, 0, 0, 255) + translucent_black = (0, 0, 0, 127) + im.putpalette(red + translucent_black, "RGBA") + + expanded_im = ImageOps.expand(im, 1, 1) + + palette = expanded_im.palette + assert palette is not None + assert palette.mode == "RGBA" + assert expanded_im.convert("RGBA").getpixel((0, 0)) == translucent_black + + def test_pil163() -> None: # Division by zero in equalize if < 255 pixels in image (@PIL163) diff --git a/Tests/test_imagesequence.py b/Tests/test_imagesequence.py index 7b9ac80bc..32da22e04 100644 --- a/Tests/test_imagesequence.py +++ b/Tests/test_imagesequence.py @@ -76,9 +76,14 @@ def test_consecutive() -> None: def test_palette_mmap() -> None: # Using mmap in ImageFile can require to reload the palette. with Image.open("Tests/images/multipage-mmap.tiff") as im: - color1 = im.getpalette()[:3] + palette = im.getpalette() + assert palette is not None + color1 = palette[:3] im.seek(0) - color2 = im.getpalette()[:3] + + palette = im.getpalette() + assert palette is not None + color2 = palette[:3] assert color1 == color2 diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index 7a2f58767..8d6731acc 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -59,15 +59,12 @@ def test_show(mode: str) -> None: assert ImageShow.show(im) -def test_show_without_viewers() -> None: - viewers = ImageShow._viewers - ImageShow._viewers = [] +def test_show_without_viewers(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(ImageShow, "_viewers", []) with hopper() as im: assert not ImageShow.show(im) - ImageShow._viewers = viewers - @pytest.mark.parametrize( "viewer", diff --git a/Tests/test_imagestat.py b/Tests/test_imagestat.py index 0dfbc5a2a..0baab7ce2 100644 --- a/Tests/test_imagestat.py +++ b/Tests/test_imagestat.py @@ -57,3 +57,13 @@ def test_constant() -> None: assert st.rms[0] == 128 assert st.var[0] == 0 assert st.stddev[0] == 0 + + +def test_zero_count() -> None: + im = Image.new("L", (0, 0)) + + st = ImageStat.Stat(im) + + assert st.mean == [0] + assert st.rms == [0] + assert st.var == [0] diff --git a/Tests/test_nanoarrow.py b/Tests/test_nanoarrow.py new file mode 100644 index 000000000..69980e719 --- /dev/null +++ b/Tests/test_nanoarrow.py @@ -0,0 +1,302 @@ +from __future__ import annotations + +import json +from typing import Any, NamedTuple + +import pytest + +from PIL import Image + +from .helper import ( + assert_deep_equal, + assert_image_equal, + hopper, + is_big_endian, +) + +TYPE_CHECKING = False +if TYPE_CHECKING: + import nanoarrow # type: ignore [import-not-found] +else: + nanoarrow = pytest.importorskip("nanoarrow", reason="Nanoarrow not installed") + +TEST_IMAGE_SIZE = (10, 10) + + +def _test_img_equals_pyarray( + img: Image.Image, arr: Any, mask: list[int] | None, elts_per_pixel: int = 1 +) -> None: + assert img.height * img.width * elts_per_pixel == len(arr) + px = img.load() + assert px is not None + if elts_per_pixel > 1 and mask is None: + # have to do element-wise comparison when we're comparing + # flattened r,g,b,a to a pixel. + mask = list(range(elts_per_pixel)) + for x in range(0, img.size[0], int(img.size[0] / 10)): + for y in range(0, img.size[1], int(img.size[1] / 10)): + if mask: + pixel = px[x, y] + assert isinstance(pixel, tuple) + for ix, elt in enumerate(mask): + if elts_per_pixel == 1: + assert pixel[ix] == arr[y * img.width + x].as_py()[elt] + else: + assert ( + pixel[ix] + == arr[(y * img.width + x) * elts_per_pixel + elt].as_py() + ) + else: + assert_deep_equal(px[x, y], arr[y * img.width + x].as_py()) + + +def _test_img_equals_int32_pyarray( + img: Image.Image, arr: Any, mask: list[int] | None, elts_per_pixel: int = 1 +) -> None: + assert img.height * img.width * elts_per_pixel == len(arr) + px = img.load() + assert px is not None + if mask is None: + # have to do element-wise comparison when we're comparing + # flattened rgba in an uint32 to a pixel. + mask = list(range(elts_per_pixel)) + for x in range(0, img.size[0], int(img.size[0] / 10)): + for y in range(0, img.size[1], int(img.size[1] / 10)): + pixel = px[x, y] + assert isinstance(pixel, tuple) + arr_pixel_int = arr[y * img.width + x].as_py() + arr_pixel_tuple = ( + arr_pixel_int % 256, + (arr_pixel_int // 256) % 256, + (arr_pixel_int // 256**2) % 256, + (arr_pixel_int // 256**3), + ) + if is_big_endian(): + arr_pixel_tuple = arr_pixel_tuple[::-1] + + for ix, elt in enumerate(mask): + assert pixel[ix] == arr_pixel_tuple[elt] + + +fl_uint8_4_type = nanoarrow.fixed_size_list( + value_type=nanoarrow.uint8(nullable=False), list_size=4, nullable=False +) + + +@pytest.mark.parametrize( + "mode, dtype, mask", + ( + ("L", nanoarrow.uint8(nullable=False), None), + ("I", nanoarrow.int32(nullable=False), None), + ("F", nanoarrow.float32(nullable=False), None), + ("LA", fl_uint8_4_type, [0, 3]), + ("RGB", fl_uint8_4_type, [0, 1, 2]), + ("RGBA", fl_uint8_4_type, None), + ("RGBX", fl_uint8_4_type, None), + ("CMYK", fl_uint8_4_type, None), + ("YCbCr", fl_uint8_4_type, [0, 1, 2]), + ("HSV", fl_uint8_4_type, [0, 1, 2]), + ), +) +def test_to_array(mode: str, dtype: nanoarrow, mask: list[int] | None) -> None: + img = hopper(mode) + + # Resize to non-square + img = img.crop((3, 0, 124, 127)) + assert img.size == (121, 127) + + arr = nanoarrow.Array(img) + _test_img_equals_pyarray(img, arr, mask) + assert arr.schema.type == dtype.type + assert arr.schema.nullable == dtype.nullable + + reloaded = Image.fromarrow(arr, mode, img.size) + assert_image_equal(img, reloaded) + + +def test_lifetime() -> None: + # valgrind shouldn't error out here. + # arrays should be accessible after the image is deleted. + + img = hopper("L") + + arr_1 = nanoarrow.Array(img) + arr_2 = nanoarrow.Array(img) + + del img + + assert sum(arr_1.iter_py()) > 0 + del arr_1 + + assert sum(arr_2.iter_py()) > 0 + del arr_2 + + +def test_lifetime2() -> None: + # valgrind shouldn't error out here. + # img should remain after the arrays are collected. + + img = hopper("L") + + arr_1 = nanoarrow.Array(img) + arr_2 = nanoarrow.Array(img) + + assert sum(arr_1.iter_py()) > 0 + del arr_1 + + assert sum(arr_2.iter_py()) > 0 + del arr_2 + + img2 = img.copy() + px = img2.load() + assert px # make mypy happy + assert isinstance(px[0, 0], int) + + +class DataShape(NamedTuple): + dtype: nanoarrow + # Strictly speaking, elt should be a pixel or pixel component, so + # list[uint8][4], float, int, uint32, uint8, etc. But more + # correctly, it should be exactly the dtype from the line above. + elt: Any + elts_per_pixel: int + + +UINT_ARR = DataShape( + dtype=fl_uint8_4_type, + elt=[1, 2, 3, 4], # array of 4 uint8 per pixel + elts_per_pixel=1, # only one array per pixel +) + +UINT = DataShape( + dtype=nanoarrow.uint8(), + elt=3, # one uint8, + elts_per_pixel=4, # but repeated 4x per pixel +) + +UINT32 = DataShape( + dtype=nanoarrow.uint32(), + elt=0xABCDEF45, # one packed int, doesn't fit in a int32 > 0x80000000 + elts_per_pixel=1, # one per pixel +) + +INT32 = DataShape( + dtype=nanoarrow.uint32(), + elt=0x12CDEF45, # one packed int + elts_per_pixel=1, # one per pixel +) + + +@pytest.mark.parametrize( + "mode, data_tp, mask", + ( + ("L", DataShape(nanoarrow.uint8(), 3, 1), None), + ("I", DataShape(nanoarrow.int32(), 1 << 24, 1), None), + ("F", DataShape(nanoarrow.float32(), 3.14159, 1), None), + ("LA", UINT_ARR, [0, 3]), + ("LA", UINT, [0, 3]), + ("RGB", UINT_ARR, [0, 1, 2]), + ("RGBA", UINT_ARR, None), + ("CMYK", UINT_ARR, None), + ("YCbCr", UINT_ARR, [0, 1, 2]), + ("HSV", UINT_ARR, [0, 1, 2]), + ("RGB", UINT, [0, 1, 2]), + ("RGBA", UINT, None), + ("CMYK", UINT, None), + ("YCbCr", UINT, [0, 1, 2]), + ("HSV", UINT, [0, 1, 2]), + ), +) +def test_fromarray(mode: str, data_tp: DataShape, mask: list[int] | None) -> None: + (dtype, elt, elts_per_pixel) = data_tp + + ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] + if dtype == fl_uint8_4_type: + tmp_arr = nanoarrow.Array( + elt * (ct_pixels * elts_per_pixel), schema=nanoarrow.uint8() + ) + c_array = nanoarrow.c_array_from_buffers( + dtype, ct_pixels, buffers=[], children=[tmp_arr] + ) + arr = nanoarrow.Array(c_array) + else: + arr = nanoarrow.Array( + nanoarrow.c_array([elt] * (ct_pixels * elts_per_pixel), schema=dtype) + ) + img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) + + _test_img_equals_pyarray(img, arr, mask, elts_per_pixel) + + +@pytest.mark.parametrize( + "mode, mask", + ( + ("LA", [0, 3]), + ("RGB", [0, 1, 2]), + ("RGBA", None), + ("CMYK", None), + ("YCbCr", [0, 1, 2]), + ("HSV", [0, 1, 2]), + ), +) +@pytest.mark.parametrize("data_tp", (UINT32, INT32)) +def test_from_int32array(mode: str, mask: list[int] | None, data_tp: DataShape) -> None: + (dtype, elt, elts_per_pixel) = data_tp + + ct_pixels = TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1] + arr = nanoarrow.Array( + nanoarrow.c_array([elt] * (ct_pixels * elts_per_pixel), schema=dtype) + ) + img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) + + _test_img_equals_int32_pyarray(img, arr, mask, elts_per_pixel) + + +@pytest.mark.parametrize( + "mode, metadata", + ( + ("LA", ["L", "X", "X", "A"]), + ("RGB", ["R", "G", "B", "X"]), + ("RGBX", ["R", "G", "B", "X"]), + ("RGBA", ["R", "G", "B", "A"]), + ("CMYK", ["C", "M", "Y", "K"]), + ("YCbCr", ["Y", "Cb", "Cr", "X"]), + ("HSV", ["H", "S", "V", "X"]), + ), +) +def test_image_nested_metadata(mode: str, metadata: list[str]) -> None: + img = hopper(mode) + + arr = nanoarrow.Array(img) + + assert arr.schema.value_type.metadata + assert arr.schema.value_type.metadata[b"image"] + + parsed_metadata = json.loads( + arr.schema.value_type.metadata[b"image"].decode("utf8") + ) + + assert "bands" in parsed_metadata + assert parsed_metadata["bands"] == metadata + + +@pytest.mark.parametrize( + "mode, metadata", + ( + ("L", ["L"]), + ("I", ["I"]), + ("F", ["F"]), + ), +) +def test_image_flat_metadata(mode: str, metadata: list[str]) -> None: + img = hopper(mode) + + arr = nanoarrow.Array(img) + + assert arr.schema.metadata + assert arr.schema.metadata[b"image"] + + parsed_metadata = json.loads(arr.schema.metadata[b"image"].decode("utf8")) + + assert "bands" in parsed_metadata + assert parsed_metadata["bands"] == metadata diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index ef54deeeb..f6acb3aff 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -28,15 +28,13 @@ def test_numpy_to_image() -> None: a = numpy.array(data, dtype=dtype) a.shape = TEST_IMAGE_SIZE i = Image.fromarray(a) - if list(i.getdata()) != data: - print("data mismatch for", dtype) + assert list(i.getdata()) == data else: data = list(range(100)) a = numpy.array([[x] * bands for x in data], dtype=dtype) a.shape = TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1], bands i = Image.fromarray(a) - if list(i.getchannel(0).getdata()) != list(range(100)): - print("data mismatch for", dtype) + assert list(i.getchannel(0).getdata()) == list(range(100)) return i # Check supported 1-bit integer formats diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index 8dad94fe0..a69504e78 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from typing import Any, NamedTuple import pytest @@ -244,3 +245,29 @@ def test_from_int32array(mode: str, data_tp: DataShape, mask: list[int] | None) img = Image.fromarrow(arr, mode, TEST_IMAGE_SIZE) _test_img_equals_int32_pyarray(img, arr, mask, elts_per_pixel) + + +@pytest.mark.parametrize( + "mode, metadata", + ( + ("LA", ["L", "X", "X", "A"]), + ("RGB", ["R", "G", "B", "X"]), + ("RGBX", ["R", "G", "B", "X"]), + ("RGBA", ["R", "G", "B", "A"]), + ("CMYK", ["C", "M", "Y", "K"]), + ("YCbCr", ["Y", "Cb", "Cr", "X"]), + ("HSV", ["H", "S", "V", "X"]), + ), +) +def test_image_metadata(mode: str, metadata: list[str]) -> None: + img = hopper(mode) + + arr = pyarrow.array(img) # type: ignore[call-overload] + + assert arr.type.field(0).metadata + assert arr.type.field(0).metadata[b"image"] + + parsed_metadata = json.loads(arr.type.field(0).metadata[b"image"].decode("utf8")) + + assert "bands" in parsed_metadata + assert parsed_metadata["bands"] == metadata diff --git a/Tests/test_pyroma.py b/Tests/test_pyroma.py index 35f3fd076..5871a7213 100644 --- a/Tests/test_pyroma.py +++ b/Tests/test_pyroma.py @@ -9,7 +9,7 @@ from PIL import __version__ pyroma = pytest.importorskip("pyroma", reason="Pyroma not installed") -def map_metadata_keys(metadata): +def map_metadata_keys(md): # Convert installed wheel metadata into canonical Core Metadata 2.4 format. # This was a utility method in pyroma 4.3.3; it was removed in 5.0. # This implementation is constructed from the relevant logic from @@ -17,8 +17,8 @@ def map_metadata_keys(metadata): # upstream to Pyroma as https://github.com/regebro/pyroma/pull/116, # so it may be possible to simplify this test in future. data = {} - for key in set(metadata.keys()): - value = metadata.get_all(key) + for key in set(md.keys()): + value = md.get_all(key) key = pyroma.projectdata.normalize(key) if len(value) == 1: diff --git a/checks/32bit_segfault_check.py b/checks/32bit_segfault_check.py old mode 100755 new mode 100644 index 06ed2ed2f..e277bc10a --- a/checks/32bit_segfault_check.py +++ b/checks/32bit_segfault_check.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 from __future__ import annotations import sys diff --git a/checks/check_imaging_leaks.py b/checks/check_imaging_leaks.py old mode 100755 new mode 100644 index a1d59ed9c..65090b6b6 --- a/checks/check_imaging_leaks.py +++ b/checks/check_imaging_leaks.py @@ -1,6 +1,6 @@ -#!/usr/bin/env python3 from __future__ import annotations +import sys from collections.abc import Callable from typing import Any @@ -8,12 +8,12 @@ import pytest from PIL import Image -from .helper import is_win32 - min_iterations = 100 max_iterations = 10000 -pytestmark = pytest.mark.skipif(is_win32(), reason="requires Unix or macOS") +pytestmark = pytest.mark.skipif( + sys.platform.startswith("win32"), reason="requires Unix or macOS" +) def _get_mem_usage() -> float: diff --git a/checks/check_j2k_leaks.py b/checks/check_j2k_leaks.py index bbe35b591..7103d502e 100644 --- a/checks/check_j2k_leaks.py +++ b/checks/check_j2k_leaks.py @@ -1,12 +1,11 @@ from __future__ import annotations +import sys from io import BytesIO import pytest -from PIL import Image - -from .helper import is_win32, skip_unless_feature +from PIL import Image, features # Limits for testing the leak mem_limit = 1024 * 1048576 @@ -15,8 +14,10 @@ iterations = int((mem_limit / stack_size) * 2) test_file = "Tests/images/rgb_trns_ycbc.jp2" pytestmark = [ - pytest.mark.skipif(is_win32(), reason="requires Unix or macOS"), - skip_unless_feature("jpg_2000"), + pytest.mark.skipif( + sys.platform.startswith("win32"), reason="requires Unix or macOS" + ), + pytest.mark.skipif(not features.check("jpg_2000"), reason="jpg_2000 not available"), ] diff --git a/checks/check_jpeg_leaks.py b/checks/check_jpeg_leaks.py index 2f42ad734..2c27ce1d5 100644 --- a/checks/check_jpeg_leaks.py +++ b/checks/check_jpeg_leaks.py @@ -1,10 +1,11 @@ from __future__ import annotations +import sys from io import BytesIO import pytest -from .helper import hopper, is_win32 +from PIL import Image iterations = 5000 @@ -18,7 +19,9 @@ valgrind --tool=massif python test-installed.py -s -v checks/check_jpeg_leaks.py """ -pytestmark = pytest.mark.skipif(is_win32(), reason="requires Unix or macOS") +pytestmark = pytest.mark.skipif( + sys.platform.startswith("win32"), reason="requires Unix or macOS" +) """ pre patch: @@ -112,10 +115,10 @@ standard_chrominance_qtable = ( ), ) def test_qtables_leak(qtables: tuple[tuple[int, ...]] | list[tuple[int, ...]]) -> None: - im = hopper("RGB") - for _ in range(iterations): - test_output = BytesIO() - im.save(test_output, "JPEG", qtables=qtables) + with Image.open("Tests/images/hopper.ppm") as im: + for _ in range(iterations): + test_output = BytesIO() + im.save(test_output, "JPEG", qtables=qtables) def test_exif_leak() -> None: @@ -173,12 +176,12 @@ def test_exif_leak() -> None: 0 +----------------------------------------------------------------------->Gi 0 11.33 """ - im = hopper("RGB") exif = b"12345678" * 4096 - for _ in range(iterations): - test_output = BytesIO() - im.save(test_output, "JPEG", exif=exif) + with Image.open("Tests/images/hopper.ppm") as im: + for _ in range(iterations): + test_output = BytesIO() + im.save(test_output, "JPEG", exif=exif) def test_base_save() -> None: @@ -207,8 +210,7 @@ def test_base_save() -> None: | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: 0 +----------------------------------------------------------------------->Gi 0 7.882""" - im = hopper("RGB") - - for _ in range(iterations): - test_output = BytesIO() - im.save(test_output, "JPEG") + with Image.open("Tests/images/hopper.ppm") as im: + for _ in range(iterations): + test_output = BytesIO() + im.save(test_output, "JPEG") diff --git a/checks/check_wheel.py b/checks/check_wheel.py index 937722c4b..f716c8498 100644 --- a/checks/check_wheel.py +++ b/checks/check_wheel.py @@ -4,7 +4,6 @@ import platform import sys from PIL import features -from Tests.helper import is_pypy def test_wheel_modules() -> None: @@ -48,8 +47,6 @@ def test_wheel_features() -> None: if sys.platform == "win32": expected_features.remove("xcb") - elif sys.platform == "darwin" and not is_pypy() and platform.processor() != "arm": - expected_features.remove("zlib_ng") elif sys.platform == "ios": # Can't distribute raqm due to licensing, and there's no system version; # fribidi and harfbuzz won't be available if raqm isn't available. diff --git a/depends/install_imagequant.sh b/depends/install_imagequant.sh index 88756f8f9..357214f1f 100755 --- a/depends/install_imagequant.sh +++ b/depends/install_imagequant.sh @@ -2,7 +2,7 @@ # install libimagequant archive_name=libimagequant -archive_version=4.3.4 +archive_version=4.4.0 archive=$archive_name-$archive_version diff --git a/depends/install_libavif.sh b/depends/install_libavif.sh index 26af8a36c..50ba01755 100755 --- a/depends/install_libavif.sh +++ b/depends/install_libavif.sh @@ -59,6 +59,6 @@ cmake \ "${LIBAVIF_CMAKE_FLAGS[@]}" \ . -sudo make install +make install popd diff --git a/depends/install_openjpeg.sh b/depends/install_openjpeg.sh index 1f8d78193..bc7c7c634 100755 --- a/depends/install_openjpeg.sh +++ b/depends/install_openjpeg.sh @@ -1,7 +1,7 @@ #!/bin/bash # install openjpeg -archive=openjpeg-2.5.3 +archive=openjpeg-2.5.4 ./download-and-extract.sh $archive https://raw.githubusercontent.com/python-pillow/pillow-depends/main/$archive.tar.gz diff --git a/depends/install_raqm.sh b/depends/install_raqm.sh index 5d862403e..33bb2d0a7 100755 --- a/depends/install_raqm.sh +++ b/depends/install_raqm.sh @@ -2,12 +2,12 @@ # install raqm -archive=libraqm-0.10.2 +archive=libraqm-0.10.3 ./download-and-extract.sh $archive https://raw.githubusercontent.com/python-pillow/pillow-depends/main/$archive.tar.gz pushd $archive -meson build --prefix=/usr && sudo ninja -C build install +meson build --prefix=/usr && ninja -C build install popd diff --git a/docs/deprecations.rst b/docs/deprecations.rst index 3f95cf7f5..cc5ac283f 100644 --- a/docs/deprecations.rst +++ b/docs/deprecations.rst @@ -35,8 +35,12 @@ Image.fromarray mode parameter .. deprecated:: 11.3.0 -The ``mode`` parameter in :py:meth:`~PIL.Image.fromarray()` has been deprecated. The -mode can be automatically determined from the object's shape and type instead. +Using the ``mode`` parameter in :py:meth:`~PIL.Image.fromarray()` was deprecated in +Pillow 11.3.0. In Pillow 12.0.0, this was partially reverted, and it is now only +deprecated when changing data types. Since pixel values do not contain information +about palettes or color spaces, the parameter can still be used to place grayscale L +mode data within a P mode image, or read RGB data as YCbCr for example. If omitted, the +mode will be automatically determined from the object's shape and type. Saving I mode images as PNG ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -61,6 +65,14 @@ ImageCms.ImageCmsProfile.product_name and .product_info ``.product_info`` attributes have been deprecated, and will be removed in Pillow 13 (2026-10-15). They have been set to ``None`` since Pillow 2.3.0. +Image._show +~~~~~~~~~~~ + +.. deprecated:: 12.0.0 + +``Image._show`` has been deprecated, and will be removed in Pillow 13 (2026-10-15). +Use :py:meth:`~PIL.ImageShow.show` instead. + Removed features ---------------- diff --git a/docs/handbook/third-party-plugins.rst b/docs/handbook/third-party-plugins.rst index a189a5773..1c7dfb5e9 100644 --- a/docs/handbook/third-party-plugins.rst +++ b/docs/handbook/third-party-plugins.rst @@ -11,7 +11,7 @@ Here is a list of PyPI projects that offer additional plugins: * :pypi:`heif-image-plugin`: Simple HEIF/HEIC images plugin, based on the pyheif library. * :pypi:`jxlpy`: Introduces reading and writing support for JPEG XL. * :pypi:`pillow-heif`: Python bindings to libheif for working with HEIF images. -* :pypi:`pillow-jpls`: Plugin for the JPEG-LS codec, based on the Charls JPEG-LS implemetation. Python bindings implemented using pybind11. +* :pypi:`pillow-jpls`: Plugin for the JPEG-LS codec, based on the Charls JPEG-LS implementation. Python bindings implemented using pybind11. * :pypi:`pillow-jxl-plugin`: Plugin for JPEG-XL, using Rust for bindings. * :pypi:`pillow-mbm`: Adds support for KSP's proprietary MBM texture format. * :pypi:`pillow-svg`: Implements basic SVG read support. Supports basic paths, shapes, and text. diff --git a/docs/installation/building-from-source.rst b/docs/installation/building-from-source.rst index 59c595742..6080d29af 100644 --- a/docs/installation/building-from-source.rst +++ b/docs/installation/building-from-source.rst @@ -44,7 +44,7 @@ Many of Pillow's features require external libraries: * **libtiff** provides compressed TIFF functionality - * Pillow has been tested with libtiff versions **4.0-4.7.0** + * Pillow has been tested with libtiff versions **4.0-4.7.1** * **libfreetype** provides type related services @@ -58,13 +58,13 @@ Many of Pillow's features require external libraries: * **openjpeg** provides JPEG 2000 functionality. * Pillow has been tested with openjpeg **2.0.0**, **2.1.0**, **2.3.1**, - **2.4.0**, **2.5.0**, **2.5.2** and **2.5.3**. + **2.4.0**, **2.5.0**, **2.5.2**, **2.5.3** and **2.5.4**. * Pillow does **not** support the earlier **1.5** series which ships with Debian Jessie. * **libimagequant** provides improved color quantization - * Pillow has been tested with libimagequant **2.6-4.3.4** + * Pillow has been tested with libimagequant **2.6-4.4.0** * Libimagequant is licensed GPLv3, which is more restrictive than the Pillow license, therefore we will not be distributing binaries with libimagequant support enabled. diff --git a/docs/installation/platform-support.rst b/docs/installation/platform-support.rst index 5cf0276d1..7999504fb 100644 --- a/docs/installation/platform-support.rst +++ b/docs/installation/platform-support.rst @@ -31,15 +31,17 @@ These platforms are built and tested for every change. +----------------------------------+----------------------------+---------------------+ | Debian 12 Bookworm | 3.11 | x86, x86-64 | +----------------------------------+----------------------------+---------------------+ +| Debian 13 Trixie | 3.13 | x86, x86-64 | ++----------------------------------+----------------------------+---------------------+ | Fedora 41 | 3.13 | x86-64 | +----------------------------------+----------------------------+---------------------+ | Fedora 42 | 3.13 | x86-64 | +----------------------------------+----------------------------+---------------------+ | Gentoo | 3.12 | x86-64 | +----------------------------------+----------------------------+---------------------+ -| macOS 13 Ventura | 3.10 | x86-64 | -+----------------------------------+----------------------------+---------------------+ -| macOS 14 Sonoma | 3.11, 3.12, 3.13, 3.14 | arm64 | +| macOS 15 Sequoia | 3.10 | x86-64 | +| +----------------------------+---------------------+ +| | 3.11, 3.12, 3.13, 3.14, | arm64 | | | PyPy3 | | +----------------------------------+----------------------------+---------------------+ | Ubuntu Linux 22.04 LTS (Jammy) | 3.10 | x86-64 | @@ -73,6 +75,8 @@ These platforms have been reported to work at the versions mentioned. | Operating system | | Tested Python | | Latest tested | | Tested | | | | versions | | Pillow version | | processors | +==================================+============================+==================+==============+ +| macOS 26 Tahoe | 3.9, 3.10, 3.11, 3.12, 3.13| 11.3.0 |arm | ++----------------------------------+----------------------------+------------------+--------------+ | macOS 15 Sequoia | 3.9, 3.10, 3.11, 3.12, 3.13| 11.3.0 |arm | | +----------------------------+------------------+ | | | 3.8 | 10.4.0 | | diff --git a/docs/reference/ImageDraw.rst b/docs/reference/ImageDraw.rst index 4a2223a40..6768a04c6 100644 --- a/docs/reference/ImageDraw.rst +++ b/docs/reference/ImageDraw.rst @@ -57,6 +57,43 @@ Color names See :ref:`color-names` for the color names supported by Pillow. +Alpha channel +^^^^^^^^^^^^^ + +By default, when drawing onto an existing image, the image's pixel values are simply +replaced by the new color:: + + im = Image.new("RGBA", (1, 1), (255, 0, 0)) + d = ImageDraw.Draw(im) + d.rectangle((0, 0, 1, 1), (0, 255, 0, 127)) + assert im.getpixel((0, 0)) == (0, 255, 0, 127) + + # Alpha channel values have no effect when drawing with RGB mode + im = Image.new("RGB", (1, 1), (255, 0, 0)) + d = ImageDraw.Draw(im) + d.rectangle((0, 0, 1, 1), (0, 255, 0, 127)) + assert im.getpixel((0, 0)) == (0, 255, 0) + +If you would like to combine translucent color with an RGB image, then initialize the +ImageDraw instance with the RGBA mode:: + + from PIL import Image, ImageDraw + im = Image.new("RGB", (1, 1), (255, 0, 0)) + d = ImageDraw.Draw(im, "RGBA") + d.rectangle((0, 0, 1, 1), (0, 255, 0, 127)) + assert im.getpixel((0, 0)) == (128, 127, 0) + +If you would like to combine translucent color with an RGBA image underneath, you will +need to combine multiple images:: + + from PIL import Image, ImageDraw + im = Image.new("RGBA", (1, 1), (255, 0, 0, 255)) + im2 = Image.new("RGBA", (1, 1)) + d = ImageDraw.Draw(im2) + d.rectangle((0, 0, 1, 1), (0, 255, 0, 127)) + im.paste(im2.convert("RGB"), mask=im2) + assert im.getpixel((0, 0)) == (128, 127, 0, 255) + Fonts ^^^^^ diff --git a/docs/reference/ImageFile.rst b/docs/reference/ImageFile.rst index 043559352..4c34ff812 100644 --- a/docs/reference/ImageFile.rst +++ b/docs/reference/ImageFile.rst @@ -74,5 +74,6 @@ Constants --------- .. autodata:: PIL.ImageFile.LOAD_TRUNCATED_IMAGES +.. autodata:: PIL.ImageFile.MAXBLOCK .. autodata:: PIL.ImageFile.ERRORS :annotation: diff --git a/docs/reference/ImageGrab.rst b/docs/reference/ImageGrab.rst index f6a2ec5bc..5c3a73fad 100644 --- a/docs/reference/ImageGrab.rst +++ b/docs/reference/ImageGrab.rst @@ -20,7 +20,9 @@ or the clipboard to a PIL image memory. used as a fallback if they are installed. To disable this behaviour, pass ``xdisplay=""`` instead. - .. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS), 7.1.0 (Linux) + .. versionadded:: 1.1.3 Windows support + .. versionadded:: 3.0.0 macOS support + .. versionadded:: 7.1.0 Linux support :param bbox: What region to copy. Default is the entire screen. On macOS, this is not increased to 2x for Retina screens, so the full @@ -53,7 +55,9 @@ or the clipboard to a PIL image memory. On Linux, ``wl-paste`` or ``xclip`` is required. - .. versionadded:: 1.1.4 (Windows), 3.3.0 (macOS), 9.4.0 (Linux) + .. versionadded:: 1.1.4 Windows support + .. versionadded:: 3.3.0 macOS support + .. versionadded:: 9.4.0 Linux support :return: On Windows, an image, a list of filenames, or None if the clipboard does not contain image data or filenames. diff --git a/docs/releasenotes/11.3.0.rst b/docs/releasenotes/11.3.0.rst index 409d50295..5c04a0373 100644 --- a/docs/releasenotes/11.3.0.rst +++ b/docs/releasenotes/11.3.0.rst @@ -29,6 +29,13 @@ Image.fromarray mode parameter The ``mode`` parameter in :py:meth:`~PIL.Image.fromarray()` has been deprecated. The mode can be automatically determined from the object's shape and type instead. +.. note:: + + Since pixel values do not contain information about palettes or color spaces, part + of this functionality was restored in Pillow 12.0.0. The parameter can be used to + place grayscale L mode data within a P mode image, or read RGB data as YCbCr for + example. + Saving I mode images as PNG ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/releasenotes/12.0.0.rst b/docs/releasenotes/12.0.0.rst index e21c243ea..fb5733944 100644 --- a/docs/releasenotes/12.0.0.rst +++ b/docs/releasenotes/12.0.0.rst @@ -1,19 +1,6 @@ 12.0.0 ------ -Security -======== - -TODO -^^^^ - -TODO - -:cve:`YYYY-XXXXX`: TODO -^^^^^^^^^^^^^^^^^^^^^^^ - -TODO - Backwards incompatible changes ============================== @@ -116,6 +103,12 @@ vulnerability introduced in FreeType 2.6 (:cve:`2020-15999`). Deprecations ============ +Image._show +^^^^^^^^^^^ + +``Image._show`` has been deprecated, and will be removed in Pillow 13 (2026-10-15). +Use :py:meth:`~PIL.ImageShow.show` instead. + ImageCms.ImageCmsProfile.product_name and .product_info ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -126,18 +119,10 @@ Pillow 13 (2026-10-15). They have been set to ``None`` since Pillow 2.3.0. API changes =========== -TODO -^^^^ +Image.alpha_composite: LA images +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -TODO - -API additions -============= - -TODO -^^^^ - -TODO +:py:meth:`~PIL.Image.alpha_composite` can now use LA images as well as RGBA. Other changes ============= @@ -150,3 +135,19 @@ others prepare for 3.14, and to ensure Pillow could be used immediately at the r of 3.14.0 final (2025-10-07, :pep:`745`). Pillow 12.0.0 now officially supports Python 3.14. + +Image.fromarray mode parameter +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +In Pillow 11.3.0, the ``mode`` parameter in :py:meth:`~PIL.Image.fromarray()` was +deprecated. Part of this functionality has been restored in Pillow 12.0.0. Since pixel +values do not contain information about palettes or color spaces, the parameter can be +used to place grayscale L mode data within a P mode image, or read RGB data as YCbCr +for example. + +ImageMorph operations must have length 1 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Valid ImageMorph operations are 4, N, 1 and M. By limiting the length to 1 character +within Pillow, long execution times can be avoided if a user provided long pattern +strings. Reported by `Jang Choi `__. diff --git a/pyproject.toml b/pyproject.toml index 137726a1c..0006ccd12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,6 +57,9 @@ optional-dependencies.mic = [ "olefile", ] optional-dependencies.test-arrow = [ + "arro3-compute", + "arro3-core", + "nanoarrow", "pyarrow", ] diff --git a/src/PIL/CurImagePlugin.py b/src/PIL/CurImagePlugin.py index b817dbc87..9c188e084 100644 --- a/src/PIL/CurImagePlugin.py +++ b/src/PIL/CurImagePlugin.py @@ -17,7 +17,7 @@ # from __future__ import annotations -from . import BmpImagePlugin, Image, ImageFile +from . import BmpImagePlugin, Image from ._binary import i16le as i16 from ._binary import i32le as i32 @@ -38,6 +38,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile): format_description = "Windows Cursor" def _open(self) -> None: + assert self.fp is not None offset = self.fp.tell() # check magic @@ -63,8 +64,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile): # patch up the bitmap height self._size = self.size[0], self.size[1] // 2 - d, e, o, a = self.tile[0] - self.tile[0] = ImageFile._Tile(d, (0, 0) + self.size, o, a) + self.tile = [self.tile[0]._replace(extents=(0, 0) + self.size)] # diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index 5e2ddad99..69f3062b4 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -354,6 +354,9 @@ class EpsImageFile(ImageFile.ImageFile): read_comment(s) elif bytes_mv[:9] == b"%%Trailer": trailer_reached = True + elif bytes_mv[:14] == b"%%BeginBinary:": + bytecount = int(byte_arr[14:bytes_read]) + self.fp.seek(bytecount, os.SEEK_CUR) bytes_read = 0 # A "BoundingBox" is always required, diff --git a/src/PIL/FliImagePlugin.py b/src/PIL/FliImagePlugin.py index 7c5bfeefa..da1e8e95c 100644 --- a/src/PIL/FliImagePlugin.py +++ b/src/PIL/FliImagePlugin.py @@ -30,7 +30,7 @@ from ._util import DeferredError def _accept(prefix: bytes) -> bool: return ( - len(prefix) >= 6 + len(prefix) >= 16 and i16(prefix, 4) in [0xAF11, 0xAF12] and i16(prefix, 14) in [0, 3] # flags ) @@ -48,8 +48,14 @@ class FliImageFile(ImageFile.ImageFile): def _open(self) -> None: # HEAD + assert self.fp is not None s = self.fp.read(128) - if not (_accept(s) and s[20:22] == b"\x00\x00"): + if not ( + _accept(s) + and s[20:22] == b"\x00" * 2 + and s[42:80] == b"\x00" * 38 + and s[88:] == b"\x00" * 40 + ): msg = "not an FLI/FLC file" raise SyntaxError(msg) @@ -77,8 +83,7 @@ class FliImageFile(ImageFile.ImageFile): if i16(s, 4) == 0xF100: # prefix chunk; ignore it - self.__offset = self.__offset + i32(s) - self.fp.seek(self.__offset) + self.fp.seek(self.__offset + i32(s)) s = self.fp.read(16) if i16(s, 4) == 0xF1FA: @@ -111,6 +116,7 @@ class FliImageFile(ImageFile.ImageFile): # load palette i = 0 + assert self.fp is not None for e in range(i16(self.fp.read(2))): s = self.fp.read(2) i = i + s[0] diff --git a/src/PIL/GbrImagePlugin.py b/src/PIL/GbrImagePlugin.py index f319d7e84..d69295363 100644 --- a/src/PIL/GbrImagePlugin.py +++ b/src/PIL/GbrImagePlugin.py @@ -54,7 +54,7 @@ class GbrImageFile(ImageFile.ImageFile): width = i32(self.fp.read(4)) height = i32(self.fp.read(4)) color_depth = i32(self.fp.read(4)) - if width <= 0 or height <= 0: + if width == 0 or height == 0: msg = "not a GIMP brush" raise SyntaxError(msg) if color_depth not in (1, 4): @@ -71,7 +71,7 @@ class GbrImageFile(ImageFile.ImageFile): raise SyntaxError(msg) self.info["spacing"] = i32(self.fp.read(4)) - comment = self.fp.read(comment_length)[:-1] + self.info["comment"] = self.fp.read(comment_length)[:-1] if color_depth == 1: self._mode = "L" @@ -80,8 +80,6 @@ class GbrImageFile(ImageFile.ImageFile): self._size = width, height - self.info["comment"] = comment - # Image might not be small Image._decompression_bomb_check(self.size) diff --git a/src/PIL/GribStubImagePlugin.py b/src/PIL/GribStubImagePlugin.py index 439fc5a3e..dfa798893 100644 --- a/src/PIL/GribStubImagePlugin.py +++ b/src/PIL/GribStubImagePlugin.py @@ -33,7 +33,7 @@ def register_handler(handler: ImageFile.StubHandler | None) -> None: def _accept(prefix: bytes) -> bool: - return prefix.startswith(b"GRIB") and prefix[7] == 1 + return len(prefix) >= 8 and prefix.startswith(b"GRIB") and prefix[7] == 1 class GribStubImageFile(ImageFile.StubImageFile): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index b7c185e0d..9d50812eb 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -103,7 +103,6 @@ try: raise ImportError(msg) except ImportError as v: - core = DeferredError.new(ImportError("The _imaging C module is not installed.")) # Explanations for ways that we know we might have an import error if str(v).startswith("Module use of python"): # The _imaging C module is present, but not compiled for @@ -1010,8 +1009,14 @@ class Image: new_im.info["transparency"] = transparency return new_im - if mode == "P" and self.mode == "RGBA": - return self.quantize(colors) + if self.mode == "RGBA": + if mode == "P": + return self.quantize(colors) + elif mode == "PA": + r, g, b, a = self.split() + rgb = merge("RGB", (r, g, b)) + p = rgb.quantize(colors) + return merge("PA", (p, a)) trns = None delete_trns = False @@ -1143,7 +1148,7 @@ class Image: raise ValueError(msg) from e new_im = self._new(im) - if mode == "P" and palette != Palette.ADAPTIVE: + if mode in ("P", "PA") and palette != Palette.ADAPTIVE: from . import ImagePalette new_im.palette = ImagePalette.ImagePalette("RGB", im.getpalette("RGB")) @@ -1337,12 +1342,6 @@ class Image: """ pass - def _expand(self, xmargin: int, ymargin: int | None = None) -> Image: - if ymargin is None: - ymargin = xmargin - self.load() - return self._new(self.im.expand(xmargin, ymargin)) - def filter(self, filter: ImageFilter.Filter | type[ImageFilter.Filter]) -> Image: """ Filters this image using the given filter. For a list of @@ -2071,9 +2070,7 @@ class Image: :param value: The pixel value. """ - if self.readonly: - self._copy() - self.load() + self._ensure_mutable() if ( self.mode in ("P", "PA") @@ -2633,7 +2630,9 @@ class Image: :param title: Optional title to use for the image window, where possible. """ - _show(self, title=title) + from . import ImageShow + + ImageShow.show(self, title) def split(self) -> tuple[Image, ...]: """ @@ -3258,19 +3257,10 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image: transferred. This means that P and PA mode images will lose their palette. :param obj: Object with array interface - :param mode: Optional mode to use when reading ``obj``. Will be determined from - type if ``None``. Deprecated. - - This will not be used to convert the data after reading, but will be used to - change how the data is read:: - - from PIL import Image - import numpy as np - a = np.full((1, 1), 300) - im = Image.fromarray(a, mode="L") - im.getpixel((0, 0)) # 44 - im = Image.fromarray(a, mode="RGB") - im.getpixel((0, 0)) # (44, 1, 0) + :param mode: Optional mode to use when reading ``obj``. Since pixel values do not + contain information about palettes or color spaces, this can be used to place + grayscale L mode data within a P mode image, or read RGB data as YCbCr for + example. See: :ref:`concept-modes` for general information about modes. :returns: An image object. @@ -3281,21 +3271,28 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image: shape = arr["shape"] ndim = len(shape) strides = arr.get("strides", None) - if mode is None: - try: - typekey = (1, 1) + shape[2:], arr["typestr"] - except KeyError as e: + try: + typekey = (1, 1) + shape[2:], arr["typestr"] + except KeyError as e: + if mode is not None: + typekey = None + color_modes: list[str] = [] + else: msg = "Cannot handle this data type" raise TypeError(msg) from e + if typekey is not None: try: - mode, rawmode = _fromarray_typemap[typekey] + typemode, rawmode, color_modes = _fromarray_typemap[typekey] except KeyError as e: typekey_shape, typestr = typekey msg = f"Cannot handle this data type: {typekey_shape}, {typestr}" raise TypeError(msg) from e - else: - deprecate("'mode' parameter", 13) + if mode is not None: + if mode != typemode and mode not in color_modes: + deprecate("'mode' parameter for changing data types", 13) rawmode = mode + else: + mode = typemode if mode in ["1", "L", "I", "P", "F"]: ndmax = 2 elif mode == "RGB": @@ -3392,29 +3389,29 @@ def fromqpixmap(im: ImageQt.QPixmap) -> ImageFile.ImageFile: _fromarray_typemap = { - # (shape, typestr) => mode, rawmode + # (shape, typestr) => mode, rawmode, color modes # first two members of shape are set to one - ((1, 1), "|b1"): ("1", "1;8"), - ((1, 1), "|u1"): ("L", "L"), - ((1, 1), "|i1"): ("I", "I;8"), - ((1, 1), "u2"): ("I", "I;16B"), - ((1, 1), "i2"): ("I", "I;16BS"), - ((1, 1), "u4"): ("I", "I;32B"), - ((1, 1), "i4"): ("I", "I;32BS"), - ((1, 1), "f4"): ("F", "F;32BF"), - ((1, 1), "f8"): ("F", "F;64BF"), - ((1, 1, 2), "|u1"): ("LA", "LA"), - ((1, 1, 3), "|u1"): ("RGB", "RGB"), - ((1, 1, 4), "|u1"): ("RGBA", "RGBA"), + ((1, 1), "|b1"): ("1", "1;8", []), + ((1, 1), "|u1"): ("L", "L", ["P"]), + ((1, 1), "|i1"): ("I", "I;8", []), + ((1, 1), "u2"): ("I", "I;16B", []), + ((1, 1), "i2"): ("I", "I;16BS", []), + ((1, 1), "u4"): ("I", "I;32B", []), + ((1, 1), "i4"): ("I", "I;32BS", []), + ((1, 1), "f4"): ("F", "F;32BF", []), + ((1, 1), "f8"): ("F", "F;64BF", []), + ((1, 1, 2), "|u1"): ("LA", "LA", ["La", "PA"]), + ((1, 1, 3), "|u1"): ("RGB", "RGB", ["YCbCr", "LAB", "HSV"]), + ((1, 1, 4), "|u1"): ("RGBA", "RGBA", ["RGBa", "RGBX", "CMYK"]), # shortcuts: - ((1, 1), f"{_ENDIAN}i4"): ("I", "I"), - ((1, 1), f"{_ENDIAN}f4"): ("F", "F"), + ((1, 1), f"{_ENDIAN}i4"): ("I", "I", []), + ((1, 1), f"{_ENDIAN}f4"): ("F", "F", []), } @@ -3571,9 +3568,8 @@ def alpha_composite(im1: Image, im2: Image) -> Image: """ Alpha composite im2 over im1. - :param im1: The first image. Must have mode RGBA. - :param im2: The second image. Must have mode RGBA, and the same size as - the first image. + :param im1: The first image. Must have mode RGBA or LA. + :param im2: The second image. Must have the same mode and size as the first image. :returns: An :py:class:`~PIL.Image.Image` object. """ @@ -3799,6 +3795,7 @@ def register_encoder(name: str, encoder: type[ImageFile.PyEncoder]) -> None: def _show(image: Image, **options: Any) -> None: from . import ImageShow + deprecate("Image._show", 13, "ImageShow.show") ImageShow.show(image, **options) @@ -4220,6 +4217,8 @@ class Exif(_ExifBase): del self._info[tag] else: del self._data[tag] + if tag in self._ifds: + del self._ifds[tag] def __iter__(self) -> Iterator[int]: keys = set(self._data) diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index a720ad40a..8bcf2d8ee 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -74,9 +74,7 @@ class ImageDraw: must be the same as the image mode. If omitted, the mode defaults to the mode of the image. """ - im.load() - if im.readonly: - im._copy() # make it writeable + im._ensure_mutable() blend = 0 if mode is None: mode = im.mode diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 27b27127e..a1d98bd51 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -46,6 +46,18 @@ if TYPE_CHECKING: logger = logging.getLogger(__name__) MAXBLOCK = 65536 +""" +By default, Pillow processes image data in blocks. This helps to prevent excessive use +of resources. Codecs may disable this behaviour with ``_pulls_fd`` or ``_pushes_fd``. + +When reading an image, this is the number of bytes to read at once. + +When writing an image, this is the number of bytes to write at once. +If the image width times 4 is greater, then that will be used instead. +Plugins may also set a greater number. + +User code may set this to another number. +""" SAFEBLOCK = 1024 * 1024 @@ -301,6 +313,9 @@ class ImageFile(Image.Image): and args[0] == self.mode and args[0] in Image._MAPMODES ): + if offset < 0: + msg = "Tile offset cannot be negative" + raise ValueError(msg) try: # use mmap, if possible import mmap diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index bf3f471f5..92eb763a5 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -125,11 +125,16 @@ class ImageFont: image.close() def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None: + # check image + if image.mode not in ("1", "L"): + msg = "invalid font image mode" + raise TypeError(msg) + # read PILfont header - if file.readline() != b"PILfont\n": + if file.read(8) != b"PILfont\n": msg = "Not a PILfont file" raise SyntaxError(msg) - file.readline().split(b";") + file.readline() self.info = [] # FIXME: should be a dictionary while True: s = file.readline() @@ -140,11 +145,6 @@ class ImageFont: # read PILfont metrics data = file.read(256 * 20) - # check image - if image.mode not in ("1", "L"): - msg = "invalid font image mode" - raise TypeError(msg) - image.load() self.font = Image.core.font(image.im, data) @@ -671,11 +671,7 @@ class FreeTypeFont: :returns: A list of the named styles in a variation font. :exception OSError: If the font is not a variation font. """ - try: - names = self.font.getvarnames() - except AttributeError as e: - msg = "FreeType 2.9.1 or greater is required" - raise NotImplementedError(msg) from e + names = self.font.getvarnames() return [name.replace(b"\x00", b"") for name in names] def set_variation_by_name(self, name: str | bytes) -> None: @@ -702,11 +698,7 @@ class FreeTypeFont: :returns: A list of the axes in a variation font. :exception OSError: If the font is not a variation font. """ - try: - axes = self.font.getvaraxes() - except AttributeError as e: - msg = "FreeType 2.9.1 or greater is required" - raise NotImplementedError(msg) from e + axes = self.font.getvaraxes() for axis in axes: if axis["name"]: axis["name"] = axis["name"].replace(b"\x00", b"") @@ -717,11 +709,7 @@ class FreeTypeFont: :param axes: A list of values for each axis. :exception OSError: If the font is not a variation font. """ - try: - self.font.setvaraxes(axes) - except AttributeError as e: - msg = "FreeType 2.9.1 or greater is required" - raise NotImplementedError(msg) from e + self.font.setvaraxes(axes) class TransposedFont: diff --git a/src/PIL/ImageMorph.py b/src/PIL/ImageMorph.py index f0a066b5b..bd70aff7b 100644 --- a/src/PIL/ImageMorph.py +++ b/src/PIL/ImageMorph.py @@ -150,7 +150,7 @@ class LutBuilder: # Parse and create symmetries of the patterns strings for p in self.patterns: - m = re.search(r"(\w*):?\s*\((.+?)\)\s*->\s*(\d)", p.replace("\n", "")) + m = re.search(r"(\w):?\s*\((.+?)\)\s*->\s*(\d)", p.replace("\n", "")) if not m: msg = 'Syntax error in pattern "' + p + '"' raise Exception(msg) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index da28854b5..42b10bd7b 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -499,14 +499,15 @@ def expand( height = top + image.size[1] + bottom color = _color(fill, image.mode) if image.palette: - palette = ImagePalette.ImagePalette(palette=image.getpalette()) + mode = image.palette.mode + palette = ImagePalette.ImagePalette(mode, image.getpalette(mode)) if isinstance(color, tuple) and (len(color) == 3 or len(color) == 4): color = palette.getcolor(color) else: palette = None out = Image.new(image.mode, (width, height), color) if palette: - out.putpalette(palette.palette) + out.putpalette(palette.palette, mode) out.paste(image, (left, top)) return out diff --git a/src/PIL/ImageStat.py b/src/PIL/ImageStat.py index 8bc504526..3a1044ba4 100644 --- a/src/PIL/ImageStat.py +++ b/src/PIL/ImageStat.py @@ -120,7 +120,7 @@ class Stat: @cached_property def mean(self) -> list[float]: """Average (arithmetic mean) pixel level for each band in the image.""" - return [self.sum[i] / self.count[i] for i in self.bands] + return [self.sum[i] / self.count[i] if self.count[i] else 0 for i in self.bands] @cached_property def median(self) -> list[int]: @@ -141,13 +141,20 @@ class Stat: @cached_property def rms(self) -> list[float]: """RMS (root-mean-square) for each band in the image.""" - return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands] + return [ + math.sqrt(self.sum2[i] / self.count[i]) if self.count[i] else 0 + for i in self.bands + ] @cached_property def var(self) -> list[float]: """Variance for each band in the image.""" return [ - (self.sum2[i] - (self.sum[i] ** 2.0) / self.count[i]) / self.count[i] + ( + (self.sum2[i] - (self.sum[i] ** 2.0) / self.count[i]) / self.count[i] + if self.count[i] + else 0 + ) for i in self.bands ] diff --git a/src/PIL/IptcImagePlugin.py b/src/PIL/IptcImagePlugin.py index b1fbb1bf1..c28f4dcc7 100644 --- a/src/PIL/IptcImagePlugin.py +++ b/src/PIL/IptcImagePlugin.py @@ -34,10 +34,6 @@ def _i(c: bytes) -> int: return i32((b"\0\0\0\0" + c)[-4:]) -def _i8(c: int | bytes) -> int: - return c if isinstance(c, int) else c[0] - - ## # Image plugin for IPTC/NAA datastreams. To read IPTC/NAA fields # from TIFF and JPEG files, use the getiptcinfo function. @@ -100,16 +96,18 @@ class IptcImageFile(ImageFile.ImageFile): # mode layers = self.info[(3, 60)][0] component = self.info[(3, 60)][1] - if (3, 65) in self.info: - id = self.info[(3, 65)][0] - 1 - else: - id = 0 if layers == 1 and not component: self._mode = "L" - elif layers == 3 and component: - self._mode = "RGB"[id] - elif layers == 4 and component: - self._mode = "CMYK"[id] + band = None + else: + if layers == 3 and component: + self._mode = "RGB" + elif layers == 4 and component: + self._mode = "CMYK" + if (3, 65) in self.info: + band = self.info[(3, 65)][0] - 1 + else: + band = 0 # size self._size = self.getint((3, 20)), self.getint((3, 30)) @@ -124,39 +122,44 @@ class IptcImageFile(ImageFile.ImageFile): # tile if tag == (8, 10): self.tile = [ - ImageFile._Tile("iptc", (0, 0) + self.size, offset, compression) + ImageFile._Tile("iptc", (0, 0) + self.size, offset, (compression, band)) ] def load(self) -> Image.core.PixelAccess | None: - if len(self.tile) != 1 or self.tile[0][0] != "iptc": - return ImageFile.ImageFile.load(self) + if self.tile: + args = self.tile[0].args + assert isinstance(args, tuple) + compression, band = args - offset, compression = self.tile[0][2:] + self.fp.seek(self.tile[0].offset) - self.fp.seek(offset) - - # Copy image data to temporary file - o = BytesIO() - if compression == "raw": - # To simplify access to the extracted file, - # prepend a PPM header - o.write(b"P5\n%d %d\n255\n" % self.size) - while True: - type, size = self.field() - if type != (8, 10): - break - while size > 0: - s = self.fp.read(min(size, 8192)) - if not s: + # Copy image data to temporary file + o = BytesIO() + if compression == "raw": + # To simplify access to the extracted file, + # prepend a PPM header + o.write(b"P5\n%d %d\n255\n" % self.size) + while True: + type, size = self.field() + if type != (8, 10): break - o.write(s) - size -= len(s) + while size > 0: + s = self.fp.read(min(size, 8192)) + if not s: + break + o.write(s) + size -= len(s) - with Image.open(o) as _im: - _im.load() - self.im = _im.im - self.tile = [] - return Image.Image.load(self) + with Image.open(o) as _im: + if band is not None: + bands = [Image.new("L", _im.size)] * Image.getmodebands(self.mode) + bands[band] = _im + _im = Image.merge(self.mode, bands) + else: + _im.load() + self.im = _im.im + self.tile = [] + return ImageFile.ImageFile.load(self) Image.register_open(IptcImageFile.format, IptcImageFile) diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 0d110035e..755ca648e 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -193,6 +193,8 @@ def SOF(self: JpegImageFile, marker: int) -> None: n = i16(self.fp.read(2)) - 2 s = ImageFile._safe_read(self.fp, n) self._size = i16(s, 3), i16(s, 1) + if self._im is not None and self.size != self.im.size: + self._im = None self.bits = s[0] if self.bits != 8: diff --git a/src/PIL/PcdImagePlugin.py b/src/PIL/PcdImagePlugin.py index 3aa249988..296f3775b 100644 --- a/src/PIL/PcdImagePlugin.py +++ b/src/PIL/PcdImagePlugin.py @@ -32,7 +32,7 @@ class PcdImageFile(ImageFile.ImageFile): assert self.fp is not None self.fp.seek(2048) - s = self.fp.read(2048) + s = self.fp.read(1539) if not s.startswith(b"PCD_"): msg = "not a PCD file" @@ -43,17 +43,21 @@ class PcdImageFile(ImageFile.ImageFile): if orientation == 1: self.tile_post_rotate = 90 elif orientation == 3: - self.tile_post_rotate = -90 + self.tile_post_rotate = 270 self._mode = "RGB" - self._size = 768, 512 # FIXME: not correct for rotated images! - self.tile = [ImageFile._Tile("pcd", (0, 0) + self.size, 96 * 2048)] + self._size = (512, 768) if orientation in (1, 3) else (768, 512) + self.tile = [ImageFile._Tile("pcd", (0, 0, 768, 512), 96 * 2048)] + + def load_prepare(self) -> None: + if self._im is None and self.tile_post_rotate: + self.im = Image.core.new(self.mode, (768, 512)) + ImageFile.ImageFile.load_prepare(self) def load_end(self) -> None: if self.tile_post_rotate: # Handle rotated PCDs - self.im = self.im.rotate(self.tile_post_rotate) - self._size = self.im.size + self.im = self.rotate(self.tile_post_rotate, expand=True).im # diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py index 458d586c4..6b16d5385 100644 --- a/src/PIL/PcxImagePlugin.py +++ b/src/PIL/PcxImagePlugin.py @@ -39,7 +39,7 @@ logger = logging.getLogger(__name__) def _accept(prefix: bytes) -> bool: - return prefix[0] == 10 and prefix[1] in [0, 2, 3, 5] + return len(prefix) >= 2 and prefix[0] == 10 and prefix[1] in [0, 2, 3, 5] ## diff --git a/src/PIL/PdfImagePlugin.py b/src/PIL/PdfImagePlugin.py index e9c20ddc1..5594c7e0f 100644 --- a/src/PIL/PdfImagePlugin.py +++ b/src/PIL/PdfImagePlugin.py @@ -27,7 +27,7 @@ import os import time from typing import IO, Any -from . import Image, ImageFile, ImageSequence, PdfParser, __version__, features +from . import Image, ImageFile, ImageSequence, PdfParser, features # # -------------------------------------------------------------------- @@ -221,7 +221,7 @@ def _save( existing_pdf.start_writing() existing_pdf.write_header() - existing_pdf.write_comment(f"created by Pillow {__version__} PDF driver") + existing_pdf.write_comment("created by Pillow PDF driver") # # pages diff --git a/src/PIL/PpmImagePlugin.py b/src/PIL/PpmImagePlugin.py index db34d107a..307bc97ff 100644 --- a/src/PIL/PpmImagePlugin.py +++ b/src/PIL/PpmImagePlugin.py @@ -47,7 +47,7 @@ MODES = { def _accept(prefix: bytes) -> bool: - return prefix.startswith(b"P") and prefix[1] in b"0123456fy" + return len(prefix) >= 2 and prefix.startswith(b"P") and prefix[1] in b"0123456fy" ## diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index c1741284b..de2ce066e 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -252,6 +252,7 @@ OPEN_INFO = { (II, 3, (1,), 1, (8,), ()): ("P", "P"), (MM, 3, (1,), 1, (8,), ()): ("P", "P"), (II, 3, (1,), 1, (8, 8), (0,)): ("P", "PX"), + (MM, 3, (1,), 1, (8, 8), (0,)): ("P", "PX"), (II, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"), (MM, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"), (II, 3, (1,), 2, (8,), ()): ("P", "P;R"), @@ -1177,6 +1178,7 @@ class TiffImageFile(ImageFile.ImageFile): """Open the first image in a TIFF file""" # Header + assert self.fp is not None ifh = self.fp.read(8) if ifh[2] == 43: ifh += self.fp.read(8) @@ -1343,6 +1345,7 @@ class TiffImageFile(ImageFile.ImageFile): # To be nice on memory footprint, if there's a # file descriptor, use that instead of reading # into a string in python. + assert self.fp is not None try: fp = hasattr(self.fp, "fileno") and self.fp.fileno() # flush the file descriptor, prevents error on pypy 2.4+ @@ -1936,9 +1939,10 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: types[tag] = TiffTags.LONG8 elif tag in ifd.tagtype: types[tag] = ifd.tagtype[tag] - elif not (isinstance(value, (int, float, str, bytes))): - continue - else: + elif isinstance(value, (int, float, str, bytes)) or ( + isinstance(value, tuple) + and all(isinstance(v, (int, float, IFDRational)) for v in value) + ): type = TiffTags.lookup(tag).type if type: types[tag] = type diff --git a/src/PIL/TiffTags.py b/src/PIL/TiffTags.py index 86adaa458..761aa3f6b 100644 --- a/src/PIL/TiffTags.py +++ b/src/PIL/TiffTags.py @@ -203,6 +203,11 @@ _tags_v2: dict[int, tuple[str, int, int] | tuple[str, int, int, dict[str, int]]] 531: ("YCbCrPositioning", SHORT, 1), 532: ("ReferenceBlackWhite", RATIONAL, 6), 700: ("XMP", BYTE, 0), + # Four private SGI tags + 32995: ("Matteing", SHORT, 1), + 32996: ("DataType", SHORT, 0), + 32997: ("ImageDepth", LONG, 1), + 32998: ("TileDepth", LONG, 1), 33432: ("Copyright", ASCII, 1), 33723: ("IptcNaaInfo", UNDEFINED, 1), 34377: ("PhotoshopInfo", BYTE, 0), diff --git a/src/PIL/WalImageFile.py b/src/PIL/WalImageFile.py index 87e32878b..5494f62e8 100644 --- a/src/PIL/WalImageFile.py +++ b/src/PIL/WalImageFile.py @@ -49,8 +49,7 @@ class WalImageFile(ImageFile.ImageFile): # strings are null-terminated self.info["name"] = header[:32].split(b"\0", 1)[0] - next_name = header[56 : 56 + 32].split(b"\0", 1)[0] - if next_name: + if next_name := header[56 : 56 + 32].split(b"\0", 1)[0]: self.info["next_name"] = next_name def load(self) -> Image.core.PixelAccess | None: diff --git a/src/PIL/WmfImagePlugin.py b/src/PIL/WmfImagePlugin.py index d569cb4b8..de714d337 100644 --- a/src/PIL/WmfImagePlugin.py +++ b/src/PIL/WmfImagePlugin.py @@ -80,7 +80,7 @@ class WmfStubImageFile(ImageFile.StubImageFile): format_description = "Windows Metafile" def _open(self) -> None: - # check placable header + # check placeable header s = self.fp.read(44) if s.startswith(b"\xd7\xcd\xc6\x9a\x00\x00"): diff --git a/src/_imaging.c b/src/_imaging.c index fbfc0e41a..6ab8e010d 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -2419,7 +2419,12 @@ _merge(PyObject *self, PyObject *args) { bands[3] = band3->image; } - return PyImagingNew(ImagingMerge(mode, bands)); + Imaging imOut = ImagingMerge(mode, bands); + if (!imOut) { + return NULL; + } + ImagingCopyPalette(imOut, bands[0]); + return PyImagingNew(imOut); } static PyObject * diff --git a/src/_imagingft.c b/src/_imagingft.c index 29d8e9e71..c9938fd3e 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -1221,8 +1221,6 @@ glyph_error: return NULL; } -#if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR > 9) || \ - (FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 9 && FREETYPE_PATCH == 1) static PyObject * font_getvarnames(FontObject *self) { int error; @@ -1432,7 +1430,6 @@ font_setvaraxes(FontObject *self, PyObject *args) { Py_RETURN_NONE; } -#endif static void font_dealloc(FontObject *self) { @@ -1451,13 +1448,10 @@ static PyMethodDef font_methods[] = { {"render", (PyCFunction)font_render, METH_VARARGS}, {"getsize", (PyCFunction)font_getsize, METH_VARARGS}, {"getlength", (PyCFunction)font_getlength, METH_VARARGS}, -#if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR > 9) || \ - (FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 9 && FREETYPE_PATCH == 1) {"getvarnames", (PyCFunction)font_getvarnames, METH_NOARGS}, {"getvaraxes", (PyCFunction)font_getvaraxes, METH_NOARGS}, {"setvarname", (PyCFunction)font_setvarname, METH_VARARGS}, {"setvaraxes", (PyCFunction)font_setvaraxes, METH_VARARGS}, -#endif {NULL, NULL} }; diff --git a/src/decode.c b/src/decode.c index 03db1ce35..e7a6e6323 100644 --- a/src/decode.c +++ b/src/decode.c @@ -870,8 +870,6 @@ PyImaging_Jpeg2KDecoderNew(PyObject *self, PyObject *args) { if (strcmp(format, "j2k") == 0) { codec_format = OPJ_CODEC_J2K; - } else if (strcmp(format, "jpt") == 0) { - codec_format = OPJ_CODEC_JPT; } else if (strcmp(format, "jp2") == 0) { codec_format = OPJ_CODEC_JP2; } else { diff --git a/src/encode.c b/src/encode.c index e56494036..a8da32318 100644 --- a/src/encode.c +++ b/src/encode.c @@ -922,6 +922,18 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { ); free(av); } + } else if (type == TIFF_RATIONAL) { + FLOAT32 *av; + /* malloc check ok, calloc checks for overflow */ + av = calloc(len, sizeof(FLOAT32)); + if (av) { + for (i = 0; i < len; i++) { + av[i] = (FLOAT32)PyFloat_AsDouble(PyTuple_GetItem(value, i)); + } + status = + ImagingLibTiffSetField(&encoder->state, (ttag_t)key_int, av); + free(av); + } } } else { if (type == TIFF_SHORT) { diff --git a/src/libImaging/Access.c b/src/libImaging/Access.c index 3db52377e..00aaaa405 100644 --- a/src/libImaging/Access.c +++ b/src/libImaging/Access.c @@ -12,8 +12,8 @@ #include "Imaging.h" /* use make_hash.py from the pillow-scripts repository to calculate these values */ -#define ACCESS_TABLE_SIZE 35 -#define ACCESS_TABLE_HASH 8940 +#define ACCESS_TABLE_SIZE 23 +#define ACCESS_TABLE_HASH 28677 static struct ImagingAccessInstance access_table[ACCESS_TABLE_SIZE]; @@ -64,7 +64,7 @@ static void get_pixel_16L(Imaging im, int x, int y, void *color) { UINT8 *in = (UINT8 *)&im->image[y][x + x]; #ifdef WORDS_BIGENDIAN - UINT16 out = in[0] + (in[1] << 8); + UINT16 out = in[0] + ((UINT16)in[1] << 8); memcpy(color, &out, sizeof(out)); #else memcpy(color, in, sizeof(UINT16)); @@ -77,7 +77,7 @@ get_pixel_16B(Imaging im, int x, int y, void *color) { #ifdef WORDS_BIGENDIAN memcpy(color, in, sizeof(UINT16)); #else - UINT16 out = in[1] + (in[0] << 8); + UINT16 out = in[1] + ((UINT16)in[0] << 8); memcpy(color, &out, sizeof(out)); #endif } @@ -87,28 +87,6 @@ get_pixel_32(Imaging im, int x, int y, void *color) { memcpy(color, &im->image32[y][x], sizeof(INT32)); } -static void -get_pixel_32L(Imaging im, int x, int y, void *color) { - UINT8 *in = (UINT8 *)&im->image[y][x * 4]; -#ifdef WORDS_BIGENDIAN - INT32 out = in[0] + (in[1] << 8) + (in[2] << 16) + (in[3] << 24); - memcpy(color, &out, sizeof(out)); -#else - memcpy(color, in, sizeof(INT32)); -#endif -} - -static void -get_pixel_32B(Imaging im, int x, int y, void *color) { - UINT8 *in = (UINT8 *)&im->image[y][x * 4]; -#ifdef WORDS_BIGENDIAN - memcpy(color, in, sizeof(INT32)); -#else - INT32 out = in[3] + (in[2] << 8) + (in[1] << 16) + (in[0] << 24); - memcpy(color, &out, sizeof(out)); -#endif -} - /* store individual pixel */ static void @@ -129,21 +107,6 @@ put_pixel_16B(Imaging im, int x, int y, const void *color) { out[1] = in[0]; } -static void -put_pixel_32L(Imaging im, int x, int y, const void *color) { - memcpy(&im->image8[y][x * 4], color, 4); -} - -static void -put_pixel_32B(Imaging im, int x, int y, const void *color) { - const char *in = color; - UINT8 *out = (UINT8 *)&im->image8[y][x * 4]; - out[0] = in[3]; - out[1] = in[2]; - out[2] = in[1]; - out[3] = in[0]; -} - static void put_pixel_32(Imaging im, int x, int y, const void *color) { memcpy(&im->image32[y][x], color, sizeof(INT32)); @@ -172,8 +135,6 @@ ImagingAccessInit(void) { #else ADD("I;16N", get_pixel_16L, put_pixel_16L); #endif - ADD("I;32L", get_pixel_32L, put_pixel_32L); - ADD("I;32B", get_pixel_32B, put_pixel_32B); ADD("F", get_pixel_32, put_pixel_32); ADD("P", get_pixel_8, put_pixel_8); ADD("PA", get_pixel_32_2bands, put_pixel_32); diff --git a/src/libImaging/AlphaComposite.c b/src/libImaging/AlphaComposite.c index 6d728f908..44c451679 100644 --- a/src/libImaging/AlphaComposite.c +++ b/src/libImaging/AlphaComposite.c @@ -25,13 +25,12 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) { int x, y; /* Check arguments */ - if (!imDst || !imSrc || strcmp(imDst->mode, "RGBA") || - imDst->type != IMAGING_TYPE_UINT8 || imDst->bands != 4) { + if (!imDst || !imSrc || + (strcmp(imDst->mode, "RGBA") && strcmp(imDst->mode, "LA"))) { return ImagingError_ModeError(); } - if (strcmp(imDst->mode, imSrc->mode) || imDst->type != imSrc->type || - imDst->bands != imSrc->bands || imDst->xsize != imSrc->xsize || + if (strcmp(imDst->mode, imSrc->mode) || imDst->xsize != imSrc->xsize || imDst->ysize != imSrc->ysize) { return ImagingError_Mismatch(); } diff --git a/src/libImaging/Arrow.c b/src/libImaging/Arrow.c index ccafe33b9..4519243ae 100644 --- a/src/libImaging/Arrow.c +++ b/src/libImaging/Arrow.c @@ -55,6 +55,98 @@ ReleaseExportedSchema(struct ArrowSchema *array) { // Mark array released array->release = NULL; } +char * +image_band_json(Imaging im) { + char *format = "{\"bands\": [\"%s\", \"%s\", \"%s\", \"%s\"]}"; + char *json; + // Bands can be 4 bands * 2 characters each + int len = strlen(format) + 8 + 1; + int err; + + json = calloc(1, len); + + if (!json) { + return NULL; + } + + err = PyOS_snprintf( + json, + len, + format, + im->band_names[0], + im->band_names[1], + im->band_names[2], + im->band_names[3] + ); + if (err < 0) { + return NULL; + } + return json; +} + +char * +single_band_json(Imaging im) { + char *format = "{\"bands\": [\"%s\"]}"; + char *json; + // Bands can be 1 band * (maybe but probably not) 2 characters each + int len = strlen(format) + 2 + 1; + int err; + + json = calloc(1, len); + + if (!json) { + return NULL; + } + + err = PyOS_snprintf(json, len, format, im->band_names[0]); + if (err < 0) { + return NULL; + } + return json; +} + +char * +assemble_metadata(const char *band_json) { + /* format is + int32: number of key/value pairs (noted N below) + int32: byte length of key 0 + key 0 (not null-terminated) + int32: byte length of value 0 + value 0 (not null-terminated) + ... + int32: byte length of key N - 1 + key N - 1 (not null-terminated) + int32: byte length of value N - 1 + value N - 1 (not null-terminated) + */ + const char *key = "image"; + INT32 key_len = strlen(key); + INT32 band_json_len = strlen(band_json); + + char *buf; + INT32 *dest_int; + char *dest; + + buf = calloc(1, key_len + band_json_len + 4 + 1 * 8); + if (!buf) { + return NULL; + } + + dest_int = (void *)buf; + + dest_int[0] = 1; + dest_int[1] = key_len; + dest_int += 2; + dest = (void *)dest_int; + memcpy(dest, key, key_len); + dest += key_len; + dest_int = (void *)dest; + dest_int[0] = band_json_len; + dest_int += 1; + memcpy(dest_int, band_json, band_json_len); + + return buf; +} int export_named_type(struct ArrowSchema *schema, char *format, char *name) { @@ -95,6 +187,8 @@ export_named_type(struct ArrowSchema *schema, char *format, char *name) { int export_imaging_schema(Imaging im, struct ArrowSchema *schema) { int retval = 0; + char *metadata; + char *band_json; if (strcmp(im->arrow_band_format, "") == 0) { return IMAGING_ARROW_INCOMPATIBLE_MODE; @@ -106,7 +200,17 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) { } if (im->bands == 1) { - return export_named_type(schema, im->arrow_band_format, im->band_names[0]); + retval = export_named_type(schema, im->arrow_band_format, im->band_names[0]); + if (retval != 0) { + return retval; + } + // band related metadata + band_json = single_band_json(im); + if (band_json) { + schema->metadata = assemble_metadata(band_json); + free(band_json); + } + return retval; } retval = export_named_type(schema, "+w:4", ""); @@ -117,13 +221,24 @@ export_imaging_schema(Imaging im, struct ArrowSchema *schema) { schema->n_children = 1; schema->children = calloc(1, sizeof(struct ArrowSchema *)); schema->children[0] = (struct ArrowSchema *)calloc(1, sizeof(struct ArrowSchema)); - retval = export_named_type(schema->children[0], im->arrow_band_format, "pixel"); + retval = export_named_type(schema->children[0], im->arrow_band_format, im->mode); if (retval != 0) { free(schema->children[0]); free(schema->children); schema->release(schema); return retval; } + + // band related metadata + band_json = image_band_json(im); + if (band_json) { + // adding the metadata to the child array. + // Accessible in pyarrow via pa.array(img).type.field(0).metadata + // adding it to the top level is not accessible. + schema->children[0]->metadata = assemble_metadata(band_json); + free(band_json); + } + return 0; } diff --git a/src/libImaging/BcnEncode.c b/src/libImaging/BcnEncode.c index 7a5072dde..861ae1c26 100644 --- a/src/libImaging/BcnEncode.c +++ b/src/libImaging/BcnEncode.c @@ -36,10 +36,9 @@ decode_565(UINT16 x) { static UINT16 encode_565(rgba item) { - UINT8 r, g, b; - r = item.color[0] >> (8 - 5); - g = item.color[1] >> (8 - 6); - b = item.color[2] >> (8 - 5); + UINT16 r = item.color[0] >> (8 - 5); + UINT8 g = item.color[1] >> (8 - 6); + UINT8 b = item.color[2] >> (8 - 5); return (r << (5 + 6)) | (g << 5) | b; } @@ -157,7 +156,8 @@ encode_bc1_color(Imaging im, ImagingCodecState state, UINT8 *dst, int separate_a static void encode_bc2_block(Imaging im, ImagingCodecState state, UINT8 *dst) { int i, j; - UINT8 block[16], current_alpha; + UINT8 block[16]; + UINT32 current_alpha; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { int x = state->x + i * im->pixelsize; diff --git a/src/libImaging/FliDecode.c b/src/libImaging/FliDecode.c index 130ecb7f7..44994823e 100644 --- a/src/libImaging/FliDecode.c +++ b/src/libImaging/FliDecode.c @@ -16,9 +16,11 @@ #include "Imaging.h" -#define I16(ptr) ((ptr)[0] + ((ptr)[1] << 8)) +#define I16(ptr) ((ptr)[0] + ((int)(ptr)[1] << 8)) -#define I32(ptr) ((ptr)[0] + ((ptr)[1] << 8) + ((ptr)[2] << 16) + ((ptr)[3] << 24)) +#define I32(ptr) \ + ((ptr)[0] + ((INT32)(ptr)[1] << 8) + ((INT32)(ptr)[2] << 16) + \ + ((INT32)(ptr)[3] << 24)) #define ERR_IF_DATA_OOB(offset) \ if ((data + (offset)) > ptr + bytes) { \ diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index d430893dd..e50bd7140 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -212,7 +212,7 @@ ImagingGetExtrema(Imaging im, void *extrema) { UINT16 v; UINT8 *pixel = *im->image8; #ifdef WORDS_BIGENDIAN - v = pixel[0] + (pixel[1] << 8); + v = pixel[0] + ((UINT16)pixel[1] << 8); #else memcpy(&v, pixel, sizeof(v)); #endif @@ -221,7 +221,7 @@ ImagingGetExtrema(Imaging im, void *extrema) { for (x = 0; x < im->xsize; x++) { pixel = (UINT8 *)im->image[y] + x * sizeof(v); #ifdef WORDS_BIGENDIAN - v = pixel[0] + (pixel[1] << 8); + v = pixel[0] + ((UINT16)pixel[1] << 8); #else memcpy(&v, pixel, sizeof(v)); #endif diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index bfe67d462..5d85ea73e 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -148,7 +148,7 @@ struct ImagingAccessInstance { struct ImagingHistogramInstance { /* Format */ char mode[IMAGING_MODE_LENGTH]; /* Band names (of corresponding source image) */ - int bands; /* Number of bands (1, 3, or 4) */ + int bands; /* Number of bands (1, 2, 3, or 4) */ /* Data */ long *histogram; /* Histogram (bands*256 longs) */ diff --git a/src/libImaging/Palette.c b/src/libImaging/Palette.c index 78916bca5..da1d80504 100644 --- a/src/libImaging/Palette.c +++ b/src/libImaging/Palette.c @@ -148,7 +148,7 @@ ImagingPaletteDelete(ImagingPalette palette) { #define BOX 8 -#define BOXVOLUME BOX *BOX *BOX +#define BOXVOLUME BOX * BOX * BOX void ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) { diff --git a/src/libImaging/Paste.c b/src/libImaging/Paste.c index 86085942a..9942f9c1c 100644 --- a/src/libImaging/Paste.c +++ b/src/libImaging/Paste.c @@ -23,6 +23,18 @@ #include "Imaging.h" +#define PREPARE_PASTE_LOOP() \ + int y, y_end, offset; \ + if (imOut == imIn && dy > sy) { \ + y = ysize - 1; \ + y_end = -1; \ + offset = -1; \ + } else { \ + y = 0; \ + y_end = ysize; \ + offset = 1; \ + } + static inline void paste( Imaging imOut, @@ -37,14 +49,13 @@ paste( ) { /* paste opaque region */ - int y; - dx *= pixelsize; sx *= pixelsize; xsize *= pixelsize; - for (y = 0; y < ysize; y++) { + PREPARE_PASTE_LOOP(); + for (; y != y_end; y += offset) { memcpy(imOut->image[y + dy] + dx, imIn->image[y + sy] + sx, xsize); } } @@ -64,12 +75,13 @@ paste_mask_1( ) { /* paste with mode "1" mask */ - int x, y; + int x; + PREPARE_PASTE_LOOP(); if (imOut->image8) { int in_i16 = strncmp(imIn->mode, "I;16", 4) == 0; int out_i16 = strncmp(imOut->mode, "I;16", 4) == 0; - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { UINT8 *out = imOut->image8[y + dy] + dx; if (out_i16) { out += dx; @@ -97,7 +109,7 @@ paste_mask_1( } } else { - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { INT32 *out = imOut->image32[y + dy] + dx; INT32 *in = imIn->image32[y + sy] + sx; UINT8 *mask = imMask->image8[y + sy] + sx; @@ -126,11 +138,12 @@ paste_mask_L( ) { /* paste with mode "L" matte */ - int x, y; + int x; unsigned int tmp1; + PREPARE_PASTE_LOOP(); if (imOut->image8) { - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { UINT8 *out = imOut->image8[y + dy] + dx; UINT8 *in = imIn->image8[y + sy] + sx; UINT8 *mask = imMask->image8[y + sy] + sx; @@ -141,7 +154,7 @@ paste_mask_L( } } else { - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { UINT8 *out = (UINT8 *)(imOut->image32[y + dy] + dx); UINT8 *in = (UINT8 *)(imIn->image32[y + sy] + sx); UINT8 *mask = (UINT8 *)(imMask->image8[y + sy] + sx); @@ -174,11 +187,12 @@ paste_mask_RGBA( ) { /* paste with mode "RGBA" matte */ - int x, y; + int x; unsigned int tmp1; + PREPARE_PASTE_LOOP(); if (imOut->image8) { - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { UINT8 *out = imOut->image8[y + dy] + dx; UINT8 *in = imIn->image8[y + sy] + sx; UINT8 *mask = (UINT8 *)imMask->image[y + sy] + sx * 4 + 3; @@ -189,7 +203,7 @@ paste_mask_RGBA( } } else { - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { UINT8 *out = (UINT8 *)(imOut->image32[y + dy] + dx); UINT8 *in = (UINT8 *)(imIn->image32[y + sy] + sx); UINT8 *mask = (UINT8 *)(imMask->image32[y + sy] + sx); @@ -222,11 +236,12 @@ paste_mask_RGBa( ) { /* paste with mode "RGBa" matte */ - int x, y; + int x; unsigned int tmp1; + PREPARE_PASTE_LOOP(); if (imOut->image8) { - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { UINT8 *out = imOut->image8[y + dy] + dx; UINT8 *in = imIn->image8[y + sy] + sx; UINT8 *mask = (UINT8 *)imMask->image[y + sy] + sx * 4 + 3; @@ -237,7 +252,7 @@ paste_mask_RGBa( } } else { - for (y = 0; y < ysize; y++) { + for (; y != y_end; y += offset) { UINT8 *out = (UINT8 *)(imOut->image32[y + dy] + dx); UINT8 *in = (UINT8 *)(imIn->image32[y + sy] + sx); UINT8 *mask = (UINT8 *)(imMask->image32[y + sy] + sx); diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index a489a882d..2ad990227 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -1745,19 +1745,23 @@ ImagingQuantize(Imaging im, int colors, int mode, int kmeans) { for (i = y = 0; y < im->ysize; y++) { for (x = 0; x < im->xsize; x++, i++) { p[i].v = im->image32[y][x]; - if (withAlpha && p[i].c.a == 0) { - if (transparency == 0) { - transparency = 1; - r = p[i].c.r; - g = p[i].c.g; - b = p[i].c.b; - } else { - /* Set all subsequent transparent pixels - to the same colour as the first */ - p[i].c.r = r; - p[i].c.g = g; - p[i].c.b = b; + if (withAlpha) { + if (p[i].c.a == 0) { + if (transparency == 0) { + transparency = 1; + r = p[i].c.r; + g = p[i].c.g; + b = p[i].c.b; + } else { + /* Set all subsequent transparent pixels + to the same colour as the first */ + p[i].c.r = r; + p[i].c.g = g; + p[i].c.b = b; + } } + } else { + p[i].c.a = 255; } } } diff --git a/src/libImaging/SgiRleDecode.c b/src/libImaging/SgiRleDecode.c index e60468990..a562f582c 100644 --- a/src/libImaging/SgiRleDecode.c +++ b/src/libImaging/SgiRleDecode.c @@ -22,7 +22,8 @@ static void read4B(UINT32 *dest, UINT8 *buf) { - *dest = (UINT32)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]); + *dest = ((UINT32)buf[0] << 24) | ((UINT32)buf[1] << 16) | ((UINT32)buf[2] << 8) | + buf[3]; } /* diff --git a/src/thirdparty/raqm/COPYING b/src/thirdparty/raqm/COPYING index 97e2489b7..964318a8a 100644 --- a/src/thirdparty/raqm/COPYING +++ b/src/thirdparty/raqm/COPYING @@ -1,7 +1,7 @@ The MIT License (MIT) Copyright © 2015 Information Technology Authority (ITA) -Copyright © 2016-2023 Khaled Hosny +Copyright © 2016-2025 Khaled Hosny Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/thirdparty/raqm/NEWS b/src/thirdparty/raqm/NEWS index e8bf32e0b..fb432cffb 100644 --- a/src/thirdparty/raqm/NEWS +++ b/src/thirdparty/raqm/NEWS @@ -1,3 +1,19 @@ +Overview of changes leading to 0.10.3 +Tuesday, August 5, 2025 +==================================== + +Fix raqm_set_text_utf8/utf16 reading beyond len for multibyte. + +Support building against SheenBidi 2.9. + +Fix deprecation warning with latest HarfBuzz. + +Overview of changes leading to 0.10.2 +Sunday, September 22, 2024 +==================================== + +Fix Unicode codepoint conversion from UTF-16. + Overview of changes leading to 0.10.1 Wednesday, April 12, 2023 ==================================== diff --git a/src/thirdparty/raqm/raqm-version.h b/src/thirdparty/raqm/raqm-version.h index 62d2d2064..f2dd61cf6 100644 --- a/src/thirdparty/raqm/raqm-version.h +++ b/src/thirdparty/raqm/raqm-version.h @@ -33,9 +33,9 @@ #define RAQM_VERSION_MAJOR 0 #define RAQM_VERSION_MINOR 10 -#define RAQM_VERSION_MICRO 1 +#define RAQM_VERSION_MICRO 3 -#define RAQM_VERSION_STRING "0.10.1" +#define RAQM_VERSION_STRING "0.10.3" #define RAQM_VERSION_ATLEAST(major,minor,micro) \ ((major)*10000+(minor)*100+(micro) <= \ diff --git a/src/thirdparty/raqm/raqm.c b/src/thirdparty/raqm/raqm.c index 2b331e1af..9ecc5cac8 100644 --- a/src/thirdparty/raqm/raqm.c +++ b/src/thirdparty/raqm/raqm.c @@ -30,7 +30,11 @@ #include #ifdef RAQM_SHEENBIDI +#ifdef RAQM_SHEENBIDI_GT_2_9 +#include +#else #include +#endif #else #ifdef HAVE_FRIBIDI_SYSTEM #include @@ -546,34 +550,32 @@ raqm_set_text (raqm_t *rq, return true; } -static void * -_raqm_get_utf8_codepoint (const void *str, +static const char * +_raqm_get_utf8_codepoint (const char *str, uint32_t *out_codepoint) { - const char *s = (const char *)str; - - if (0xf0 == (0xf8 & s[0])) + if (0xf0 == (0xf8 & str[0])) { - *out_codepoint = ((0x07 & s[0]) << 18) | ((0x3f & s[1]) << 12) | ((0x3f & s[2]) << 6) | (0x3f & s[3]); - s += 4; + *out_codepoint = ((0x07 & str[0]) << 18) | ((0x3f & str[1]) << 12) | ((0x3f & str[2]) << 6) | (0x3f & str[3]); + str += 4; } - else if (0xe0 == (0xf0 & s[0])) + else if (0xe0 == (0xf0 & str[0])) { - *out_codepoint = ((0x0f & s[0]) << 12) | ((0x3f & s[1]) << 6) | (0x3f & s[2]); - s += 3; + *out_codepoint = ((0x0f & str[0]) << 12) | ((0x3f & str[1]) << 6) | (0x3f & str[2]); + str += 3; } - else if (0xc0 == (0xe0 & s[0])) + else if (0xc0 == (0xe0 & str[0])) { - *out_codepoint = ((0x1f & s[0]) << 6) | (0x3f & s[1]); - s += 2; + *out_codepoint = ((0x1f & str[0]) << 6) | (0x3f & str[1]); + str += 2; } else { - *out_codepoint = s[0]; - s += 1; + *out_codepoint = str[0]; + str += 1; } - return (void *)s; + return str; } static size_t @@ -585,42 +587,41 @@ _raqm_u8_to_u32 (const char *text, size_t len, uint32_t *unicode) while ((*in_utf8 != '\0') && (in_len < len)) { - in_utf8 = _raqm_get_utf8_codepoint (in_utf8, out_utf32); + const char *out_utf8 = _raqm_get_utf8_codepoint (in_utf8, out_utf32); + in_len += out_utf8 - in_utf8; + in_utf8 = out_utf8; ++out_utf32; - ++in_len; } return (out_utf32 - unicode); } -static void * -_raqm_get_utf16_codepoint (const void *str, - uint32_t *out_codepoint) +static const uint16_t * +_raqm_get_utf16_codepoint (const uint16_t *str, + uint32_t *out_codepoint) { - const uint16_t *s = (const uint16_t *)str; - - if (s[0] > 0xD800 && s[0] < 0xDBFF) + if (str[0] >= 0xD800 && str[0] <= 0xDBFF) { - if (s[1] > 0xDC00 && s[1] < 0xDFFF) + if (str[1] >= 0xDC00 && str[1] <= 0xDFFF) { - uint32_t X = ((s[0] & ((1 << 6) -1)) << 10) | (s[1] & ((1 << 10) -1)); - uint32_t W = (s[0] >> 6) & ((1 << 5) - 1); + uint32_t X = ((str[0] & ((1 << 6) -1)) << 10) | (str[1] & ((1 << 10) -1)); + uint32_t W = (str[0] >> 6) & ((1 << 5) - 1); *out_codepoint = (W+1) << 16 | X; - s += 2; + str += 2; } else { /* A single high surrogate, this is an error. */ - *out_codepoint = s[0]; - s += 1; + *out_codepoint = str[0]; + str += 1; } } else { - *out_codepoint = s[0]; - s += 1; + *out_codepoint = str[0]; + str += 1; } - return (void *)s; + return str; } static size_t @@ -632,9 +633,10 @@ _raqm_u16_to_u32 (const uint16_t *text, size_t len, uint32_t *unicode) while ((*in_utf16 != '\0') && (in_len < len)) { - in_utf16 = _raqm_get_utf16_codepoint (in_utf16, out_utf32); + const uint16_t *out_utf16 = _raqm_get_utf16_codepoint (in_utf16, out_utf32); + in_len += (out_utf16 - in_utf16); + in_utf16 = out_utf16; ++out_utf32; - ++in_len; } return (out_utf32 - unicode); @@ -1114,12 +1116,12 @@ _raqm_set_spacing (raqm_t *rq, { if (_raqm_allowed_grapheme_boundary (rq->text[i], rq->text[i+1])) { - /* CSS word seperators, word spacing is only applied on these.*/ + /* CSS word separators, word spacing is only applied on these.*/ if (rq->text[i] == 0x0020 || /* Space */ rq->text[i] == 0x00A0 || /* No Break Space */ rq->text[i] == 0x1361 || /* Ethiopic Word Space */ - rq->text[i] == 0x10100 || /* Aegean Word Seperator Line */ - rq->text[i] == 0x10101 || /* Aegean Word Seperator Dot */ + rq->text[i] == 0x10100 || /* Aegean Word Separator Line */ + rq->text[i] == 0x10101 || /* Aegean Word Separator Dot */ rq->text[i] == 0x1039F || /* Ugaric Word Divider */ rq->text[i] == 0x1091F) /* Phoenician Word Separator */ { @@ -2167,6 +2169,10 @@ _raqm_ft_transform (int *x, *y = vector.y; } +#if !HB_VERSION_ATLEAST (10, 4, 0) +# define hb_ft_font_get_ft_face hb_ft_font_get_face +#endif + static bool _raqm_shape (raqm_t *rq) { @@ -2199,7 +2205,7 @@ _raqm_shape (raqm_t *rq) hb_glyph_position_t *pos; unsigned int len; - FT_Get_Transform (hb_ft_font_get_face (run->font), &matrix, NULL); + FT_Get_Transform (hb_ft_font_get_ft_face (run->font), &matrix, NULL); pos = hb_buffer_get_glyph_positions (run->buffer, &len); info = hb_buffer_get_glyph_infos (run->buffer, &len); diff --git a/tox.ini b/tox.ini index 8933945b1..d58fd67b6 100644 --- a/tox.ini +++ b/tox.ini @@ -30,4 +30,4 @@ skip_install = true deps = -r .ci/requirements-mypy.txt commands = - mypy conftest.py selftest.py setup.py docs src winbuild Tests {posargs} + mypy conftest.py selftest.py setup.py checks docs src winbuild Tests {posargs} diff --git a/wheels/dependency_licenses/ZSTD.txt b/wheels/dependency_licenses/ZSTD.txt new file mode 100644 index 000000000..75800288c --- /dev/null +++ b/wheels/dependency_licenses/ZSTD.txt @@ -0,0 +1,30 @@ +BSD License + +For Zstandard software + +Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name Facebook, nor Meta, nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/wheels/multibuild b/wheels/multibuild index 42d761728..647393271 160000 --- a/wheels/multibuild +++ b/wheels/multibuild @@ -1 +1 @@ -Subproject commit 42d761728d141d8462cd9943f4329f12fe62b155 +Subproject commit 64739327166fcad1fa41ad9b23fa910fa244c84f diff --git a/winbuild/README.md b/winbuild/README.md index 62345af60..db71f094e 100644 --- a/winbuild/README.md +++ b/winbuild/README.md @@ -16,7 +16,7 @@ For more extensive info, see the [Windows build instructions](build.rst). Here's an example script to build on Windows: ``` -set PYTHON=C:\Python39\bin +set PYTHON=C:\Python310\bin cd /D C:\Pillow\winbuild %PYTHON%\python.exe build_prepare.py -v --depends=C:\pillow-depends build\build_dep_all.cmd diff --git a/winbuild/build.rst b/winbuild/build.rst index aa4677ad5..23b26c422 100644 --- a/winbuild/build.rst +++ b/winbuild/build.rst @@ -115,7 +115,7 @@ Example Here's an example script to build on Windows:: - set PYTHON=C:\Python39\bin + set PYTHON=C:\Python310\bin cd /D C:\Pillow\winbuild %PYTHON%\python.exe build_prepare.py -v --depends C:\pillow-depends build\build_dep_all.cmd diff --git a/winbuild/build_prepare.py b/winbuild/build_prepare.py index fbff0daf2..186a80cca 100644 --- a/winbuild/build_prepare.py +++ b/winbuild/build_prepare.py @@ -114,19 +114,19 @@ ARCHITECTURES = { V = { "BROTLI": "1.1.0", - "FREETYPE": "2.13.3", + "FREETYPE": "2.14.1", "FRIBIDI": "1.0.16", - "HARFBUZZ": "11.2.1", - "JPEGTURBO": "3.1.1", + "HARFBUZZ": "12.1.0", + "JPEGTURBO": "3.1.2", "LCMS2": "2.17", "LIBAVIF": "1.3.0", - "LIBIMAGEQUANT": "4.3.4", + "LIBIMAGEQUANT": "4.4.0", "LIBPNG": "1.6.50", "LIBWEBP": "1.6.0", - "OPENJPEG": "2.5.3", - "TIFF": "4.7.0", + "OPENJPEG": "2.5.4", + "TIFF": "4.7.1", "XZ": "5.8.1", - "ZLIBNG": "2.2.4", + "ZLIBNG": "2.2.5", } V["LIBPNG_XY"] = "".join(V["LIBPNG"].split(".")[:2]) @@ -228,12 +228,6 @@ DEPS: dict[str, dict[str, Any]] = { # link against libwebp.lib "#ifdef WEBP_SUPPORT": '#ifdef WEBP_SUPPORT\n#pragma comment(lib, "libwebp.lib")', # noqa: E501 }, - r"test\CMakeLists.txt": { - "add_executable(test_write_read_tags ../placeholder.h)": "", - "target_sources(test_write_read_tags PRIVATE test_write_read_tags.c)": "", # noqa: E501 - "target_link_libraries(test_write_read_tags PRIVATE tiff)": "", - "list(APPEND simple_tests test_write_read_tags)": "", - }, }, "build": [ *cmds_cmake( @@ -241,7 +235,6 @@ DEPS: dict[str, dict[str, Any]] = { "-DBUILD_SHARED_LIBS:BOOL=OFF", "-DWebP_LIBRARY=libwebp", '-DCMAKE_C_FLAGS="-nologo -DLZMA_API_STATIC"', - "-DCMAKE_POLICY_VERSION_MINIMUM=3.5", ) ], "headers": [r"libtiff\tiff*.h"],