diff --git a/.ci/install.sh b/.ci/install.sh index 36bce295c..2143c0613 100755 --- a/.ci/install.sh +++ b/.ci/install.sh @@ -30,7 +30,10 @@ pip install -U pytest-cov pip install pyroma pip install test-image-results pip install numpy -if [ "$TRAVIS_PYTHON_VERSION" == "3.9-dev" ]; then pip install setuptools==47.3.1 ; fi + +# TODO Remove when 3.9-dev includes setuptools 49.3.2+: +if [ "$GHA_PYTHON_VERSION" == "3.9-dev" ]; then pip install -U "setuptools>=49.3.2" ; fi + if [[ $TRAVIS_PYTHON_VERSION == 3.* ]]; then # arm64, ppc64le, s390x CPUs: # "ERROR: Could not find a version that satisfies the requirement pyqt5" diff --git a/.github/workflows/macos-install.sh b/.github/workflows/macos-install.sh index 76a3ef2b7..a0418c3ba 100755 --- a/.github/workflows/macos-install.sh +++ b/.github/workflows/macos-install.sh @@ -15,5 +15,8 @@ pip install test-image-results echo -e "[openblas]\nlibraries = openblas\nlibrary_dirs = /usr/local/opt/openblas/lib" >> ~/.numpy-site.cfg pip install numpy +# TODO Remove when 3.9-dev includes setuptools 49.3.2+: +if [ "$GHA_PYTHON_VERSION" == "3.9-dev" ]; then pip install -U "setuptools>=49.3.2" ; fi + # extra test images pushd depends && ./install_extra_test_images.sh && popd diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml index 9b04b2bd7..a90a0a954 100644 --- a/.github/workflows/test-windows.yml +++ b/.github/workflows/test-windows.yml @@ -63,7 +63,12 @@ jobs: - name: pip install wheel pytest pytest-cov run: python -m pip install wheel pytest pytest-cov - - name: Prepare dependencies + # TODO Remove when 3.9-dev includes setuptools 49.3.2+: + - name: Upgrade setuptools + if: "contains(matrix.python-version, '3.9-dev')" + run: python -m pip install -U "setuptools>=49.3.2" + + - name: Install dependencies run: | 7z x winbuild\depends\nasm-2.14.02-win64.zip "-o$env:RUNNER_WORKSPACE\" Write-Host "::add-path::$env:RUNNER_WORKSPACE\nasm-2.14.02" @@ -72,41 +77,71 @@ jobs: Write-Host "::add-path::C:\Program Files (x86)\gs\gs9.50\bin" xcopy /s winbuild\depends\test_images\* Tests\images\ + shell: pwsh - & python.exe winbuild\build_prepare.py -v --python=$env:pythonLocation + - name: Cache build + id: build-cache + uses: actions/cache@v2 + with: + path: winbuild\build + key: + ${{ hashFiles('winbuild\build_prepare.py') }}-${{ hashFiles('.github\workflows\test-windows.yml') }}-${{ env.pythonLocation }} + + - name: Prepare build + if: steps.build-cache.outputs.cache-hit != 'true' + run: | + & python.exe winbuild\build_prepare.py -v --python=$env:pythonLocation --srcdir shell: pwsh - name: Build dependencies / libjpeg-turbo + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_libjpeg.cmd" - name: Build dependencies / zlib + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_zlib.cmd" - name: Build dependencies / LibTiff + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_libtiff.cmd" - name: Build dependencies / WebP + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_libwebp.cmd" - name: Build dependencies / FreeType + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_freetype.cmd" - name: Build dependencies / LCMS2 + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_lcms2.cmd" - name: Build dependencies / OpenJPEG + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_openjpeg.cmd" - # GPL licensed; skip if building wheels + # GPL licensed - name: Build dependencies / libimagequant - if: "github.event_name != 'push'" + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_libimagequant.cmd" # Raqm dependencies - name: Build dependencies / HarfBuzz + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_harfbuzz.cmd" - name: Build dependencies / FriBidi + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_fribidi.cmd" - name: Build dependencies / Raqm + if: steps.build-cache.outputs.cache-hit != 'true' run: "& winbuild\\build\\build_dep_libraqm.cmd" + # trim ~150MB x 9 + - name: Optimize build cache + if: steps.build-cache.outputs.cache-hit != 'true' + run: rmdir /S /Q winbuild\build\src + shell: cmd + - name: Build Pillow run: | - & winbuild\build\build_pillow.cmd install + $FLAGS="" + if ('${{ github.event_name }}' -eq 'push') { $FLAGS="--disable-imagequant" } + & winbuild\build\build_pillow.cmd $FLAGS install & $env:pythonLocation\python.exe selftest.py --installed shell: pwsh @@ -151,7 +186,7 @@ jobs: if: "github.event_name == 'push'" run: | for /f "tokens=3 delims=/" %%a in ("${{ github.ref }}") do echo ::set-output name=dist::dist-%%a - winbuild\\build\\build_pillow.cmd bdist_wheel" + winbuild\\build\\build_pillow.cmd --disable-imagequant bdist_wheel shell: cmd - uses: actions/upload-artifact@v2 @@ -169,8 +204,10 @@ jobs: mingw: ["MINGW32", "MINGW64"] include: - mingw: "MINGW32" + name: "MSYS2 MinGW 32-bit" package: "mingw-w64-i686" - mingw: "MINGW64" + name: "MSYS2 MinGW 64-bit" package: "mingw-w64-x86_64" defaults: @@ -181,7 +218,7 @@ jobs: CHERE_INVOKING: 1 timeout-minutes: 30 - name: MSYS2 ${{ matrix.mingw }} + name: ${{ matrix.name }} steps: - uses: actions/checkout@v2 @@ -193,23 +230,22 @@ jobs: - name: Install Dependencies run: | pacman -S --noconfirm \ + ${{ matrix.package }}-python3-cffi \ + ${{ matrix.package }}-python3-numpy \ + ${{ matrix.package }}-python3-olefile \ ${{ matrix.package }}-python3-pip \ - ${{ matrix.package }}-python3-setuptools \ + ${{ matrix.package }}-python3-pyqt5 \ ${{ matrix.package }}-python3-pytest \ ${{ matrix.package }}-python3-pytest-cov \ - ${{ matrix.package }}-python3-cffi \ - ${{ matrix.package }}-python3-olefile \ - ${{ matrix.package }}-python3-numpy \ - ${{ matrix.package }}-python3-pyqt5 \ - ${{ matrix.package }}-python3-numpy \ + ${{ matrix.package }}-python3-setuptools \ ${{ matrix.package }}-freetype \ - ${{ matrix.package }}-lcms2 \ - ${{ matrix.package }}-libwebp \ - ${{ matrix.package }}-libjpeg-turbo \ - ${{ matrix.package }}-openjpeg2 \ - ${{ matrix.package }}-libimagequant \ - ${{ matrix.package }}-libraqm \ ${{ matrix.package }}-ghostscript \ + ${{ matrix.package }}-lcms2 \ + ${{ matrix.package }}-libimagequant \ + ${{ matrix.package }}-libjpeg-turbo \ + ${{ matrix.package }}-libraqm \ + ${{ matrix.package }}-libwebp \ + ${{ matrix.package }}-openjpeg2 \ subversion python3 -m pip install pyroma @@ -231,4 +267,4 @@ jobs: python3 -m pip install codecov bash <(curl -s https://codecov.io/bash) -F GHA_Windows env: - CODECOV_NAME: MSYS2 ${{ matrix.mingw }} + CODECOV_NAME: ${{ matrix.name }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65673b0a2..dbe0eb822 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -62,11 +62,15 @@ jobs: if: startsWith(matrix.os, 'ubuntu') run: | .ci/install.sh + env: + GHA_PYTHON_VERSION: ${{ matrix.python-version }} - name: Install macOS dependencies if: startsWith(matrix.os, 'macOS') run: | .github/workflows/macos-install.sh + env: + GHA_PYTHON_VERSION: ${{ matrix.python-version }} - name: Build run: | diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a37674036..459251d77 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,43 +1,43 @@ repos: - repo: https://github.com/psf/black - rev: 6bedb5c58a7d8c25aa9509f8217bc24e9797e90d # frozen: 19.10b0 + rev: e66be67b9b6811913470f70c28b4d50f94d05b22 # frozen: 20.8b1 hooks: - id: black - args: ["--target-version", "py35"] + args: ["--target-version", "py36"] # Only .py files, until https://github.com/psf/black/issues/402 resolved files: \.py$ types: [] - repo: https://github.com/timothycrosley/isort - rev: 7c29dd9d55161704cfc45998c6f5c2c43d39264b # frozen: 4.3.21 + rev: 377d260ffa6f746693f97b46d95025afc4bd8275 # frozen: 5.4.2 hooks: - id: isort - repo: https://github.com/asottile/yesqa - rev: b13a51aa54142c59219c764e9f9362c049b439ed # frozen: v1.2.0 + rev: 7a009f3ee493c796827ee334f9058b110a0e0db8 # frozen: v1.2.1 hooks: - id: yesqa - repo: https://github.com/Lucas-C/pre-commit-hooks - rev: ffbd448645bad2e7ca13f96fca5830058d27ccd5 # frozen: v1.1.7 + rev: f30f4974a08a6b2f6a1eeaf30a4d501cf909163a # frozen: v1.1.9 hooks: - id: remove-tabs exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.opt$) - repo: https://gitlab.com/pycqa/flake8 - rev: 735cfe7e1c57a8e05f660ba75de72313005af54a # frozen: 3.8.2 + rev: 05f6544aef321e2fee03a1277ce2eef8880fb927 # frozen: 3.8.3 hooks: - id: flake8 additional_dependencies: [flake8-2020, flake8-implicit-str-concat] - repo: https://github.com/pre-commit/pygrep-hooks - rev: 0d7d077d6ed5624854f93ac601739c1804ebeb98 # frozen: v1.5.1 + rev: eae6397e4c259ed3d057511f6dd5330b92867e62 # frozen: v1.6.0 hooks: - id: python-check-blanket-noqa - id: rst-backticks - repo: https://github.com/pre-commit/pre-commit-hooks - rev: ebc15addedad713c86ef18ae9632c88e187dd0af # frozen: v3.1.0 + rev: e1668fe86af3810fbca72b8653fe478e66a0afdc # frozen: v3.2.0 hooks: - id: check-merge-conflict - id: check-yaml diff --git a/CHANGES.rst b/CHANGES.rst index d5d1330c2..fd4e3aea3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,36 @@ Changelog (Pillow) 8.0.0 (unreleased) ------------------ +- Added writing of subIFDs #4862 + [radarhere] + +- Fix IFDRational __eq__ bug #4888 + [luphord, radarhere] + +- Fixed duplicate variable name #4885 + [liZe, radarhere] + +- Added homebrew zlib include directory #4842 + [radarhere] + +- Corrected inverted PDF CMYK colors #4866 + [radarhere] + +- Do not try to close file pointer if file pointer is empty #4823 + [radarhere] + +- ImageOps.autocontrast: add mask parameter #4843 + [navneeth, hugovk] + +- Read EXIF data tEXt chunk into info as bytes instead of string #4828 + [radarhere] + +- Remove long-deprecated Image.py functions #4798 + [hugovk, nulano, radarhere] + +- Replaced most uses of distutils with setuptools #4797, #4809, #4814, #4817, #4829 + [hugovk, radarhere] + - Add MIME type to PsdImagePlugin #4788 [samamorgan] diff --git a/Makefile b/Makefile index 06208ad98..cbee5923b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ -# https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: clean coverage doc docserve help inplace install install-req release-test sdist test upload upload-test .DEFAULT_GOAL := release-test +.PHONY: clean clean: python3 setup.py clean rm src/PIL/*.so || true @@ -9,28 +8,34 @@ clean: find . -name __pycache__ | xargs rm -r || true BRANCHES=`git branch -a | grep -v HEAD | grep -v master | grep remote` +.PHONY: co co: -for i in $(BRANCHES) ; do \ git checkout -t $$i ; \ done +.PHONY: coverage coverage: pytest -qq rm -r htmlcov || true coverage report +.PHONY: doc doc: $(MAKE) -C docs html +.PHONY: doccheck doccheck: $(MAKE) -C docs html # Don't make our tests rely on the links in the docs being up every single build. # We don't control them. But do check, and update them to the target of their redirects. $(MAKE) -C docs linkcheck || true +.PHONY: docserve docserve: cd docs/_build/html && python3 -mSimpleHTTPServer 2> /dev/null& +.PHONY: help help: @echo "Welcome to Pillow development. Please use \`make \` where is one of" @echo " clean remove build products" @@ -48,17 +53,21 @@ help: @echo " upload build and upload sdists to PyPI" @echo " upload-test build and upload sdists to test.pythonpackages.com" +.PHONY: inplace inplace: clean python3 setup.py develop build_ext --inplace +.PHONY: install install: python3 setup.py install python3 selftest.py +.PHONY: install-coverage install-coverage: CFLAGS="-coverage" python3 setup.py build_ext install python3 selftest.py +.PHONY: debug debug: # make a debug version if we don't have a -dbg python. Leaves in symbols # for our stuff, kills optimization, and redirects to dev null so we @@ -66,13 +75,16 @@ debug: make clean > /dev/null CFLAGS='-g -O0' python3 setup.py build_ext install > /dev/null +.PHONY: install-req install-req: python3 -m pip install -r requirements.txt +.PHONY: install-venv install-venv: virtualenv . bin/pip install -r requirements.txt +.PHONY: release-test release-test: $(MAKE) install-req python3 setup.py develop @@ -84,22 +96,14 @@ release-test: pyroma . viewdoc +.PHONY: sdist sdist: python3 setup.py sdist --format=gztar +.PHONY: test test: pytest -qq -# https://docs.python.org/3/distutils/packageindex.html#the-pypirc-file -upload-test: -# [test] -# username: -# password: -# repository = http://test.pythonpackages.com - python3 setup.py sdist --format=gztar upload -r test - -upload: - python3 setup.py sdist --format=gztar upload - +.PHONY: readme readme: viewdoc diff --git a/README.md b/README.md new file mode 100644 index 000000000..6ca6cbf83 --- /dev/null +++ b/README.md @@ -0,0 +1,96 @@ +

+ Pillow logo +

+ +# Pillow + +## Python Imaging Library (Fork) + +Pillow is the friendly PIL fork by [Alex Clark and +Contributors](https://github.com/python-pillow/Pillow/graphs/contributors). +PIL is the Python Imaging Library by Fredrik Lundh and Contributors. +As of 2019, Pillow development is +[supported by Tidelift](https://tidelift.com/subscription/pkg/pypi-pillow?utm_source=pypi-pillow&utm_medium=readme&utm_campaign=enterprise). + + + + + + + + + + + + + + + + + + +
docs + Documentation Status +
tests + Travis CI build status (Linux) + Travis CI build status (macOS) + AppVeyor CI build status (Windows) + GitHub Actions build status (Lint) + GitHub Actions build status (Test Linux and macOS) + GitHub Actions build status (Test Windows) + GitHub Actions build status (Test Docker) + Code coverage +
package + Zenodo + Tidelift + Newest PyPI version + Number of PyPI downloads +
social + Join the chat at https://gitter.im/python-pillow/Pillow + Follow on https://twitter.com/PythonPillow +
+ +## More Information + +- [Documentation](https://pillow.readthedocs.io/) + - [Installation](https://pillow.readthedocs.io/en/latest/installation.html) + - [Handbook](https://pillow.readthedocs.io/en/latest/handbook/index.html) +- [Contribute](https://github.com/python-pillow/Pillow/blob/master/.github/CONTRIBUTING.md) + - [Issues](https://github.com/python-pillow/Pillow/issues) + - [Pull requests](https://github.com/python-pillow/Pillow/pulls) +- [Changelog](https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst) + - [Pre-fork](https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#pre-fork) + +## Report a Vulnerability + +To report a security vulnerability, please follow the procedure described in the [Tidelift security policy](https://tidelift.com/docs/security). diff --git a/README.rst b/README.rst deleted file mode 100644 index c1d5be579..000000000 --- a/README.rst +++ /dev/null @@ -1,103 +0,0 @@ -Pillow -====== - -Python Imaging Library (Fork) ------------------------------ - -Pillow is the friendly PIL fork by `Alex Clark and Contributors `_. PIL is the Python Imaging Library by Fredrik Lundh and Contributors. As of 2019, Pillow development is `supported by Tidelift `_. - -.. start-badges - -.. list-table:: - :stub-columns: 1 - - * - docs - - |docs| - * - tests - - |linux| |macos| |windows| |gha_lint| |gha| |gha_windows| |gha_docker| |coverage| - * - package - - |zenodo| |tidelift| |version| |downloads| - * - social - - |gitter| |twitter| - -.. end-badges - -More Information ----------------- - -- `Documentation `_ - - - `Installation `_ - - `Handbook `_ - -- `Contribute `_ - - - `Issues `_ - - `Pull requests `_ - -- `Changelog `_ - - - `Pre-fork `_ - -Report a Vulnerability ----------------------- - -To report a security vulnerability, please follow the procedure described in the `Tidelift security policy `_. - -.. |docs| image:: https://readthedocs.org/projects/pillow/badge/?version=latest - :target: https://pillow.readthedocs.io/?badge=latest - :alt: Documentation Status - -.. |linux| image:: https://img.shields.io/travis/python-pillow/Pillow/master.svg?label=Linux%20build - :target: https://travis-ci.org/python-pillow/Pillow - :alt: Travis CI build status (Linux) - -.. |macos| image:: https://img.shields.io/travis/python-pillow/pillow-wheels/master.svg?label=macOS%20build - :target: https://travis-ci.org/python-pillow/pillow-wheels - :alt: Travis CI build status (macOS) - -.. |windows| image:: https://img.shields.io/appveyor/build/python-pillow/Pillow/master.svg?label=Windows%20build - :target: https://ci.appveyor.com/project/python-pillow/Pillow - :alt: AppVeyor CI build status (Windows) - -.. |gha_lint| image:: https://github.com/python-pillow/Pillow/workflows/Lint/badge.svg - :target: https://github.com/python-pillow/Pillow/actions?query=workflow%3ALint - :alt: GitHub Actions build status (Lint) - -.. |gha_docker| image:: https://github.com/python-pillow/Pillow/workflows/Test%20Docker/badge.svg - :target: https://github.com/python-pillow/Pillow/actions?query=workflow%3A%22Test+Docker%22 - :alt: GitHub Actions build status (Test Docker) - -.. |gha| image:: https://github.com/python-pillow/Pillow/workflows/Test/badge.svg - :target: https://github.com/python-pillow/Pillow/actions?query=workflow%3ATest - :alt: GitHub Actions build status (Test Linux and macOS) - -.. |gha_windows| image:: https://github.com/python-pillow/Pillow/workflows/Test%20Windows/badge.svg - :target: https://github.com/python-pillow/Pillow/actions?query=workflow%3A%22Test+Windows%22 - :alt: GitHub Actions build status (Test Windows) - -.. |coverage| image:: https://codecov.io/gh/python-pillow/Pillow/branch/master/graph/badge.svg - :target: https://codecov.io/gh/python-pillow/Pillow - :alt: Code coverage - -.. |zenodo| image:: https://zenodo.org/badge/17549/python-pillow/Pillow.svg - :target: https://zenodo.org/badge/latestdoi/17549/python-pillow/Pillow - -.. |tidelift| image:: https://tidelift.com/badges/package/pypi/Pillow?style=flat - :target: https://tidelift.com/subscription/pkg/pypi-pillow?utm_source=pypi-pillow&utm_medium=badge - -.. |version| image:: https://img.shields.io/pypi/v/pillow.svg - :target: https://pypi.org/project/Pillow/ - :alt: Latest PyPI version - -.. |downloads| image:: https://img.shields.io/pypi/dm/pillow.svg - :target: https://pypi.org/project/Pillow/ - :alt: Number of PyPI downloads - -.. |gitter| image:: https://badges.gitter.im/python-pillow/Pillow.svg - :target: https://gitter.im/python-pillow/Pillow?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge - :alt: Join the chat at https://gitter.im/python-pillow/Pillow - -.. |twitter| image:: https://img.shields.io/badge/tweet-on%20Twitter-00aced.svg - :target: https://twitter.com/PythonPillow - :alt: Follow on https://twitter.com/PythonPillow diff --git a/RELEASING.md b/RELEASING.md index 3f62a70c4..c9a0439d8 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -101,11 +101,7 @@ Released as needed privately to individual vendors for critical security-related cd pillow-wheels ./update-pillow-tag.sh [[release tag]] ``` -* [ ] Download distributions from the [Pillow Wheel Builder container](http://a365fff413fe338398b6-1c8a9b3114517dc5fe17b7c3f8c63a43.r19.cf2.rackcdn.com/). - ```bash - wget -m -A 'Pillow--*' \ - http://a365fff413fe338398b6-1c8a9b3114517dc5fe17b7c3f8c63a43.r19.cf2.rackcdn.com - ``` +* [ ] Download wheels from the [Pillow Wheel Builder release](https://github.com/python-pillow/pillow-wheels/releases). ## Publicize Release diff --git a/Tests/bench_cffi_access.py b/Tests/bench_cffi_access.py index f196757dc..f9edcf09a 100644 --- a/Tests/bench_cffi_access.py +++ b/Tests/bench_cffi_access.py @@ -28,15 +28,17 @@ def timer(func, label, *args): func(*args) if time.time() - starttime > 10: print( - "%s: breaking at %s iterations, %.6f per iteration" - % (label, x + 1, (time.time() - starttime) / (x + 1.0)) + "{}: breaking at {} iterations, {:.6f} per iteration".format( + label, x + 1, (time.time() - starttime) / (x + 1.0) + ) ) break if x == iterations - 1: endtime = time.time() print( - "%s: %.4f s %.6f per iteration" - % (label, endtime - starttime, (endtime - starttime) / (x + 1.0)) + "{}: {:.4f} s {:.6f} per iteration".format( + label, endtime - starttime, (endtime - starttime) / (x + 1.0) + ) ) diff --git a/Tests/check_imaging_leaks.py b/Tests/check_imaging_leaks.py index db12d00e3..407f3ea80 100755 --- a/Tests/check_imaging_leaks.py +++ b/Tests/check_imaging_leaks.py @@ -1,5 +1,6 @@ #!/usr/bin/env python import pytest + from PIL import Image from .helper import is_win32 @@ -11,7 +12,7 @@ pytestmark = pytest.mark.skipif(is_win32(), reason="requires Unix or macOS") def _get_mem_usage(): - from resource import getpagesize, getrusage, RUSAGE_SELF + from resource import RUSAGE_SELF, getpagesize, getrusage mem = getrusage(RUSAGE_SELF).ru_maxrss return mem * getpagesize() / 1024 / 1024 @@ -25,7 +26,7 @@ def _test_leak(min_iterations, max_iterations, fn, *args, **kwargs): if i < min_iterations: mem_limit = mem + 1 continue - msg = "memory usage limit exceeded after %d iterations" % (i + 1) + msg = f"memory usage limit exceeded after {i + 1} iterations" assert mem <= mem_limit, msg diff --git a/Tests/check_j2k_leaks.py b/Tests/check_j2k_leaks.py index 5cef4b544..afe5836f3 100755 --- a/Tests/check_j2k_leaks.py +++ b/Tests/check_j2k_leaks.py @@ -1,6 +1,7 @@ from io import BytesIO import pytest + from PIL import Image from .helper import is_win32, skip_unless_feature @@ -18,7 +19,7 @@ pytestmark = [ def test_leak_load(): - from resource import setrlimit, RLIMIT_AS, RLIMIT_STACK + from resource import RLIMIT_AS, RLIMIT_STACK, setrlimit setrlimit(RLIMIT_STACK, (stack_size, stack_size)) setrlimit(RLIMIT_AS, (mem_limit, mem_limit)) @@ -28,7 +29,7 @@ def test_leak_load(): def test_leak_save(): - from resource import setrlimit, RLIMIT_AS, RLIMIT_STACK + from resource import RLIMIT_AS, RLIMIT_STACK, setrlimit setrlimit(RLIMIT_STACK, (stack_size, stack_size)) setrlimit(RLIMIT_AS, (mem_limit, mem_limit)) diff --git a/Tests/check_j2k_overflow.py b/Tests/check_j2k_overflow.py index 7a0a5f948..b16412898 100644 --- a/Tests/check_j2k_overflow.py +++ b/Tests/check_j2k_overflow.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image diff --git a/Tests/check_jpeg_leaks.py b/Tests/check_jpeg_leaks.py index b63fa2a1e..ab8d77719 100644 --- a/Tests/check_jpeg_leaks.py +++ b/Tests/check_jpeg_leaks.py @@ -119,60 +119,59 @@ def test_qtables_leak(): def test_exif_leak(): """ -pre patch: + pre patch: - MB -177.1^ # - | @@@# - | :@@@@@@# - | ::::@@@@@@# - | ::::::::@@@@@@# - | @@::::: ::::@@@@@@# - | @@@@ ::::: ::::@@@@@@# - | @@@@@@@ ::::: ::::@@@@@@# - | @@::@@@@@@@ ::::: ::::@@@@@@# - | @@@@ : @@@@@@@ ::::: ::::@@@@@@# - | @@@@@@ @@ : @@@@@@@ ::::: ::::@@@@@@# - | @@@@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | @::@@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | ::::@: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | :@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | ::@@::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | @@::: @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | @::@ : : @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | :::@: @ : : @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - | @@@:: @: @ : : @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# - 0 +----------------------------------------------------------------------->Gi - 0 11.37 + MB + 177.1^ # + | @@@# + | :@@@@@@# + | ::::@@@@@@# + | ::::::::@@@@@@# + | @@::::: ::::@@@@@@# + | @@@@ ::::: ::::@@@@@@# + | @@@@@@@ ::::: ::::@@@@@@# + | @@::@@@@@@@ ::::: ::::@@@@@@# + | @@@@ : @@@@@@@ ::::: ::::@@@@@@# + | @@@@@@ @@ : @@@@@@@ ::::: ::::@@@@@@# + | @@@@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | @::@@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | ::::@: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | :@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | ::@@::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | @@::: @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | @::@ : : @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | :::@: @ : : @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + | @@@:: @: @ : : @ ::@@: : @: @@ @@ @@ @ @@ : @@@@@@@ ::::: ::::@@@@@@# + 0 +----------------------------------------------------------------------->Gi + 0 11.37 -post patch: + post patch: - MB -21.06^ ::::::::::::::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | ##::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | # ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | # ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | # ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @@@@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: - 0 +----------------------------------------------------------------------->Gi - 0 11.33 - -""" + MB + 21.06^ ::::::::::::::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | ##::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | # ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | # ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | # ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @@@@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + | @ @@@# ::: ::::: : ::::::::::@::::@::::@::::@::::@::::@:::::::::@:::::: + 0 +----------------------------------------------------------------------->Gi + 0 11.33 + """ im = hopper("RGB") exif = b"12345678" * 4096 @@ -183,31 +182,30 @@ post patch: def test_base_save(): """ -base case: - MB -20.99^ ::::: :::::::::::::::::::::::::::::::::::::::::::@::: - | ##: : ::::::@::::::: :::: :::: : : : : : : :::::::::::: :::@::: - | # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @@# : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @@@ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | :@@@@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: - 0 +----------------------------------------------------------------------->Gi - 0 7.882 -""" + base case: + MB + 20.99^ ::::: :::::::::::::::::::::::::::::::::::::::::::@::: + | ##: : ::::::@::::::: :::: :::: : : : : : : :::::::::::: :::@::: + | # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @@# : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @@@ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | :@@@@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + | :@ @@ @ # : : :: :: @:: :::: :::: :::: : : : : : : :::::::::::: :::@::: + 0 +----------------------------------------------------------------------->Gi + 0 7.882""" im = hopper("RGB") for _ in range(iterations): diff --git a/Tests/check_large_memory.py b/Tests/check_large_memory.py index f44a5a5bb..723a1a21e 100644 --- a/Tests/check_large_memory.py +++ b/Tests/check_large_memory.py @@ -1,6 +1,7 @@ import sys import pytest + from PIL import Image # This test is not run automatically. diff --git a/Tests/check_large_memory_numpy.py b/Tests/check_large_memory_numpy.py index de6f4571c..79d1cfd5b 100644 --- a/Tests/check_large_memory_numpy.py +++ b/Tests/check_large_memory_numpy.py @@ -1,6 +1,7 @@ import sys import pytest + from PIL import Image # This test is not run automatically. diff --git a/Tests/check_libtiff_segfault.py b/Tests/check_libtiff_segfault.py index 6663ac097..bd7f407e4 100644 --- a/Tests/check_libtiff_segfault.py +++ b/Tests/check_libtiff_segfault.py @@ -1,13 +1,14 @@ import pytest + from PIL import Image TEST_FILE = "Tests/images/libtiff_segfault.tif" def test_libtiff_segfault(): - """ This test should not segfault. It will on Pillow <= 3.1.0 and - libtiff >= 4.0.0 - """ + """This test should not segfault. It will on Pillow <= 3.1.0 and + libtiff >= 4.0.0 + """ with pytest.raises(OSError): with Image.open(TEST_FILE) as im: diff --git a/Tests/check_png_dos.py b/Tests/check_png_dos.py index 86eb937e9..d8d645189 100644 --- a/Tests/check_png_dos.py +++ b/Tests/check_png_dos.py @@ -42,8 +42,8 @@ def test_dos_total_memory(): info = PngImagePlugin.PngInfo() for x in range(64): - info.add_text("t%s" % x, compressed_data, zip=True) - info.add_itxt("i%s" % x, compressed_data, zip=True) + info.add_text(f"t{x}", compressed_data, zip=True) + info.add_itxt(f"i{x}", compressed_data, zip=True) b = BytesIO() im.save(b, "PNG", pnginfo=info) diff --git a/Tests/conftest.py b/Tests/conftest.py index 624eab73c..082f2f7c3 100644 --- a/Tests/conftest.py +++ b/Tests/conftest.py @@ -9,4 +9,4 @@ def pytest_report_header(config): features.pilinfo(out=out, supported_formats=False) return out.getvalue() except Exception as e: - return "pytest_report_header failed: %s" % e + return f"pytest_report_header failed: {e}" diff --git a/Tests/createfontdatachunk.py b/Tests/createfontdatachunk.py index c7055995e..011bb0bed 100755 --- a/Tests/createfontdatachunk.py +++ b/Tests/createfontdatachunk.py @@ -6,7 +6,7 @@ if __name__ == "__main__": # create font data chunk for embedding font = "Tests/images/courB08" print(" f._load_pilfont_data(") - print(" # %s" % os.path.basename(font)) + print(f" # {os.path.basename(font)}") print(" BytesIO(base64.decodestring(b'''") with open(font + ".pil", "rb") as fp: print(base64.b64encode(fp.read()).decode()) diff --git a/Tests/helper.py b/Tests/helper.py index cdc5f4efe..c8cbb80e1 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -11,6 +11,7 @@ import tempfile from io import BytesIO import pytest + from PIL import Image, ImageMath, features logger = logging.getLogger(__name__) @@ -67,37 +68,31 @@ def convert_to_comparable(a, b): def assert_deep_equal(a, b, msg=None): try: - assert len(a) == len(b), msg or "got length {}, expected {}".format( - len(a), len(b) - ) + assert len(a) == len(b), msg or f"got length {len(a)}, expected {len(b)}" except Exception: assert a == b, msg def assert_image(im, mode, size, msg=None): if mode is not None: - assert im.mode == mode, msg or "got mode {!r}, expected {!r}".format( - im.mode, mode + assert im.mode == mode, ( + msg or f"got mode {repr(im.mode)}, expected {repr(mode)}" ) if size is not None: - assert im.size == size, msg or "got size {!r}, expected {!r}".format( - im.size, size + assert im.size == size, ( + msg or f"got size {repr(im.size)}, expected {repr(size)}" ) def assert_image_equal(a, b, msg=None): - assert a.mode == b.mode, msg or "got mode {!r}, expected {!r}".format( - a.mode, b.mode - ) - assert a.size == b.size, msg or "got size {!r}, expected {!r}".format( - a.size, b.size - ) + assert a.mode == b.mode, msg or f"got mode {repr(a.mode)}, expected {repr(b.mode)}" + assert a.size == b.size, msg or f"got size {repr(a.size)}, expected {repr(b.size)}" if a.tobytes() != b.tobytes(): if HAS_UPLOADER: try: url = test_image_results.upload(a, b) - logger.error("Url for test images: %s" % url) + logger.error(f"Url for test images: {url}") except Exception: pass @@ -112,12 +107,8 @@ def assert_image_equal_tofile(a, filename, msg=None, mode=None): def assert_image_similar(a, b, epsilon, msg=None): - assert a.mode == b.mode, msg or "got mode {!r}, expected {!r}".format( - a.mode, b.mode - ) - assert a.size == b.size, msg or "got size {!r}, expected {!r}".format( - a.size, b.size - ) + assert a.mode == b.mode, msg or f"got mode {repr(a.mode)}, expected {repr(b.mode)}" + assert a.size == b.size, msg or f"got size {repr(a.size)}, expected {repr(b.size)}" a, b = convert_to_comparable(a, b) @@ -129,13 +120,14 @@ def assert_image_similar(a, b, epsilon, msg=None): ave_diff = diff / (a.size[0] * a.size[1]) try: assert epsilon >= ave_diff, ( - msg or "" - ) + " average pixel value difference %.4f > epsilon %.4f" % (ave_diff, epsilon) + (msg or "") + + f" average pixel value difference {ave_diff:.4f} > epsilon {epsilon:.4f}" + ) except Exception as e: if HAS_UPLOADER: try: url = test_image_results.upload(a, b) - logger.error("Url for test images: %s" % url) + logger.error(f"Url for test images: {url}") except Exception: pass raise e @@ -166,7 +158,7 @@ def assert_tuple_approx_equal(actuals, targets, threshold, msg): def skip_unless_feature(feature): - reason = "%s not available" % feature + reason = f"{feature} not available" return pytest.mark.skipif(not features.check(feature), reason=reason) @@ -184,7 +176,7 @@ class PillowLeakTestCase: :returns: memory usage in kilobytes """ - from resource import getrusage, RUSAGE_SELF + from resource import RUSAGE_SELF, getrusage mem = getrusage(RUSAGE_SELF).ru_maxrss if sys.platform == "darwin": @@ -204,7 +196,7 @@ class PillowLeakTestCase: for cycle in range(self.iterations): core() mem = self._get_mem_usage() - start_mem - msg = "memory usage limit exceeded in iteration %d" % cycle + msg = f"memory usage limit exceeded in iteration {cycle}" assert mem < self.mem_limit, msg diff --git a/Tests/images/empty_gps_ifd.jpg b/Tests/images/empty_gps_ifd.jpg new file mode 100644 index 000000000..28f180b87 Binary files /dev/null and b/Tests/images/empty_gps_ifd.jpg differ diff --git a/Tests/images/exif_text.png b/Tests/images/exif_text.png new file mode 100644 index 000000000..e2d8dc0ff Binary files /dev/null and b/Tests/images/exif_text.png differ diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py index ade2901b7..19602c1e7 100644 --- a/Tests/test_bmp_reference.py +++ b/Tests/test_bmp_reference.py @@ -1,6 +1,7 @@ import os import pytest + from PIL import Image from .helper import assert_image_similar @@ -15,8 +16,8 @@ def get_files(d, ext=".bmp"): def test_bad(): - """ These shouldn't crash/dos, but they shouldn't return anything - either """ + """These shouldn't crash/dos, but they shouldn't return anything + either""" for f in get_files("b"): def open(f): @@ -31,8 +32,8 @@ def test_bad(): def test_questionable(): - """ These shouldn't crash/dos, but it's not well defined that these - are in spec """ + """These shouldn't crash/dos, but it's not well defined that these + are in spec""" supported = [ "pal8os2v2.bmp", "rgb24prof.bmp", @@ -49,15 +50,15 @@ def test_questionable(): with Image.open(f) as im: im.load() if os.path.basename(f) not in supported: - print("Please add %s to the partially supported bmp specs." % f) + print(f"Please add {f} to the partially supported bmp specs.") except Exception: # as msg: if os.path.basename(f) in supported: raise def test_good(): - """ These should all work. There's a set of target files in the - html directory that we can compare against. """ + """These should all work. There's a set of target files in the + html directory that we can compare against.""" # Target files, if they're not just replacing the extension file_map = { @@ -84,7 +85,7 @@ def test_good(): if name in file_map: return os.path.join(base, "html", file_map[name]) name = os.path.splitext(name)[0] - return os.path.join(base, "html", "%s.png" % name) + return os.path.join(base, "html", f"{name}.png") for f in get_files("g"): try: @@ -107,4 +108,4 @@ def test_good(): os.path.join(base, "g", "pal8rle.bmp"), os.path.join(base, "g", "pal4rle.bmp"), ) - assert f in unsupported, "Unsupported Image {}: {}".format(f, msg) + assert f in unsupported, f"Unsupported Image {f}: {msg}" diff --git a/Tests/test_box_blur.py b/Tests/test_box_blur.py index 44910b9ed..94f504e0b 100644 --- a/Tests/test_box_blur.py +++ b/Tests/test_box_blur.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageFilter sample = Image.new("L", (7, 5)) diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index b34dbadb6..99776ce58 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -1,6 +1,7 @@ from array import array import pytest + from PIL import Image, ImageFilter from .helper import assert_image_equal diff --git a/Tests/test_core_resources.py b/Tests/test_core_resources.py index a8fe8bfeb..6c52d25a4 100644 --- a/Tests/test_core_resources.py +++ b/Tests/test_core_resources.py @@ -1,6 +1,7 @@ import sys import pytest + from PIL import Image from .helper import is_pypy diff --git a/Tests/test_decompression_bomb.py b/Tests/test_decompression_bomb.py index 1704400b4..5799fc0ed 100644 --- a/Tests/test_decompression_bomb.py +++ b/Tests/test_decompression_bomb.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import hopper diff --git a/Tests/test_features.py b/Tests/test_features.py index 1e7692204..284f72205 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -2,6 +2,7 @@ import io import re import pytest + from PIL import features from .helper import skip_unless_feature diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index 0eca78690..2d50748bd 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageSequence, PngImagePlugin @@ -311,7 +312,7 @@ def test_apng_sequence_errors(): ] for f in test_files: with pytest.raises(SyntaxError): - with Image.open("Tests/images/apng/{0}".format(f)) as im: + with Image.open(f"Tests/images/apng/{f}") as im: im.seek(im.n_frames - 1) im.load() @@ -358,7 +359,10 @@ def test_apng_save_split_fdat(tmp_path): with Image.open("Tests/images/old-style-jpeg-compression.png") as im: frames = [im.copy(), Image.new("RGBA", im.size, (255, 0, 0, 255))] im.save( - test_file, save_all=True, default_image=True, append_images=frames, + test_file, + save_all=True, + default_image=True, + append_images=frames, ) with Image.open(test_file) as im: exception = None diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index 8bb58794c..e2381df1e 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -1,6 +1,7 @@ import io import pytest + from PIL import BmpImagePlugin, Image from .helper import assert_image_equal, hopper diff --git a/Tests/test_file_bufrstub.py b/Tests/test_file_bufrstub.py index 6803a1230..11acc1c88 100644 --- a/Tests/test_file_bufrstub.py +++ b/Tests/test_file_bufrstub.py @@ -1,4 +1,5 @@ import pytest + from PIL import BufrStubImagePlugin, Image from .helper import hopper diff --git a/Tests/test_file_cur.py b/Tests/test_file_cur.py index 3200fd8f6..f04a20a22 100644 --- a/Tests/test_file_cur.py +++ b/Tests/test_file_cur.py @@ -1,4 +1,5 @@ import pytest + from PIL import CurImagePlugin, Image TEST_FILE = "Tests/images/deerstalker.cur" diff --git a/Tests/test_file_dcx.py b/Tests/test_file_dcx.py index bc76b4591..818d6ed5e 100644 --- a/Tests/test_file_dcx.py +++ b/Tests/test_file_dcx.py @@ -1,4 +1,5 @@ import pytest + from PIL import DcxImagePlugin, Image from .helper import assert_image_equal, hopper, is_pypy diff --git a/Tests/test_file_dds.py b/Tests/test_file_dds.py index 8960edea3..0aec9ea37 100644 --- a/Tests/test_file_dds.py +++ b/Tests/test_file_dds.py @@ -2,6 +2,7 @@ from io import BytesIO import pytest + from PIL import DdsImagePlugin, Image from .helper import assert_image_equal diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index 504c09db1..f585a0669 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -1,6 +1,7 @@ import io import pytest + from PIL import EpsImagePlugin, Image, features from .helper import assert_image_similar, hopper, skip_unless_feature diff --git a/Tests/test_file_fitsstub.py b/Tests/test_file_fitsstub.py index 01bc2deee..6dc7c4602 100644 --- a/Tests/test_file_fitsstub.py +++ b/Tests/test_file_fitsstub.py @@ -1,4 +1,5 @@ import pytest + from PIL import FitsStubImagePlugin, Image TEST_FILE = "Tests/images/hopper.fits" diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index 726e16c1a..16b3dc59a 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -1,4 +1,5 @@ import pytest + from PIL import FliImagePlugin, Image from .helper import assert_image_equal, is_pypy diff --git a/Tests/test_file_fpx.py b/Tests/test_file_fpx.py index a247de79c..c3cc37ddf 100644 --- a/Tests/test_file_fpx.py +++ b/Tests/test_file_fpx.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image FpxImagePlugin = pytest.importorskip( diff --git a/Tests/test_file_gbr.py b/Tests/test_file_gbr.py index b95970889..760f12e4d 100644 --- a/Tests/test_file_gbr.py +++ b/Tests/test_file_gbr.py @@ -1,4 +1,5 @@ import pytest + from PIL import GbrImagePlugin, Image from .helper import assert_image_equal diff --git a/Tests/test_file_gd.py b/Tests/test_file_gd.py index b6f8594be..5594e5bbb 100644 --- a/Tests/test_file_gd.py +++ b/Tests/test_file_gd.py @@ -1,4 +1,5 @@ import pytest + from PIL import GdImageFile, UnidentifiedImageError TEST_GD_FILE = "Tests/images/hopper.gd" diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 455e30f71..90943ac8f 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1,6 +1,7 @@ from io import BytesIO import pytest + from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette, features from .helper import ( diff --git a/Tests/test_file_gimppalette.py b/Tests/test_file_gimppalette.py index a38c6320c..caec9cf21 100644 --- a/Tests/test_file_gimppalette.py +++ b/Tests/test_file_gimppalette.py @@ -1,4 +1,5 @@ import pytest + from PIL.GimpPaletteFile import GimpPaletteFile diff --git a/Tests/test_file_gribstub.py b/Tests/test_file_gribstub.py index 9d9def96b..e4930d8dc 100644 --- a/Tests/test_file_gribstub.py +++ b/Tests/test_file_gribstub.py @@ -1,4 +1,5 @@ import pytest + from PIL import GribStubImagePlugin, Image from .helper import hopper diff --git a/Tests/test_file_hdf5stub.py b/Tests/test_file_hdf5stub.py index 862cafa91..ff3397055 100644 --- a/Tests/test_file_hdf5stub.py +++ b/Tests/test_file_hdf5stub.py @@ -1,4 +1,5 @@ import pytest + from PIL import Hdf5StubImagePlugin, Image TEST_FILE = "Tests/images/hdf5.h5" diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index 05feedb1a..a3d502d42 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -2,6 +2,7 @@ import io import sys import pytest + from PIL import IcnsImagePlugin, Image, features from .helper import assert_image_equal, assert_image_similar diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 9ed1ffcb7..f7a513509 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -1,6 +1,7 @@ import io import pytest + from PIL import IcoImagePlugin, Image, ImageDraw from .helper import assert_image_equal, hopper diff --git a/Tests/test_file_im.py b/Tests/test_file_im.py index 30a9fd52a..afea82359 100644 --- a/Tests/test_file_im.py +++ b/Tests/test_file_im.py @@ -1,6 +1,7 @@ import filecmp import pytest + from PIL import Image, ImImagePlugin from .helper import assert_image_equal, hopper, is_pypy diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 1801cd8ff..78d588dec 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -3,10 +3,12 @@ import re from io import BytesIO import pytest + from PIL import ( ExifTags, Image, ImageFile, + ImageOps, JpegImagePlugin, UnidentifiedImageError, features, @@ -38,7 +40,7 @@ class TestFileJpeg: return im def gen_random_image(self, size, mode="RGB"): - """ Generates a very hard to compress file + """Generates a very hard to compress file :param size: tuple :param mode: optional image mode @@ -98,7 +100,8 @@ class TestFileJpeg: assert k > 0.9 @pytest.mark.parametrize( - "test_image_path", [TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"], + "test_image_path", + [TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"], ) def test_dpi(self, test_image_path): def test(xdpi, ydpi=None): @@ -223,23 +226,58 @@ class TestFileJpeg: # Should not raise a TypeError im._getexif() - def test_exif_gps(self): - # Arrange + def test_exif_gps(self, tmp_path): + expected_exif_gps = { + 0: b"\x00\x00\x00\x01", + 2: 4294967295, + 5: b"\x01", + 30: 65535, + 29: "1999:99:99 99:99:99", + } + gps_index = 34853 + + # Reading with Image.open("Tests/images/exif_gps.jpg") as im: - gps_index = 34853 - expected_exif_gps = { - 0: b"\x00\x00\x00\x01", - 2: 4294967295, - 5: b"\x01", - 30: 65535, - 29: "1999:99:99 99:99:99", - } - - # Act exif = im._getexif() + assert exif[gps_index] == expected_exif_gps - # Assert - assert exif[gps_index] == expected_exif_gps + # Writing + f = str(tmp_path / "temp.jpg") + exif = Image.Exif() + exif[gps_index] = expected_exif_gps + hopper().save(f, exif=exif) + + with Image.open(f) as reloaded: + exif = reloaded._getexif() + assert exif[gps_index] == expected_exif_gps + + def test_empty_exif_gps(self): + with Image.open("Tests/images/empty_gps_ifd.jpg") as im: + exif = im.getexif() + del exif[0x8769] + + # Assert that it needs to be transposed + assert exif[0x0112] == Image.TRANSVERSE + + # Assert that the GPS IFD is present and empty + assert exif[0x8825] == {} + + transposed = ImageOps.exif_transpose(im) + exif = transposed.getexif() + assert exif[0x8825] == {} + + # Assert that it was transposed + assert 0x0112 not in exif + + def test_exif_equality(self): + # In 7.2.0, Exif rationals were changed to be read as + # TiffImagePlugin.IFDRational. This class had a bug in __eq__, + # breaking the self-equality of Exif data + exifs = [] + for i in range(2): + with Image.open("Tests/images/exif-200dpcm.jpg") as im: + exifs.append(im._getexif()) + assert exifs[0] == exifs[1] def test_exif_rollback(self): # rolling back exif support in 3.1 to pre-3.0 formatting. diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index d558843b8..c9e37f8b0 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -2,6 +2,7 @@ import re from io import BytesIO import pytest + from PIL import Image, ImageFile, Jpeg2KImagePlugin, features from .helper import ( diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 300967a30..da955b3de 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1,13 +1,13 @@ import base64 import io import itertools -import logging import os import re from collections import namedtuple from ctypes import c_float import pytest + from PIL import Image, ImageFilter, TiffImagePlugin, TiffTags, features from .helper import ( @@ -19,8 +19,6 @@ from .helper import ( skip_unless_feature, ) -logger = logging.getLogger(__name__) - @skip_unless_feature("libtiff") class LibTiffTestCase: @@ -173,18 +171,18 @@ class TestFileLibTiff(LibTiffTestCase): assert ( c_float(val[0][0] / val[0][1]).value == c_float(value[0][0] / value[0][1]).value - ), ("%s didn't roundtrip" % tag) + ), f"{tag} didn't roundtrip" else: - assert c_float(val).value == c_float(value).value, ( - "%s didn't roundtrip" % tag - ) + assert ( + c_float(val).value == c_float(value).value + ), f"{tag} didn't roundtrip" else: - assert val == value, "%s didn't roundtrip" % tag + assert val == value, f"{tag} didn't roundtrip" # https://github.com/python-pillow/Pillow/issues/1561 requested_fields = ["StripByteCounts", "RowsPerStrip", "StripOffsets"] for field in requested_fields: - assert field in reloaded, "%s not in metadata" % field + assert field in reloaded, f"{field} not in metadata" def test_additional_metadata(self, tmp_path): # these should not crash. Seriously dummy data, most of it doesn't make @@ -404,8 +402,8 @@ class TestFileLibTiff(LibTiffTestCase): assert "temp.tif" == reread.tag[269][0] def test_12bit_rawmode(self): - """ Are we generating the same interpretation - of the image as Imagemagick is? """ + """Are we generating the same interpretation + of the image as Imagemagick is?""" TiffImagePlugin.READ_LIBTIFF = True with Image.open("Tests/images/12bit.cropped.tif") as im: im.load() @@ -505,9 +503,9 @@ class TestFileLibTiff(LibTiffTestCase): assert len(reloaded.tag_v2[320]) == 768 def xtest_bw_compression_w_rgb(self, tmp_path): - """ This test passes, but when running all tests causes a failure due - to output on stderr from the error thrown by libtiff. We need to - capture that but not now""" + """This test passes, but when running all tests causes a failure due + to output on stderr from the error thrown by libtiff. We need to + capture that but not now""" im = hopper("RGB") out = str(tmp_path / "temp.tif") @@ -770,7 +768,7 @@ class TestFileLibTiff(LibTiffTestCase): assert im.mode == "RGBA" assert im.size == (100, 40) assert im.tile, [ - ("libtiff", (0, 0, 100, 40), 0, ("RGBa;16N", "tiff_lzw", False, 38236),) + ("libtiff", (0, 0, 100, 40), 0, ("RGBa;16N", "tiff_lzw", False, 38236)) ] im.load() diff --git a/Tests/test_file_libtiff_small.py b/Tests/test_file_libtiff_small.py index 593a8eda8..03137c8b6 100644 --- a/Tests/test_file_libtiff_small.py +++ b/Tests/test_file_libtiff_small.py @@ -7,13 +7,13 @@ from .test_file_libtiff import LibTiffTestCase class TestFileLibTiffSmall(LibTiffTestCase): - """ The small lena image was failing on open in the libtiff - decoder because the file pointer was set to the wrong place - by a spurious seek. It wasn't failing with the byteio method. + """The small lena image was failing on open in the libtiff + decoder because the file pointer was set to the wrong place + by a spurious seek. It wasn't failing with the byteio method. - It was fixed by forcing an lseek to the beginning of the - file just before reading in libtiff. These tests remain - to ensure that it stays fixed. """ + It was fixed by forcing an lseek to the beginning of the + file just before reading in libtiff. These tests remain + to ensure that it stays fixed.""" def test_g4_hopper_file(self, tmp_path): """Testing the open file load path""" diff --git a/Tests/test_file_mcidas.py b/Tests/test_file_mcidas.py index 516dbb208..88c8f8f4f 100644 --- a/Tests/test_file_mcidas.py +++ b/Tests/test_file_mcidas.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, McIdasImagePlugin from .helper import assert_image_equal diff --git a/Tests/test_file_mic.py b/Tests/test_file_mic.py index 5003090c7..464d138e2 100644 --- a/Tests/test_file_mic.py +++ b/Tests/test_file_mic.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImagePalette from .helper import assert_image_similar, hopper, skip_unless_feature diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 893f9075d..791efcc3f 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -1,6 +1,7 @@ from io import BytesIO import pytest + from PIL import Image from .helper import assert_image_similar, is_pypy, skip_unless_feature diff --git a/Tests/test_file_msp.py b/Tests/test_file_msp.py index 9b508a4e4..293b856b0 100644 --- a/Tests/test_file_msp.py +++ b/Tests/test_file_msp.py @@ -1,6 +1,7 @@ import os import pytest + from PIL import Image, MspImagePlugin from .helper import assert_image_equal, hopper diff --git a/Tests/test_file_palm.py b/Tests/test_file_palm.py index e7afeef23..25d194b62 100644 --- a/Tests/test_file_palm.py +++ b/Tests/test_file_palm.py @@ -2,6 +2,7 @@ import os.path import subprocess import pytest + from PIL import Image from .helper import IMCONVERT, assert_image_equal, hopper, imagemagick_available diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index 5af7469c7..670c03b95 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageFile, PcxImagePlugin from .helper import assert_image_equal, hopper diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index 14a7a654f..3e23beae7 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -5,6 +5,7 @@ import tempfile import time import pytest + from PIL import Image, PdfParser from .helper import hopper diff --git a/Tests/test_file_pixar.py b/Tests/test_file_pixar.py index 5e83c6104..315ea4676 100644 --- a/Tests/test_file_pixar.py +++ b/Tests/test_file_pixar.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, PixarImagePlugin from .helper import assert_image_similar, hopper diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 9891b6399..f580f4ae7 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -3,6 +3,7 @@ import zlib from io import BytesIO import pytest + from PIL import Image, ImageFile, PngImagePlugin, features from .helper import ( @@ -606,6 +607,11 @@ class TestFilePng: exif = im.copy().getexif() assert exif[274] == 1 + # With a tEXt chunk + with Image.open("Tests/images/exif_text.png") as im: + exif = im._getexif() + assert exif[274] == 1 + # With XMP tags with Image.open("Tests/images/xmp_tags_orientation.png") as im: exif = im.getexif() diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 15c08e438..e7c3fb06f 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import assert_image_equal, assert_image_similar, hopper diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 6b26fe442..8bb45630e 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, PsdImagePlugin from .helper import assert_image_similar, hopper, is_pypy diff --git a/Tests/test_file_sgi.py b/Tests/test_file_sgi.py index cb16276ce..a197fa775 100644 --- a/Tests/test_file_sgi.py +++ b/Tests/test_file_sgi.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, SgiImagePlugin from .helper import assert_image_equal, assert_image_similar, hopper diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index 8c69491e6..9cdb451c9 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -2,6 +2,7 @@ import tempfile from io import BytesIO import pytest + from PIL import Image, ImageSequence, SpiderImagePlugin from .helper import assert_image_equal, hopper, is_pypy diff --git a/Tests/test_file_sun.py b/Tests/test_file_sun.py index 03e26ef8b..8421106a2 100644 --- a/Tests/test_file_sun.py +++ b/Tests/test_file_sun.py @@ -1,6 +1,7 @@ import os import pytest + from PIL import Image, SunImagePlugin from .helper import assert_image_equal, assert_image_similar, hopper @@ -45,7 +46,7 @@ def test_others(): with Image.open(path) as im: im.load() assert isinstance(im, SunImagePlugin.SunImageFile) - target_path = "%s.png" % os.path.splitext(path)[0] + target_path = f"{os.path.splitext(path)[0]}.png" # im.save(target_file) with Image.open(target_path) as target: assert_image_equal(im, target) diff --git a/Tests/test_file_tar.py b/Tests/test_file_tar.py index 3fe0cd04e..02001e5b1 100644 --- a/Tests/test_file_tar.py +++ b/Tests/test_file_tar.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, TarIO, features from .helper import is_pypy diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index 4919ad766..465e13316 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -3,6 +3,7 @@ from glob import glob from itertools import product import pytest + from PIL import Image from .helper import assert_image_equal, hopper @@ -35,9 +36,7 @@ def test_sanity(tmp_path): assert_image_equal(saved_im, original_im) - png_paths = glob( - os.path.join(_TGA_DIR_COMMON, "*x*_{}.png".format(mode.lower())) - ) + png_paths = glob(os.path.join(_TGA_DIR_COMMON, f"*x*_{mode.lower()}.png")) for png_path in png_paths: with Image.open(png_path) as reference_im: diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 7aa55dad0..594115042 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -1,8 +1,8 @@ -import logging import os from io import BytesIO import pytest + from PIL import Image, TiffImagePlugin from PIL.TiffImagePlugin import RESOLUTION_UNIT, X_RESOLUTION, Y_RESOLUTION @@ -16,8 +16,6 @@ from .helper import ( is_win32, ) -logger = logging.getLogger(__name__) - class TestFileTiff: def test_sanity(self, tmp_path): @@ -227,8 +225,8 @@ class TestFileTiff: assert im.getpixel((0, 1)) == 0 def test_12bit_rawmode(self): - """ Are we generating the same interpretation - of the image as Imagemagick is? """ + """Are we generating the same interpretation + of the image as Imagemagick is?""" with Image.open("Tests/images/12bit.cropped.tif") as im: # to make the target -- diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index d57f63717..0f7f8adf1 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -2,6 +2,7 @@ import io import struct import pytest + from PIL import Image, TiffImagePlugin, TiffTags from PIL.TiffImagePlugin import IFDRational @@ -11,10 +12,10 @@ TAG_IDS = {info.name: info.value for info in TiffTags.TAGS_V2.values()} def test_rt_metadata(tmp_path): - """ Test writing arbitrary metadata into the tiff image directory - Use case is ImageJ private tags, one numeric, one arbitrary - data. https://github.com/python-pillow/Pillow/issues/291 - """ + """Test writing arbitrary metadata into the tiff image directory + Use case is ImageJ private tags, one numeric, one arbitrary + data. https://github.com/python-pillow/Pillow/issues/291 + """ img = hopper() @@ -144,16 +145,16 @@ def test_write_metadata(tmp_path): assert_deep_equal( original[tag], value, - "{} didn't roundtrip, {}, {}".format(tag, original[tag], value), + f"{tag} didn't roundtrip, {original[tag]}, {value}", ) else: - assert original[tag] == value, "{} didn't roundtrip, {}, {}".format( - tag, original[tag], value - ) + assert ( + original[tag] == value + ), f"{tag} didn't roundtrip, {original[tag]}, {value}" for tag, value in original.items(): if tag not in ignored: - assert value == reloaded[tag], "%s didn't roundtrip" % tag + assert value == reloaded[tag], f"{tag} didn't roundtrip" def test_change_stripbytecounts_tag_type(tmp_path): diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 25a4bb8da..11fbd9fd5 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -2,6 +2,7 @@ import io import re import pytest + from PIL import Image, WebPImagePlugin, features from .helper import ( diff --git a/Tests/test_file_webp_alpha.py b/Tests/test_file_webp_alpha.py index c624156df..362edac1a 100644 --- a/Tests/test_file_webp_alpha.py +++ b/Tests/test_file_webp_alpha.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import assert_image_equal, assert_image_similar, hopper diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index cd272f154..26e903488 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import ( diff --git a/Tests/test_file_webp_lossless.py b/Tests/test_file_webp_lossless.py index 4d06f53b1..2da443628 100644 --- a/Tests/test_file_webp_lossless.py +++ b/Tests/test_file_webp_lossless.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import assert_image_equal, hopper diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 3339cbfd3..d18225680 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, WmfImagePlugin from .helper import assert_image_similar, hopper diff --git a/Tests/test_file_xbm.py b/Tests/test_file_xbm.py index 23a540569..487920a92 100644 --- a/Tests/test_file_xbm.py +++ b/Tests/test_file_xbm.py @@ -1,6 +1,7 @@ from io import BytesIO import pytest + from PIL import Image from .helper import hopper diff --git a/Tests/test_file_xpm.py b/Tests/test_file_xpm.py index 187440d4e..8595b07eb 100644 --- a/Tests/test_file_xpm.py +++ b/Tests/test_file_xpm.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, XpmImagePlugin from .helper import assert_image_similar, hopper diff --git a/Tests/test_file_xvthumb.py b/Tests/test_file_xvthumb.py index 7c8c45113..ae53d2b63 100644 --- a/Tests/test_file_xvthumb.py +++ b/Tests/test_file_xvthumb.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, XVThumbImagePlugin from .helper import assert_image_similar, hopper diff --git a/Tests/test_font_bdf.py b/Tests/test_font_bdf.py index 4be39c383..1e7caee32 100644 --- a/Tests/test_font_bdf.py +++ b/Tests/test_font_bdf.py @@ -1,4 +1,5 @@ import pytest + from PIL import BdfFontFile, FontFile filename = "Tests/images/courB08.bdf" diff --git a/Tests/test_font_pcf.py b/Tests/test_font_pcf.py index a60163713..4db73e56e 100644 --- a/Tests/test_font_pcf.py +++ b/Tests/test_font_pcf.py @@ -1,6 +1,7 @@ import os import pytest + from PIL import FontFile, Image, ImageDraw, ImageFont, PcfFontFile from .helper import assert_image_equal, assert_image_similar, skip_unless_feature diff --git a/Tests/test_font_pcf_charsets.py b/Tests/test_font_pcf_charsets.py index 4a39803be..d7d1bf200 100644 --- a/Tests/test_font_pcf_charsets.py +++ b/Tests/test_font_pcf_charsets.py @@ -47,11 +47,11 @@ def save_font(request, tmp_path, encoding): font.save(tempname) with Image.open(tempname.replace(".pil", ".pbm")) as loaded: - with Image.open("Tests/fonts/ter-x20b-%s.pbm" % encoding) as target: + with Image.open(f"Tests/fonts/ter-x20b-{encoding}.pbm") as target: assert_image_equal(loaded, target) with open(tempname, "rb") as f_loaded: - with open("Tests/fonts/ter-x20b-%s.pil" % encoding, "rb") as f_target: + with open(f"Tests/fonts/ter-x20b-{encoding}.pil", "rb") as f_target: assert f_loaded.read() == f_target.read() return tempname diff --git a/Tests/test_format_hsv.py b/Tests/test_format_hsv.py index d10b1acfd..3b9c8b071 100644 --- a/Tests/test_format_hsv.py +++ b/Tests/test_format_hsv.py @@ -85,7 +85,10 @@ def test_wedge(): im.getchannel(0), comparable.getchannel(0), 1, "Hue conversion is wrong" ) assert_image_similar( - im.getchannel(1), comparable.getchannel(1), 1, "Saturation conversion is wrong", + im.getchannel(1), + comparable.getchannel(1), + 1, + "Saturation conversion is wrong", ) assert_image_similar( im.getchannel(2), comparable.getchannel(2), 1, "Value conversion is wrong" @@ -113,7 +116,10 @@ def test_convert(): im.getchannel(0), comparable.getchannel(0), 1, "Hue conversion is wrong" ) assert_image_similar( - im.getchannel(1), comparable.getchannel(1), 1, "Saturation conversion is wrong", + im.getchannel(1), + comparable.getchannel(1), + 1, + "Saturation conversion is wrong", ) assert_image_similar( im.getchannel(2), comparable.getchannel(2), 1, "Value conversion is wrong" @@ -126,11 +132,20 @@ def test_hsv_to_rgb(): comparable = to_rgb_colorsys(comparable) assert_image_similar( - converted.getchannel(0), comparable.getchannel(0), 3, "R conversion is wrong", + converted.getchannel(0), + comparable.getchannel(0), + 3, + "R conversion is wrong", ) assert_image_similar( - converted.getchannel(1), comparable.getchannel(1), 3, "G conversion is wrong", + converted.getchannel(1), + comparable.getchannel(1), + 3, + "G conversion is wrong", ) assert_image_similar( - converted.getchannel(2), comparable.getchannel(2), 3, "B conversion is wrong", + converted.getchannel(2), + comparable.getchannel(2), + 3, + "B conversion is wrong", ) diff --git a/Tests/test_image.py b/Tests/test_image.py index 6d29ac80e..6d188e740 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -3,8 +3,9 @@ import os import shutil import tempfile -import PIL import pytest + +import PIL from PIL import Image, ImageDraw, ImagePalette, ImageShow, UnidentifiedImageError from .helper import ( @@ -466,18 +467,6 @@ class TestImage: with pytest.raises(ValueError): Image.core.fill("RGB", (2, -2), (0, 0, 0)) - def test_offset_not_implemented(self): - # Arrange - with hopper() as im: - - # Act / Assert - with pytest.raises(NotImplementedError): - im.offset(None) - - def test_fromstring(self): - with pytest.raises(NotImplementedError): - Image.fromstring() - def test_linear_gradient_wrong_mode(self): # Arrange wrong_mode = "RGB" @@ -718,7 +707,8 @@ class TestImage: } @pytest.mark.parametrize( - "test_module", [PIL, Image], + "test_module", + [PIL, Image], ) def test_pillow_version(self, test_module): with pytest.warns(DeprecationWarning): @@ -746,7 +736,7 @@ class TestImage: assert test_module.PILLOW_VERSION > "7.0.0" def test_overrun(self): - """ For overrun completeness, test as: + """For overrun completeness, test as: valgrind pytest -qq Tests/test_image.py::TestImage::test_overrun | grep decode.c """ for file in [ diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 25cc9fef4..3f0c6ab3b 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -2,9 +2,11 @@ import ctypes import os import subprocess import sys -from distutils import ccompiler, sysconfig +import sysconfig import pytest +from setuptools.command.build_ext import new_compiler + from PIL import Image from .helper import assert_image_equal, hopper, is_win32, on_ci @@ -15,8 +17,9 @@ if os.environ.get("PYTHONOPTIMIZE") == "2": cffi = None else: try: - from PIL import PyAccess import cffi + + from PIL import PyAccess except ImportError: cffi = None @@ -125,14 +128,13 @@ class TestImageGetPixel(AccessTest): im.putpixel((0, 0), c) assert ( im.getpixel((0, 0)) == c - ), "put/getpixel roundtrip failed for mode {}, color {}".format(mode, c) + ), f"put/getpixel roundtrip failed for mode {mode}, color {c}" # check putpixel negative index im.putpixel((-1, -1), c) - assert im.getpixel((-1, -1)) == c, ( - "put/getpixel roundtrip negative index failed for mode %s, color %s" - % (mode, c) - ) + assert ( + im.getpixel((-1, -1)) == c + ), f"put/getpixel roundtrip negative index failed for mode {mode}, color {c}" # Check 0 im = Image.new(mode, (0, 0), None) @@ -150,11 +152,11 @@ class TestImageGetPixel(AccessTest): im = Image.new(mode, (1, 1), c) assert ( im.getpixel((0, 0)) == c - ), "initial color failed for mode {}, color {} ".format(mode, c) + ), f"initial color failed for mode {mode}, color {c} " # check initial color negative index assert ( im.getpixel((-1, -1)) == c - ), "initial color failed with negative index for mode %s, color %s " % (mode, c) + ), f"initial color failed with negative index for mode {mode}, color {c} " # Check 0 im = Image.new(mode, (0, 0), c) @@ -359,13 +361,12 @@ int main(int argc, char* argv[]) % sys.prefix.replace("\\", "\\\\") ) - compiler = ccompiler.new_compiler() - compiler.add_include_dir(sysconfig.get_python_inc()) + compiler = new_compiler() + compiler.add_include_dir(sysconfig.get_config_var("INCLUDEPY")) - libdir = sysconfig.get_config_var( - "LIBDIR" - ) or sysconfig.get_python_inc().replace("include", "libs") - print(libdir) + libdir = sysconfig.get_config_var("LIBDIR") or sysconfig.get_config_var( + "INCLUDEPY" + ).replace("include", "libs") compiler.add_library_dir(libdir) objects = compiler.compile(["embed_pil.c"]) compiler.link_executable(objects, "embed_pil") diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index bf6d88a97..980458407 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import hopper diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index cf83922b6..6fe1bd962 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import assert_image, assert_image_equal, assert_image_similar, hopper diff --git a/Tests/test_image_crop.py b/Tests/test_image_crop.py index 3a2ce150d..e2228758c 100644 --- a/Tests/test_image_crop.py +++ b/Tests/test_image_crop.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import assert_image_equal, hopper diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index ed71ea968..df8c353f3 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageFilter from .helper import assert_image_equal, hopper diff --git a/Tests/test_image_frombytes.py b/Tests/test_image_frombytes.py index faf94ac77..7fb05cda7 100644 --- a/Tests/test_image_frombytes.py +++ b/Tests/test_image_frombytes.py @@ -1,4 +1,3 @@ -import pytest from PIL import Image from .helper import assert_image_equal, hopper @@ -9,8 +8,3 @@ def test_sanity(): im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes()) assert_image_equal(im1, im2) - - -def test_not_implemented(): - with pytest.raises(NotImplementedError): - Image.fromstring() diff --git a/Tests/test_image_fromqimage.py b/Tests/test_image_fromqimage.py index 170d49ae1..5ad5b5c3c 100644 --- a/Tests/test_image_fromqimage.py +++ b/Tests/test_image_fromqimage.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageQt from .helper import assert_image_equal, hopper diff --git a/Tests/test_image_load.py b/Tests/test_image_load.py index efb9a1452..f7fe99bb4 100644 --- a/Tests/test_image_load.py +++ b/Tests/test_image_load.py @@ -1,6 +1,8 @@ +import logging import os import pytest + from PIL import Image from .helper import hopper @@ -22,6 +24,14 @@ def test_close(): im.getpixel((0, 0)) +def test_close_after_load(caplog): + im = Image.open("Tests/images/hopper.gif") + im.load() + with caplog.at_level(logging.DEBUG): + im.close() + assert len(caplog.records) == 0 + + def test_contextmanager(): fn = None with Image.open("Tests/images/hopper.gif") as im: diff --git a/Tests/test_image_point.py b/Tests/test_image_point.py index fe868b7c2..51108ead2 100644 --- a/Tests/test_image_point.py +++ b/Tests/test_image_point.py @@ -24,9 +24,9 @@ def test_sanity(): def test_16bit_lut(): - """ Tests for 16 bit -> 8 bit lut for converting I->L images - see https://github.com/python-pillow/Pillow/issues/440 - """ + """Tests for 16 bit -> 8 bit lut for converting I->L images + see https://github.com/python-pillow/Pillow/issues/440 + """ im = hopper("I") im.point(list(range(256)) * 256, "L") diff --git a/Tests/test_image_putpalette.py b/Tests/test_image_putpalette.py index 7b05e88b6..32f8de2c0 100644 --- a/Tests/test_image_putpalette.py +++ b/Tests/test_image_putpalette.py @@ -1,4 +1,5 @@ import pytest + from PIL import ImagePalette from .helper import hopper diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index 96fa143a9..192617a52 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import assert_image, assert_image_similar, hopper diff --git a/Tests/test_image_reduce.py b/Tests/test_image_reduce.py index 0f92b87f8..b4eebc142 100644 --- a/Tests/test_image_reduce.py +++ b/Tests/test_image_reduce.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageMath, ImageMode from .helper import convert_to_comparable, skip_unless_feature @@ -161,8 +162,8 @@ def compare_reduce_with_reference(im, factor, average_diff=0.4, max_diff=1): def assert_compare_images(a, b, max_average_diff, max_diff=255): - assert a.mode == b.mode, "got mode %r, expected %r" % (a.mode, b.mode) - assert a.size == b.size, "got size %r, expected %r" % (a.size, b.size) + assert a.mode == b.mode, f"got mode {repr(a.mode)}, expected {repr(b.mode)}" + assert a.size == b.size, f"got size {repr(a.size)}, expected {repr(b.size)}" a, b = convert_to_comparable(a, b) @@ -175,16 +176,15 @@ def assert_compare_images(a, b, max_average_diff, max_diff=255): a.size[0] * a.size[1] ) msg = ( - "average pixel value difference {:.4f} > expected {:.4f} " - "for '{}' band".format(average_diff, max_average_diff, band) + f"average pixel value difference {average_diff:.4f} > " + f"expected {max_average_diff:.4f} for '{band}' band" ) assert max_average_diff >= average_diff, msg last_diff = [i for i, num in enumerate(ch_hist) if num > 0][-1] - assert ( - max_diff >= last_diff - ), "max pixel value difference {} > expected {} for '{}' band".format( - last_diff, max_diff, band + assert max_diff >= last_diff, ( + f"max pixel value difference {last_diff} > expected {max_diff} " + f"for '{band}' band" ) diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index 35eae128b..ef4ca4101 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -1,6 +1,7 @@ from contextlib import contextmanager import pytest + from PIL import Image, ImageDraw from .helper import assert_image_equal, assert_image_similar, hopper @@ -81,15 +82,16 @@ class TestImagingCoreResampleAccuracy: for y in range(case.size[1]): for x in range(case.size[0]): if c_px[x, y] != s_px[x, y]: - message = "\nHave: \n{}\n\nExpected: \n{}".format( - self.serialize_image(case), self.serialize_image(sample) + message = ( + f"\nHave: \n{self.serialize_image(case)}\n" + f"\nExpected: \n{self.serialize_image(sample)}" ) assert s_px[x, y] == c_px[x, y], message def serialize_image(self, image): s_px = image.load() return "\n".join( - " ".join("{:02x}".format(s_px[x, y]) for x in range(image.size[0])) + " ".join(f"{s_px[x, y]:02x}" for x in range(image.size[0])) for y in range(image.size[1]) ) @@ -229,7 +231,7 @@ class TestCoreResampleConsistency: for x in range(channel.size[0]): for y in range(channel.size[1]): if px[x, y] != color: - message = "{} != {} for pixel {}".format(px[x, y], color, (x, y)) + message = f"{px[x, y]} != {color} for pixel {(x, y)}" assert px[x, y] == color, message def test_8u(self): @@ -268,10 +270,9 @@ class TestCoreResampleAlphaCorrect: px = i.load() for y in range(i.size[1]): used_colors = {px[x, y][0] for x in range(i.size[0])} - assert 256 == len( - used_colors - ), "All colors should present in resized image. Only {} on {} line.".format( - len(used_colors), y + assert 256 == len(used_colors), ( + "All colors should be present in resized image. " + f"Only {len(used_colors)} on {y} line." ) @pytest.mark.xfail(reason="Current implementation isn't precise enough") @@ -307,8 +308,9 @@ class TestCoreResampleAlphaCorrect: for y in range(i.size[1]): for x in range(i.size[0]): if px[x, y][-1] != 0 and px[x, y][:-1] != clean_pixel: - message = "pixel at ({}, {}) is differ:\n{}\n{}".format( - x, y, px[x, y], clean_pixel + message = ( + f"pixel at ({x}, {y}) is different:\n" + f"{px[x, y]}\n{clean_pixel}" ) assert px[x, y][:3] == clean_pixel, message @@ -503,7 +505,7 @@ class TestCoreResampleBox: ]: res = im.resize(size, Image.LANCZOS, box) assert res.size == size - assert_image_equal(res, im.crop(box), ">>> {} {}".format(size, box)) + assert_image_equal(res, im.crop(box), f">>> {size} {box}") def test_no_passthrough(self): # When resize is required @@ -519,9 +521,7 @@ class TestCoreResampleBox: assert res.size == size with pytest.raises(AssertionError, match=r"difference \d"): # check that the difference at least that much - assert_image_similar( - res, im.crop(box), 20, ">>> {} {}".format(size, box) - ) + assert_image_similar(res, im.crop(box), 20, f">>> {size} {box}") def test_skip_horizontal(self): # Can skip resize for one dimension @@ -541,7 +541,7 @@ class TestCoreResampleBox: res, im.crop(box).resize(size, flt), 0.4, - ">>> {} {} {}".format(size, box, flt), + f">>> {size} {box} {flt}", ) def test_skip_vertical(self): @@ -562,5 +562,5 @@ class TestCoreResampleBox: res, im.crop(box).resize(size, flt), 0.4, - ">>> {} {} {}".format(size, box, flt), + f">>> {size} {box} {flt}", ) diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index ad4be135a..a49abe1b9 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -4,6 +4,7 @@ Tests for resize functionality. from itertools import permutations import pytest + from PIL import Image from .helper import assert_image_equal, assert_image_similar, hopper diff --git a/Tests/test_image_thumbnail.py b/Tests/test_image_thumbnail.py index da63efe55..c42310c32 100644 --- a/Tests/test_image_thumbnail.py +++ b/Tests/test_image_thumbnail.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import ( diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index 3409d86f0..3ee51178d 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -1,6 +1,7 @@ import math import pytest + from PIL import Image, ImageTransform from .helper import assert_image_equal, assert_image_similar, hopper diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index e549f0922..e9149b843 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -4,6 +4,7 @@ import re from io import BytesIO import pytest + from PIL import Image, ImageMode, features from .helper import assert_image, assert_image_equal, assert_image_similar, hopper @@ -436,7 +437,7 @@ def test_extended_information(): def test_profile_typesafety(): - """ Profile init type safety + """Profile init type safety prepatch, these would segfault, postpatch they should emit a typeerror """ diff --git a/Tests/test_imagecolor.py b/Tests/test_imagecolor.py index d2fd07c81..b5d693796 100644 --- a/Tests/test_imagecolor.py +++ b/Tests/test_imagecolor.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageColor diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 56b189ecd..271a1629d 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -1,6 +1,7 @@ import os.path import pytest + from PIL import Image, ImageColor, ImageDraw, ImageFont from .helper import ( @@ -181,7 +182,7 @@ def helper_chord(mode, bbox, start, end): # Arrange im = Image.new(mode, (W, H)) draw = ImageDraw.Draw(im) - expected = "Tests/images/imagedraw_chord_{}.png".format(mode) + expected = f"Tests/images/imagedraw_chord_{mode}.png" # Act draw.chord(bbox, start, end, fill="red", outline="yellow") @@ -243,7 +244,7 @@ def helper_ellipse(mode, bbox): # Arrange im = Image.new(mode, (W, H)) draw = ImageDraw.Draw(im) - expected = "Tests/images/imagedraw_ellipse_{}.png".format(mode) + expected = f"Tests/images/imagedraw_ellipse_{mode}.png" # Act draw.ellipse(bbox, fill="green", outline="blue") @@ -513,7 +514,7 @@ def test_polygon_kite(): # Arrange im = Image.new(mode, (W, H)) draw = ImageDraw.Draw(im) - expected = "Tests/images/imagedraw_polygon_kite_{}.png".format(mode) + expected = f"Tests/images/imagedraw_polygon_kite_{mode}.png" # Act draw.polygon(KITE_POINTS, fill="blue", outline="yellow") @@ -666,7 +667,10 @@ def test_floodfill_border(): # Act ImageDraw.floodfill( - im, centre_point, ImageColor.getrgb("red"), border=ImageColor.getrgb("black"), + im, + centre_point, + ImageColor.getrgb("red"), + border=ImageColor.getrgb("black"), ) # Assert @@ -1087,7 +1091,5 @@ def test_same_color_outline(): draw_method(*args) # Assert - expected = "Tests/images/imagedraw_outline_{}_{}.png".format( - operation, mode - ) + expected = f"Tests/images/imagedraw_outline_{operation}_{mode}.png" assert_image_similar_tofile(im, expected, 1) diff --git a/Tests/test_imagedraw2.py b/Tests/test_imagedraw2.py index 72cbb79b8..0d9f16ce8 100644 --- a/Tests/test_imagedraw2.py +++ b/Tests/test_imagedraw2.py @@ -55,7 +55,7 @@ def helper_ellipse(mode, bbox): draw = ImageDraw2.Draw(im) pen = ImageDraw2.Pen("blue", width=2) brush = ImageDraw2.Brush("green") - expected = "Tests/images/imagedraw_ellipse_{}.png".format(mode) + expected = f"Tests/images/imagedraw_ellipse_{mode}.png" # Act draw.ellipse(bbox, pen, brush) diff --git a/Tests/test_imageenhance.py b/Tests/test_imageenhance.py index 32ab05f17..8bc94401e 100644 --- a/Tests/test_imageenhance.py +++ b/Tests/test_imageenhance.py @@ -35,7 +35,7 @@ def _check_alpha(im, original, op, amount): assert_image_equal( im.getchannel("A"), original.getchannel("A"), - "Diff on {}: {}".format(op, amount), + f"Diff on {op}: {amount}", ) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 48fecc26e..b4107e8e3 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -1,6 +1,7 @@ from io import BytesIO import pytest + from PIL import EpsImagePlugin, Image, ImageFile, features from .helper import ( @@ -243,3 +244,8 @@ class TestPyDecoder: im = MockImageFile(buf) assert im.format is None assert im.get_format_mimetype() is None + + def test_oserror(self): + im = Image.new("RGB", (1, 1)) + with pytest.raises(OSError): + im.save(BytesIO(), "JPEG2000") diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 6668a100b..aa7ec6fa6 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -7,6 +7,7 @@ from io import BytesIO import pytest from packaging.version import parse as parse_version + from PIL import Image, ImageDraw, ImageFont, features from .helper import ( diff --git a/Tests/test_imagefont_bitmap.py b/Tests/test_imagefont_bitmap.py index c4032d55d..0ba682885 100644 --- a/Tests/test_imagefont_bitmap.py +++ b/Tests/test_imagefont_bitmap.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageDraw, ImageFont from .helper import assert_image_similar @@ -33,6 +34,9 @@ def test_similar(): (0, size_final[1] - size_bitmap[1]), text, fill=(0, 0, 0), font=font_bitmap ) draw_outline.text( - (0, size_final[1] - size_outline[1]), text, fill=(0, 0, 0), font=font_outline, + (0, size_final[1] - size_outline[1]), + text, + fill=(0, 0, 0), + font=font_outline, ) assert_image_similar(im_bitmap, im_outline, 20) diff --git a/Tests/test_imagefontctl.py b/Tests/test_imagefontctl.py index 386dd3be6..edd6fab57 100644 --- a/Tests/test_imagefontctl.py +++ b/Tests/test_imagefontctl.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageDraw, ImageFont from .helper import assert_image_similar, skip_unless_feature diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index ae1277ced..c36285451 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -3,6 +3,7 @@ import subprocess import sys import pytest + from PIL import Image, ImageGrab from .helper import assert_image, assert_image_equal_tofile, skip_unless_feature diff --git a/Tests/test_imagemath.py b/Tests/test_imagemath.py index bc4f1af28..239806796 100644 --- a/Tests/test_imagemath.py +++ b/Tests/test_imagemath.py @@ -3,7 +3,7 @@ from PIL import Image, ImageMath def pixel(im): if hasattr(im, "im"): - return "{} {!r}".format(im.mode, im.getpixel((0, 0))) + return "{} {}".format(im.mode, repr(im.getpixel((0, 0)))) else: if isinstance(im, int): return int(im) # hack to deal with booleans diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index 62119e4b3..087c39e01 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -1,5 +1,6 @@ # Test the ImageMorphology functionality import pytest + from PIL import Image, ImageMorph, _imagingmorph from .helper import assert_image_equal, hopper @@ -64,7 +65,7 @@ def create_lut(): for op in ("corner", "dilation4", "dilation8", "erosion4", "erosion8", "edge"): lb = ImageMorph.LutBuilder(op_name=op) lut = lb.build_lut() - with open("Tests/images/%s.lut" % op, "wb") as f: + with open(f"Tests/images/{op}.lut", "wb") as f: f.write(lut) @@ -75,7 +76,7 @@ def test_lut(): assert lb.get_lut() is None lut = lb.build_lut() - with open("Tests/images/%s.lut" % op, "rb") as f: + with open(f"Tests/images/{op}.lut", "rb") as f: assert lut == bytearray(f.read()) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 864df447e..f17bfdd2f 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -1,5 +1,6 @@ import pytest -from PIL import Image, ImageOps, features + +from PIL import Image, ImageDraw, ImageOps, ImageStat, features from .helper import ( assert_image_equal, @@ -24,7 +25,9 @@ def test_sanity(): ImageOps.autocontrast(hopper("RGB")) ImageOps.autocontrast(hopper("L"), cutoff=10) + ImageOps.autocontrast(hopper("L"), cutoff=(2, 10)) ImageOps.autocontrast(hopper("L"), ignore=[0, 255]) + ImageOps.autocontrast(hopper("L"), mask=hopper("L")) ImageOps.colorize(hopper("L"), (0, 0, 0), (255, 255, 255)) ImageOps.colorize(hopper("L"), "black", "white") @@ -311,3 +314,51 @@ def test_autocontrast_cutoff(): assert autocontrast(10) == autocontrast((10, 10)) assert autocontrast(10) != autocontrast((1, 10)) + + +def test_autocontrast_mask_toy_input(): + # Test the mask argument of autocontrast + with Image.open("Tests/images/bw_gradient.png") as img: + + rect_mask = Image.new("L", img.size, 0) + draw = ImageDraw.Draw(rect_mask) + x0 = img.size[0] // 4 + y0 = img.size[1] // 4 + x1 = 3 * img.size[0] // 4 + y1 = 3 * img.size[1] // 4 + draw.rectangle((x0, y0, x1, y1), fill=255) + + result = ImageOps.autocontrast(img, mask=rect_mask) + result_nomask = ImageOps.autocontrast(img) + + assert result != result_nomask + assert ImageStat.Stat(result, mask=rect_mask).median == [127] + assert ImageStat.Stat(result_nomask).median == [128] + + +def test_auto_contrast_mask_real_input(): + # Test the autocontrast with a rectangular mask + with Image.open("Tests/images/iptc.jpg") as img: + + rect_mask = Image.new("L", img.size, 0) + draw = ImageDraw.Draw(rect_mask) + x0, y0 = img.size[0] // 2, img.size[1] // 2 + x1, y1 = img.size[0] - 40, img.size[1] + draw.rectangle((x0, y0, x1, y1), fill=255) + + result = ImageOps.autocontrast(img, mask=rect_mask) + result_nomask = ImageOps.autocontrast(img) + + assert result_nomask != result + assert_tuple_approx_equal( + ImageStat.Stat(result, mask=rect_mask).median, + [195, 202, 184], + threshold=2, + msg="autocontrast with mask pixel incorrect", + ) + assert_tuple_approx_equal( + ImageStat.Stat(result_nomask).median, + [119, 106, 79], + threshold=2, + msg="autocontrast without mask pixel incorrect", + ) diff --git a/Tests/test_imageops_usm.py b/Tests/test_imageops_usm.py index 61f8dc2ba..8837ed2a2 100644 --- a/Tests/test_imageops_usm.py +++ b/Tests/test_imageops_usm.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageFilter diff --git a/Tests/test_imagepalette.py b/Tests/test_imagepalette.py index 4ef2d3ffd..a2b0d2b02 100644 --- a/Tests/test_imagepalette.py +++ b/Tests/test_imagepalette.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImagePalette from .helper import assert_image_equal diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index 52af16455..7cc89ae39 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -2,6 +2,7 @@ import array import struct import pytest + from PIL import Image, ImagePath diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py index d723690ef..c39bb0a06 100644 --- a/Tests/test_imageqt.py +++ b/Tests/test_imageqt.py @@ -1,4 +1,5 @@ import pytest + from PIL import ImageQt from .helper import hopper diff --git a/Tests/test_imagesequence.py b/Tests/test_imagesequence.py index b3fe9df97..7cf237b46 100644 --- a/Tests/test_imagesequence.py +++ b/Tests/test_imagesequence.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageSequence, TiffImagePlugin from .helper import assert_image_equal, hopper, skip_unless_feature diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index fddc73bd1..78e80f521 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageShow from .helper import hopper, is_win32, on_ci @@ -18,7 +19,8 @@ def test_register(): @pytest.mark.parametrize( - "order", [-1, 0], + "order", + [-1, 0], ) def test_viewer_show(order): class TestViewer(ImageShow.Viewer): @@ -40,7 +42,8 @@ def test_viewer_show(order): @pytest.mark.skipif( - not on_ci() or is_win32(), reason="Only run on CIs; hangs on Windows CIs", + not on_ci() or is_win32(), + reason="Only run on CIs; hangs on Windows CIs", ) def test_show(): for mode in ("1", "I;16", "LA", "RGB", "RGBA"): diff --git a/Tests/test_imagestat.py b/Tests/test_imagestat.py index 6c70193ce..9474ff6f9 100644 --- a/Tests/test_imagestat.py +++ b/Tests/test_imagestat.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageStat from .helper import hopper diff --git a/Tests/test_imagetk.py b/Tests/test_imagetk.py index d13920c16..928b8cbd1 100644 --- a/Tests/test_imagetk.py +++ b/Tests/test_imagetk.py @@ -1,13 +1,14 @@ import pytest + from PIL import Image from .helper import assert_image_equal, hopper try: - from PIL import ImageTk - import tkinter as tk + from PIL import ImageTk + dir(ImageTk) HAS_TK = True except (OSError, ImportError): @@ -26,7 +27,7 @@ def setup_module(): tk.Frame() # root = tk.Tk() except tk.TclError as v: - pytest.skip("TCL Error: %s" % v) + pytest.skip(f"TCL Error: {v}") def test_kw(): diff --git a/Tests/test_imagewin.py b/Tests/test_imagewin.py index b1ddc75e9..9d64d17a3 100644 --- a/Tests/test_imagewin.py +++ b/Tests/test_imagewin.py @@ -1,4 +1,5 @@ import pytest + from PIL import ImageWin from .helper import hopper, is_win32 diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 7115e62ad..37ed3659d 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py index 8e3c1fda9..8a1460346 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -1,6 +1,7 @@ import sys import pytest + from PIL import Image X = 255 diff --git a/Tests/test_locale.py b/Tests/test_locale.py index c5e54883d..7a07fbbe0 100644 --- a/Tests/test_locale.py +++ b/Tests/test_locale.py @@ -1,6 +1,7 @@ import locale import pytest + from PIL import Image # ref https://github.com/python-pillow/Pillow/issues/272 diff --git a/Tests/test_map.py b/Tests/test_map.py index bdb59bfe0..2b65fb3f9 100644 --- a/Tests/test_map.py +++ b/Tests/test_map.py @@ -1,6 +1,7 @@ import sys import pytest + from PIL import Image from .helper import is_win32 diff --git a/Tests/test_mode_i16.py b/Tests/test_mode_i16.py index 19e16f2c4..0571aabf4 100644 --- a/Tests/test_mode_i16.py +++ b/Tests/test_mode_i16.py @@ -15,9 +15,9 @@ def verify(im1): xy = x, y p1 = pix1[xy] p2 = pix2[xy] - assert p1 == p2, "got {!r} from mode {} at {}, expected {!r}".format( - p1, im1.mode, xy, p2 - ) + assert ( + p1 == p2 + ), f"got {repr(p1)} from mode {im1.mode} at {xy}, expected {repr(p2)}" def test_basic(tmp_path): diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 56addca1b..da367fa46 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image from .helper import assert_deep_equal, assert_image, hopper diff --git a/Tests/test_pdfparser.py b/Tests/test_pdfparser.py index f5cd403d5..2d428e95f 100644 --- a/Tests/test_pdfparser.py +++ b/Tests/test_pdfparser.py @@ -1,6 +1,7 @@ import time import pytest + from PIL.PdfParser import ( IndirectObjectDef, IndirectReference, diff --git a/Tests/test_pickle.py b/Tests/test_pickle.py index dd241fd74..a10dcec8c 100644 --- a/Tests/test_pickle.py +++ b/Tests/test_pickle.py @@ -1,6 +1,7 @@ import pickle import pytest + from PIL import Image from .helper import skip_unless_feature diff --git a/Tests/test_pyroma.py b/Tests/test_pyroma.py index f4302350d..aa05c2cfd 100644 --- a/Tests/test_pyroma.py +++ b/Tests/test_pyroma.py @@ -1,4 +1,5 @@ import pytest + from PIL import __version__ pyroma = pytest.importorskip("pyroma", reason="Pyroma not installed") diff --git a/Tests/test_qt_image_toqimage.py b/Tests/test_qt_image_toqimage.py index 4c98bf0b4..8d599f9bf 100644 --- a/Tests/test_qt_image_toqimage.py +++ b/Tests/test_qt_image_toqimage.py @@ -1,4 +1,5 @@ import pytest + from PIL import Image, ImageQt from .helper import assert_image_equal, hopper @@ -12,10 +13,10 @@ if ImageQt.qt_is_installed: try: from PyQt5 import QtGui - from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication + from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget except (ImportError, RuntimeError): from PySide2 import QtGui - from PySide2.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication + from PySide2.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget def test_sanity(tmp_path): @@ -42,7 +43,7 @@ def test_sanity(tmp_path): continue # Test saving the file - tempfile = str(tmp_path / "temp_{}.png".format(mode)) + tempfile = str(tmp_path / f"temp_{mode}.png") data.save(tempfile) # Check that it actually worked. diff --git a/Tests/test_qt_image_toqpixmap.py b/Tests/test_qt_image_toqpixmap.py index af281da69..f38cc7f13 100644 --- a/Tests/test_qt_image_toqpixmap.py +++ b/Tests/test_qt_image_toqpixmap.py @@ -16,5 +16,5 @@ class TestToQPixmap(PillowQPixmapTestCase): assert not data.isNull() # Test saving the file - tempfile = str(tmp_path / "temp_{}.png".format(mode)) + tempfile = str(tmp_path / f"temp_{mode}.png") data.save(tempfile) diff --git a/Tests/test_sgi_crash.py b/Tests/test_sgi_crash.py index b1a3e1515..2b671244a 100644 --- a/Tests/test_sgi_crash.py +++ b/Tests/test_sgi_crash.py @@ -1,5 +1,6 @@ #!/usr/bin/env python import pytest + from PIL import Image diff --git a/Tests/test_shell_injection.py b/Tests/test_shell_injection.py index 45c60fa10..d25d42dfc 100644 --- a/Tests/test_shell_injection.py +++ b/Tests/test_shell_injection.py @@ -1,6 +1,7 @@ import shutil import pytest + from PIL import GifImagePlugin, Image, JpegImagePlugin from .helper import cjpeg_available, djpeg_available, is_win32, netpbm_available diff --git a/Tests/test_tiff_ifdrational.py b/Tests/test_tiff_ifdrational.py index 707284d7b..1697a8d49 100644 --- a/Tests/test_tiff_ifdrational.py +++ b/Tests/test_tiff_ifdrational.py @@ -29,6 +29,12 @@ def test_sanity(): _test_equal(1, 2, IFDRational(1, 2)) +def test_ranges(): + for num in range(1, 10): + for denom in range(1, 10): + assert IFDRational(num, denom) == IFDRational(num, denom) + + def test_nonetype(): # Fails if the _delegate function doesn't return a valid function diff --git a/Tests/test_util.py b/Tests/test_util.py index 3d88b9472..b5bfca012 100644 --- a/Tests/test_util.py +++ b/Tests/test_util.py @@ -1,4 +1,5 @@ import pytest + from PIL import _util @@ -13,7 +14,6 @@ def test_is_path(): assert it_is -@pytest.mark.skipif(not _util.py36, reason="os.path support for Paths added in 3.6") def test_path_obj_is_path(): # Arrange from pathlib import Path diff --git a/docs/about.rst b/docs/about.rst index ce6537e14..283135aca 100644 --- a/docs/about.rst +++ b/docs/about.rst @@ -36,10 +36,4 @@ What about PIL? Prior to Pillow 2.0.0, very few image code changes were made. Pillow 2.0.0 added Python 3 support and includes many bug fixes from many contributors. -As more time passes since the last PIL release (1.1.7 in 2009), the likelihood of a new PIL release decreases. However, we've yet to hear an official "PIL is dead" announcement. So if you still want to support PIL, please `report issues here first`_, then `open corresponding Pillow tickets here`_. - -.. _report issues here first: https://bitbucket.org/effbot/pil-2009-raclette/issues - -.. _open corresponding Pillow tickets here: https://github.com/python-pillow/Pillow/issues - -Please provide a link to the first ticket so we can track the issue(s) upstream. +As more time passes since the last PIL release (1.1.7 in 2009), the likelihood of a new PIL release decreases. However, we've yet to hear an official "PIL is dead" announcement. diff --git a/docs/conf.py b/docs/conf.py index 2d80e3170..a022e61cb 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -16,9 +16,10 @@ # documentation root, use os.path.abspath to make it absolute, like shown here. # sys.path.insert(0, os.path.abspath('.')) -import PIL import sphinx_rtd_theme +import PIL + # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. @@ -76,7 +77,7 @@ language = None # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = ["_build"] +exclude_patterns = ["_build", "releasenotes/template.rst"] # The reST default role (used for this markup: `text`) to use for all # documents. @@ -114,13 +115,7 @@ nitpicky = True # generating warnings in “nitpicky mode”. Note that type should include the domain name # if present. Example entries would be ('py:func', 'int') or # ('envvar', 'LD_LIBRARY_PATH'). -nitpick_ignore = [ - ("py:attr", "PIL.Image.Image.tag"), - ("py:attr", "PIL.Image.Image.tag_v2"), - ("py:attr", "PIL.Image.Image.tile"), - ("py:data", "PIL.Image.MAX_IMAGE_PIXELS"), - ("py:attr", "PIL.TiffImagePlugin.ImageFileDirectory_v2.tagtype"), -] +# nitpick_ignore = [] # -- Options for HTML output ---------------------------------------------- diff --git a/docs/deprecations.rst b/docs/deprecations.rst index b5ba00e40..e3ad2a9e3 100644 --- a/docs/deprecations.rst +++ b/docs/deprecations.rst @@ -18,16 +18,16 @@ Image.show command parameter .. deprecated:: 7.2.0 The ``command`` parameter was deprecated and will be removed in a future release. -Use a subclass of ``ImageShow.Viewer`` instead. +Use a subclass of :py:class:`.ImageShow.Viewer` instead. Image._showxv ~~~~~~~~~~~~~ .. deprecated:: 7.2.0 -``Image._showxv`` has been deprecated. Use :py:meth:`~PIL.Image.Image.show` -instead. If custom behaviour is required, use :py:meth:`~PIL.ImageShow.register` to add -a custom :py:class:`~PIL.ImageShow.Viewer` class. +``Image._showxv`` has been deprecated. Use :py:meth:`.Image.Image.show` +instead. If custom behaviour is required, use :py:func:`.ImageShow.register` to add +a custom :py:class:`.ImageShow.Viewer` class. ImageFile.raise_ioerror ~~~~~~~~~~~~~~~~~~~~~~~ @@ -55,26 +55,54 @@ Removed features Deprecated features are only removed in major releases after an appropriate period of deprecation has passed. +im.offset +~~~~~~~~~ + +.. deprecated:: 1.1.2 +.. versionremoved:: 8.0.0 + +``im.offset()`` has been removed, call :py:func:`.ImageChops.offset()` instead. + +It was documented as deprecated in PIL 1.1.2, +raised a ``DeprecationWarning`` since 1.1.5, +an ``Exception`` since Pillow 3.0.0 +and ``NotImplementedError`` since 3.3.0. + +Image.fromstring, im.fromstring and im.tostring +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. deprecated:: 2.0.0 +.. versionremoved:: 8.0.0 + +* ``Image.fromstring()`` has been removed, call :py:func:`.Image.frombytes()` instead. +* ``im.fromstring()`` has been removed, call :py:meth:`~PIL.Image.Image.frombytes()` instead. +* ``im.tostring()`` has been removed, call :py:meth:`~PIL.Image.Image.tobytes()` instead. + +They issued a ``DeprecationWarning`` since 2.0.0, +an ``Exception`` since 3.0.0 +and ``NotImplementedError`` since 3.3.0. + ImageCms.CmsProfile attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. deprecated:: 3.2.0 .. versionremoved:: 8.0.0 -Some attributes in ``ImageCms.CmsProfile`` have been removed. From 6.0.0, they issued a -``DeprecationWarning``: +Some attributes in :py:class:`PIL.ImageCms.CmsProfile` have been removed. From 6.0.0, +they issued a ``DeprecationWarning``: + +======================== =================================================== -======================== =============================== Removed Use instead -======================== =============================== -``color_space`` Padded ``xcolor_space`` -``pcs`` Padded ``connection_space`` -``product_copyright`` Unicode ``copyright`` -``product_desc`` Unicode ``profile_description`` -``product_description`` Unicode ``profile_description`` -``product_manufacturer`` Unicode ``manufacturer`` -``product_model`` Unicode ``model`` -======================== =============================== +======================== =================================================== +``color_space`` Padded :py:attr:`~.CmsProfile.xcolor_space` +``pcs`` Padded :py:attr:`~.CmsProfile.connection_space` +``product_copyright`` Unicode :py:attr:`~.CmsProfile.copyright` +``product_desc`` Unicode :py:attr:`~.CmsProfile.profile_description` +``product_description`` Unicode :py:attr:`~.CmsProfile.profile_description` +``product_manufacturer`` Unicode :py:attr:`~.CmsProfile.manufacturer` +``product_model`` Unicode :py:attr:`~.CmsProfile.model` +======================== =================================================== Python 2.7 ~~~~~~~~~~ diff --git a/docs/example/DdsImagePlugin.py b/docs/example/DdsImagePlugin.py index 1e36f093a..78aa3ce72 100644 --- a/docs/example/DdsImagePlugin.py +++ b/docs/example/DdsImagePlugin.py @@ -212,10 +212,10 @@ class DdsImageFile(ImageFile.ImageFile): def _open(self): magic, header_size = struct.unpack("= (3, 9): warnings.warn( - "Pillow {} does not support Python {}.{} and does not provide prebuilt " - "Windows binaries. We do not recommend building from source on Windows.".format( - PILLOW_VERSION, sys.version_info.major, sys.version_info.minor - ), + f"Pillow {PILLOW_VERSION} does not support Python " + f"{sys.version_info.major}.{sys.version_info.minor} and does not provide " + "prebuilt Windows binaries. We do not recommend building from source on " + "Windows.", RuntimeWarning, ) @@ -131,7 +130,7 @@ class RequiredDependencyException(Exception): pass -PLATFORM_MINGW = "mingw" in ccompiler.get_default_compiler() +PLATFORM_MINGW = os.name == "nt" and "GCC" in sys.version PLATFORM_PYPY = hasattr(sys, "pypy_version_info") if sys.platform == "win32" and PLATFORM_MINGW: @@ -175,7 +174,7 @@ def _find_library_dirs_ldconfig(): # Assuming GLIBC's ldconfig (with option -p) # Alpine Linux uses musl that can't print cache args = ["/sbin/ldconfig", "-p"] - expr = r".*\(%s.*\) => (.*)" % abi_type + expr = fr".*\({abi_type}.*\) => (.*)" env = dict(os.environ) env["LC_ALL"] = "C" env["LANG"] = "C" @@ -244,11 +243,6 @@ def _cmd_exists(cmd): ) -def _read(file): - with open(file, "rb") as fp: - return fp.read() - - def _pkg_config(name): try: command = os.environ.get("PKG_CONFIG", "pkg-config") @@ -308,8 +302,8 @@ class pil_build_ext(build_ext): user_options = ( build_ext.user_options - + [("disable-%s" % x, None, "Disable support for %s" % x) for x in feature] - + [("enable-%s" % x, None, "Enable support for %s" % x) for x in feature] + + [(f"disable-{x}", None, f"Disable support for {x}") for x in feature] + + [(f"enable-{x}", None, f"Enable support for {x}") for x in feature] + [ ("disable-platform-guessing", None, "Disable platform guessing on Linux"), ("debug", None, "Debug logging"), @@ -322,8 +316,8 @@ class pil_build_ext(build_ext): self.add_imaging_libs = "" build_ext.initialize_options(self) for x in self.feature: - setattr(self, "disable_%s" % x, None) - setattr(self, "enable_%s" % x, None) + setattr(self, f"disable_{x}", None) + setattr(self, f"enable_{x}", None) def finalize_options(self): build_ext.finalize_options(self) @@ -340,18 +334,34 @@ class pil_build_ext(build_ext): except TypeError: self.parallel = None for x in self.feature: - if getattr(self, "disable_%s" % x): + if getattr(self, f"disable_{x}"): setattr(self.feature, x, False) self.feature.required.discard(x) _dbg("Disabling %s", x) - if getattr(self, "enable_%s" % x): + if getattr(self, f"enable_{x}"): raise ValueError( - "Conflicting options: --enable-{} and --disable-{}".format(x, x) + f"Conflicting options: --enable-{x} and --disable-{x}" ) - if getattr(self, "enable_%s" % x): + if getattr(self, f"enable_{x}"): _dbg("Requiring %s", x) self.feature.required.add(x) + def _update_extension(self, name, libraries, define_macros=None, include_dirs=None): + for extension in self.extensions: + if extension.name == name: + extension.libraries += libraries + if define_macros is not None: + extension.define_macros += define_macros + if include_dirs is not None: + extension.include_dirs += include_dirs + break + + def _remove_extension(self, name): + for extension in self.extensions: + if extension.name == name: + self.extensions.remove(extension) + break + def build_extensions(self): library_dirs = [] @@ -383,12 +393,12 @@ class pil_build_ext(build_ext): if root is None and pkg_config: if isinstance(lib_name, tuple): for lib_name2 in lib_name: - _dbg("Looking for `%s` using pkg-config." % lib_name2) + _dbg(f"Looking for `{lib_name2}` using pkg-config.") root = pkg_config(lib_name2) if root: break else: - _dbg("Looking for `%s` using pkg-config." % lib_name) + _dbg(f"Looking for `{lib_name}` using pkg-config.") root = pkg_config(lib_name) if isinstance(root, tuple): @@ -418,10 +428,8 @@ class pil_build_ext(build_ext): for d in os.environ[k].split(os.path.pathsep): _add_directory(library_dirs, d) - prefix = sysconfig.get_config_var("prefix") - if prefix: - _add_directory(library_dirs, os.path.join(prefix, "lib")) - _add_directory(include_dirs, os.path.join(prefix, "include")) + _add_directory(library_dirs, os.path.join(sys.prefix, "lib")) + _add_directory(include_dirs, os.path.join(sys.prefix, "include")) # # add platform directories @@ -466,6 +474,9 @@ class pil_build_ext(build_ext): # add Homebrew's include and lib directories _add_directory(library_dirs, os.path.join(prefix, "lib")) _add_directory(include_dirs, os.path.join(prefix, "include")) + _add_directory( + include_dirs, os.path.join(prefix, "opt", "zlib", "include") + ) ft_prefix = os.path.join(prefix, "opt", "freetype") if ft_prefix and os.path.isdir(ft_prefix): @@ -697,12 +708,6 @@ class pil_build_ext(build_ext): # # core library - files = ["src/_imaging.c"] - for src_file in _IMAGING: - files.append("src/" + src_file + ".c") - for src_file in _LIB_IMAGING: - files.append(os.path.join("src/libImaging", src_file + ".c")) - libs = self.add_imaging_libs.split() defs = [] if feature.jpeg: @@ -735,11 +740,11 @@ class pil_build_ext(build_ext): and sys.version_info < (3, 9) and not (PLATFORM_PYPY or PLATFORM_MINGW) ): - defs.append(("PILLOW_VERSION", '"\\"%s\\""' % PILLOW_VERSION)) + defs.append(("PILLOW_VERSION", f'"\\"{PILLOW_VERSION}\\""')) else: - defs.append(("PILLOW_VERSION", '"%s"' % PILLOW_VERSION)) + defs.append(("PILLOW_VERSION", f'"{PILLOW_VERSION}"')) - exts = [(Extension("PIL._imaging", files, libraries=libs, define_macros=defs))] + self._update_extension("PIL._imaging", libs, defs) # # additional libraries @@ -747,26 +752,17 @@ class pil_build_ext(build_ext): if feature.freetype: libs = ["freetype"] defs = [] - exts.append( - Extension( - "PIL._imagingft", - ["src/_imagingft.c"], - libraries=libs, - define_macros=defs, - ) - ) + self._update_extension("PIL._imagingft", libs, defs) + else: + self._remove_extension("PIL._imagingft") if feature.lcms: extra = [] if sys.platform == "win32": extra.extend(["user32", "gdi32"]) - exts.append( - Extension( - "PIL._imagingcms", - ["src/_imagingcms.c"], - libraries=[feature.lcms] + extra, - ) - ) + self._update_extension("PIL._imagingcms", [feature.lcms] + extra) + else: + self._remove_extension("PIL._imagingcms") if feature.webp: libs = [feature.webp] @@ -777,26 +773,12 @@ class pil_build_ext(build_ext): libs.append(feature.webpmux) libs.append(feature.webpmux.replace("pmux", "pdemux")) - exts.append( - Extension( - "PIL._webp", ["src/_webp.c"], libraries=libs, define_macros=defs - ) - ) + self._update_extension("PIL._webp", libs, defs) + else: + self._remove_extension("PIL._webp") tk_libs = ["psapi"] if sys.platform == "win32" else [] - exts.append( - Extension( - "PIL._imagingtk", - ["src/_imagingtk.c", "src/Tk/tkImaging.c"], - include_dirs=["src/Tk"], - libraries=tk_libs, - ) - ) - - exts.append(Extension("PIL._imagingmath", ["src/_imagingmath.c"])) - exts.append(Extension("PIL._imagingmorph", ["src/_imagingmorph.c"])) - - self.extensions[:] = exts + self._update_extension("PIL._imagingtk", tk_libs, include_dirs=["src/Tk"]) build_ext.build_extensions(self) @@ -810,11 +792,11 @@ class pil_build_ext(build_ext): print("-" * 68) print("PIL SETUP SUMMARY") print("-" * 68) - print("version Pillow %s" % PILLOW_VERSION) + print(f"version Pillow {PILLOW_VERSION}") v = sys.version.split("[") - print("platform {} {}".format(sys.platform, v[0].strip())) + print(f"platform {sys.platform} {v[0].strip()}") for v in v[1:]: - print(" [%s" % v.strip()) + print(f" [{v.strip()}") print("-" * 68) options = [ @@ -835,10 +817,10 @@ class pil_build_ext(build_ext): if option[0]: version = "" if len(option) >= 3 and option[2]: - version = " (%s)" % option[2] - print("--- {} support available{}".format(option[1], version)) + version = f" ({option[2]})" + print(f"--- {option[1]} support available{version}") else: - print("*** %s support not available" % option[1]) + print(f"*** {option[1]} support not available") all = 0 print("-" * 68) @@ -860,12 +842,31 @@ def debug_build(): return hasattr(sys, "gettotalrefcount") +files = ["src/_imaging.c"] +for src_file in _IMAGING: + files.append("src/" + src_file + ".c") +for src_file in _LIB_IMAGING: + files.append(os.path.join("src/libImaging", src_file + ".c")) +ext_modules = [ + Extension("PIL._imaging", files), + Extension("PIL._imagingft", ["src/_imagingft.c"]), + Extension("PIL._imagingcms", ["src/_imagingcms.c"]), + Extension("PIL._webp", ["src/_webp.c"]), + Extension("PIL._imagingtk", ["src/_imagingtk.c", "src/Tk/tkImaging.c"]), + Extension("PIL._imagingmath", ["src/_imagingmath.c"]), + Extension("PIL._imagingmorph", ["src/_imagingmorph.c"]), +] + +with open("README.md") as f: + long_description = f.read() + try: setup( name=NAME, version=PILLOW_VERSION, description="Python Imaging Library (Fork)", - long_description=_read("README.rst").decode("utf-8"), + long_description=long_description, + long_description_content_type="text/markdown", license="HPND", author="Alex Clark (PIL Fork Author)", author_email="aclark@python-pillow.org", @@ -894,7 +895,7 @@ try: ], python_requires=">=3.6", cmdclass={"build_ext": pil_build_ext}, - ext_modules=[Extension("PIL._imaging", ["_imaging.c"])], + ext_modules=ext_modules, include_package_data=True, packages=["PIL"], package_dir={"": "src"}, @@ -902,28 +903,23 @@ try: zip_safe=not (debug_build() or PLATFORM_MINGW), ) except RequiredDependencyException as err: - msg = """ + msg = f""" -The headers or library files could not be found for %s, +The headers or library files could not be found for {str(err)}, a required dependency when compiling Pillow from source. Please see the install instructions at: https://pillow.readthedocs.io/en/latest/installation.html -""" % ( - str(err) - ) +""" sys.stderr.write(msg) raise RequiredDependencyException(msg) except DependencyException as err: - msg = """ + msg = f""" -The headers or library files could not be found for %s, -which was requested by the option flag --enable-%s +The headers or library files could not be found for {str(err)}, +which was requested by the option flag --enable-{str(err)} -""" % ( - str(err), - str(err), - ) +""" sys.stderr.write(msg) raise DependencyException(msg) diff --git a/src/PIL/BlpImagePlugin.py b/src/PIL/BlpImagePlugin.py index cb8a08e20..d5d7c0e05 100644 --- a/src/PIL/BlpImagePlugin.py +++ b/src/PIL/BlpImagePlugin.py @@ -250,7 +250,7 @@ class BlpImageFile(ImageFile.ImageFile): decoder = "BLP2" self.mode = "RGBA" if self._blp_alpha_depth else "RGB" else: - raise BLPFormatError("Bad BLP magic %r" % (self.magic)) + raise BLPFormatError(f"Bad BLP magic {repr(self.magic)}") self.tile = [(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))] @@ -336,11 +336,11 @@ class BLP1Decoder(_BLPBaseDecoder): self.set_as_raw(bytes(data)) else: raise BLPFormatError( - "Unsupported BLP encoding %r" % (self._blp_encoding) + f"Unsupported BLP encoding {repr(self._blp_encoding)}" ) else: raise BLPFormatError( - "Unsupported BLP compression %r" % (self._blp_encoding) + f"Unsupported BLP compression {repr(self._blp_encoding)}" ) def _decode_jpeg_stream(self): @@ -400,13 +400,15 @@ class BLP2Decoder(_BLPBaseDecoder): data += d else: raise BLPFormatError( - "Unsupported alpha encoding %r" % (self._blp_alpha_encoding) + f"Unsupported alpha encoding {repr(self._blp_alpha_encoding)}" ) else: - raise BLPFormatError("Unknown BLP encoding %r" % (self._blp_encoding)) + raise BLPFormatError(f"Unknown BLP encoding {repr(self._blp_encoding)}") else: - raise BLPFormatError("Unknown BLP compression %r" % (self._blp_compression)) + raise BLPFormatError( + f"Unknown BLP compression {repr(self._blp_compression)}" + ) self.set_as_raw(bytes(data)) diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index 1d348b5a3..711e030e1 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -25,7 +25,12 @@ from . import Image, ImageFile, ImagePalette -from ._binary import i8, i16le as i16, i32le as i32, o8, o16le as o16, o32le as o32 +from ._binary import i8 +from ._binary import i16le as i16 +from ._binary import i32le as i32 +from ._binary import o8 +from ._binary import o16le as o16 +from ._binary import o32le as o32 # # -------------------------------------------------------------------- @@ -144,7 +149,7 @@ class BmpImageFile(ImageFile.ImageFile): file_info["a_mask"], ) else: - raise OSError("Unsupported BMP header type (%d)" % file_info["header_size"]) + raise OSError(f"Unsupported BMP header type ({file_info['header_size']})") # ------------------ Special case : header is reported 40, which # ---------------------- is shorter than real size for bpp >= 16 @@ -164,7 +169,7 @@ class BmpImageFile(ImageFile.ImageFile): # ---------------------- Check bit depth for unusual unsupported values self.mode, raw_mode = BIT2MODE.get(file_info["bits"], (None, None)) if self.mode is None: - raise OSError("Unsupported BMP pixel depth (%d)" % file_info["bits"]) + raise OSError(f"Unsupported BMP pixel depth ({file_info['bits']})") # ---------------- Process BMP with Bitfields compression (not palette) if file_info["compression"] == self.BITFIELDS: @@ -209,14 +214,14 @@ class BmpImageFile(ImageFile.ImageFile): if file_info["bits"] == 32 and header == 22: # 32-bit .cur offset raw_mode, self.mode = "BGRA", "RGBA" else: - raise OSError("Unsupported BMP compression (%d)" % file_info["compression"]) + raise OSError(f"Unsupported BMP compression ({file_info['compression']})") # --------------- Once the header is processed, process the palette/LUT if self.mode == "P": # Paletted for 1, 4 and 8 bit images # ---------------------------------------------------- 1-bit images if not (0 < file_info["colors"] <= 65536): - raise OSError("Unsupported BMP Palette size (%d)" % file_info["colors"]) + raise OSError(f"Unsupported BMP Palette size ({file_info['colors']})") else: padding = file_info["palette_padding"] palette = read(padding * file_info["colors"]) @@ -305,7 +310,7 @@ def _save(im, fp, filename, bitmap_header=True): try: rawmode, bits, colors = SAVE[im.mode] except KeyError as e: - raise OSError("cannot write mode %s as BMP" % im.mode) from e + raise OSError(f"cannot write mode {im.mode} as BMP") from e info = im.encoderinfo diff --git a/src/PIL/CurImagePlugin.py b/src/PIL/CurImagePlugin.py index 3a1b6d2e5..35123f789 100644 --- a/src/PIL/CurImagePlugin.py +++ b/src/PIL/CurImagePlugin.py @@ -16,7 +16,9 @@ # See the README file for information on usage and redistribution. # from . import BmpImagePlugin, Image -from ._binary import i8, i16le as i16, i32le as i32 +from ._binary import i8 +from ._binary import i16le as i16 +from ._binary import i32le as i32 # # -------------------------------------------------------------------- diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index 9ba6e0ff8..3837192ab 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -106,10 +106,10 @@ class DdsImageFile(ImageFile.ImageFile): def _open(self): magic, header_size = struct.unpack("= (3, 7): if name == "PILLOW_VERSION": _raise_version_warning() return __version__ - raise AttributeError("module '{}' has no attribute '{}'".format(__name__, name)) + raise AttributeError(f"module '{__name__}' has no attribute '{name}'") else: @@ -81,7 +81,7 @@ class DecompressionBombError(Exception): pass -# Limit to around a quarter gigabyte for a 24 bit (3 bpp) image +# Limit to around a quarter gigabyte for a 24-bit (3 bpp) image MAX_IMAGE_PIXELS = int(1024 * 1024 * 1024 // 4 // 3) @@ -96,8 +96,8 @@ try: if __version__ != getattr(core, "PILLOW_VERSION", None): raise ImportError( "The _imaging extension was built for another version of Pillow or PIL:\n" - "Core version: %s\n" - "Pillow version: %s" % (getattr(core, "PILLOW_VERSION", None), __version__) + f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n" + f"Pillow version: {__version__}" ) except ImportError as v: @@ -403,7 +403,7 @@ def init(): for plugin in _plugins: try: logger.debug("Importing %s", plugin) - __import__("PIL.%s" % plugin, globals(), locals(), []) + __import__(f"PIL.{plugin}", globals(), locals(), []) except ImportError as e: logger.debug("Image: failed to import %s: %s", plugin, e) @@ -435,7 +435,7 @@ def _getdecoder(mode, decoder_name, args, extra=()): # get decoder decoder = getattr(core, decoder_name + "_decoder") except AttributeError as e: - raise OSError("decoder %s not available" % decoder_name) from e + raise OSError(f"decoder {decoder_name} not available") from e return decoder(mode, *args + extra) @@ -458,7 +458,7 @@ def _getencoder(mode, encoder_name, args, extra=()): # get encoder encoder = getattr(core, encoder_name + "_encoder") except AttributeError as e: - raise OSError("encoder %s not available" % encoder_name) from e + raise OSError(f"encoder {encoder_name} not available") from e return encoder(mode, *args + extra) @@ -594,7 +594,8 @@ class Image: try: if hasattr(self, "_close__fp"): self._close__fp() - self.fp.close() + if self.fp: + self.fp.close() self.fp = None except Exception as msg: logger.debug("Error closing: %s", msg) @@ -664,7 +665,7 @@ class Image: ) def _repr_png_(self): - """ iPython display hook support + """iPython display hook support :returns: png version of the image as bytes """ @@ -742,15 +743,10 @@ class Image: if s: break if s < 0: - raise RuntimeError("encoder error %d in tobytes" % s) + raise RuntimeError(f"encoder error {s} in tobytes") return b"".join(data) - def tostring(self, *args, **kw): - raise NotImplementedError( - "tostring() has been removed. Please call tobytes() instead." - ) - def tobitmap(self, name="image"): """ Returns the image converted to an X11 bitmap. @@ -768,9 +764,9 @@ class Image: data = self.tobytes("xbm") return b"".join( [ - ("#define %s_width %d\n" % (name, self.size[0])).encode("ascii"), - ("#define %s_height %d\n" % (name, self.size[1])).encode("ascii"), - ("static char %s_bits[] = {\n" % name).encode("ascii"), + f"#define {name}_width {self.size[0]}\n".encode("ascii"), + f"#define {name}_height {self.size[1]}\n".encode("ascii"), + f"static char {name}_bits[] = {{\n".encode("ascii"), data, b"};", ] @@ -802,11 +798,6 @@ class Image: if s[1] != 0: raise ValueError("cannot decode image data") - def fromstring(self, *args, **kw): - raise NotImplementedError( - "fromstring() has been removed. Please call frombytes() instead." - ) - def load(self): """ Allocates storage for the image and loads the pixel data. In @@ -865,7 +856,7 @@ class Image: and the palette can be represented without a palette. The current version supports all possible conversions between - "L", "RGB" and "CMYK." The **matrix** argument only supports "L" + "L", "RGB" and "CMYK." The ``matrix`` argument only supports "L" and "RGB". When translating a color image to greyscale (mode "L"), @@ -880,9 +871,9 @@ class Image: all other values to 0 (black). To use other thresholds, use the :py:meth:`~PIL.Image.Image.point` method. - When converting from "RGBA" to "P" without a **matrix** argument, + When converting from "RGBA" to "P" without a ``matrix`` argument, this passes the operation to :py:meth:`~PIL.Image.Image.quantize`, - and **dither** and **palette** are ignored. + and ``dither`` and ``palette`` are ignored. :param mode: The requested mode. See: :ref:`concept-modes`. :param matrix: An optional conversion matrix. If given, this @@ -890,7 +881,7 @@ class Image: :param dither: Dithering method, used when converting from mode "RGB" to "P" or from "RGB" or "L" to "1". Available methods are :data:`NONE` or :data:`FLOYDSTEINBERG` (default). - Note that this is not used when **matrix** is supplied. + Note that this is not used when ``matrix`` is supplied. :param palette: Palette to use when converting from mode "RGB" to "P". Available palettes are :data:`WEB` or :data:`ADAPTIVE`. :param colors: Number of colors to use for the :data:`ADAPTIVE` palette. @@ -1190,7 +1181,7 @@ class Image: available filters, see the :py:mod:`~PIL.ImageFilter` module. :param filter: Filter kernel. - :returns: An :py:class:`~PIL.Image.Image` object. """ + :returns: An :py:class:`~PIL.Image.Image` object.""" from . import ImageFilter @@ -1215,7 +1206,7 @@ class Image: def getbands(self): """ Returns a tuple containing the name of each band in this image. - For example, **getbands** on an RGB image returns ("R", "G", "B"). + For example, ``getbands`` on an RGB image returns ("R", "G", "B"). :returns: A tuple containing band names. :rtype: tuple @@ -1269,7 +1260,7 @@ class Image: Note that the sequence object returned by this method is an internal PIL data type, which only supports certain sequence operations. To convert it to an ordinary sequence (e.g. for - printing), use **list(im.getdata())**. + printing), use ``list(im.getdata())``. :param band: What band to return. The default is to return all bands. To return a single band, pass in the index @@ -1434,11 +1425,6 @@ class Image: return self.im.entropy(extrema) return self.im.entropy() - def offset(self, xoffset, yoffset=None): - raise NotImplementedError( - "offset() has been removed. Please call ImageChops.offset() instead." - ) - def paste(self, im, box=None, mask=None): """ Pastes another image into this image. The box argument is either @@ -1520,7 +1506,7 @@ class Image: self.im.paste(im, box) def alpha_composite(self, im, dest=(0, 0), source=(0, 0)): - """ 'In-place' analog of Image.alpha_composite. Composites an image + """'In-place' analog of Image.alpha_composite. Composites an image onto this image. :param im: image to composite over this one @@ -1755,7 +1741,7 @@ class Image: Rewrites the image to reorder the palette. :param dest_map: A list of indexes into the original palette. - e.g. [1,0] would swap a two item palette, and list(range(256)) + e.g. ``[1,0]`` would swap a two item palette, and ``list(range(256))`` is the identity transform. :param source_palette: Bytes or None. :returns: An :py:class:`~PIL.Image.Image` object. @@ -1876,7 +1862,7 @@ class Image: """ if resample not in (NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX, HAMMING): - message = "Unknown resampling filter ({}).".format(resample) + message = f"Unknown resampling filter ({resample})." filters = [ "{} ({})".format(filter[1], filter[0]) @@ -1937,16 +1923,16 @@ class Image: def reduce(self, factor, box=None): """ - Returns a copy of the image reduced by `factor` times. - If the size of the image is not dividable by the `factor`, + Returns a copy of the image reduced ``factor`` times. + If the size of the image is not dividable by ``factor``, the resulting size will be rounded up. :param factor: A greater than 0 integer or tuple of two integers for width and height separately. :param box: An optional 4-tuple of ints providing the source image region to be reduced. - The values must be within (0, 0, width, height) rectangle. - If omitted or None, the entire source is used. + The values must be within ``(0, 0, width, height)`` rectangle. + If omitted or ``None``, the entire source is used. """ if not isinstance(factor, (list, tuple)): factor = (factor, factor) @@ -2144,7 +2130,7 @@ class Image: try: format = EXTENSION[ext] except KeyError as e: - raise ValueError("unknown file extension: {}".format(ext)) from e + raise ValueError(f"unknown file extension: {ext}") from e if format.upper() not in SAVE: init() @@ -2256,7 +2242,7 @@ class Image: try: channel = self.getbands().index(channel) except ValueError as e: - raise ValueError('The image has no channel "{}"'.format(channel)) from e + raise ValueError(f'The image has no channel "{channel}"') from e return self._new(self.im.getband(channel)) @@ -2369,7 +2355,7 @@ class Image: # Return result It may also be an object with a ``method.getdata`` method - that returns a tuple supplying new **method** and **data** values:: + that returns a tuple supplying new ``method`` and ``data`` values:: class Example: def getdata(self): @@ -2384,7 +2370,7 @@ class Image: interpolation in a 4x4 environment). If omitted, or if the image has mode "1" or "P", it is set to :py:data:`PIL.Image.NEAREST`. See: :ref:`concept-filters`. - :param fill: If **method** is an + :param fill: If ``method`` is an :py:class:`~PIL.Image.ImageTransformHandler` object, this is one of the arguments passed to it. Otherwise, it is unused. :param fillcolor: Optional fill color for the area outside the @@ -2477,9 +2463,9 @@ class Image: BOX: "Image.BOX", HAMMING: "Image.HAMMING", LANCZOS: "Image.LANCZOS/Image.ANTIALIAS", - }[resample] + " ({}) cannot be used.".format(resample) + }[resample] + f" ({resample}) cannot be used." else: - message = "Unknown resampling filter ({}).".format(resample) + message = f"Unknown resampling filter ({resample})." filters = [ "{} ({})".format(filter[1], filter[0]) @@ -2672,12 +2658,6 @@ def frombytes(mode, size, data, decoder_name="raw", *args): return im -def fromstring(*args, **kw): - raise NotImplementedError( - "fromstring() has been removed. Please call frombytes() instead." - ) - - def frombuffer(mode, size, data, decoder_name="raw", *args): """ Creates an image memory referencing pixel data in a byte buffer. @@ -2689,7 +2669,7 @@ def frombuffer(mode, size, data, decoder_name="raw", *args): Note that this function decodes pixel data only, not entire images. If you have an entire image file in a string, wrap it in a - **BytesIO** object, and use :py:func:`~PIL.Image.open` to load it. + :py:class:`~io.BytesIO` object, and use :py:func:`~PIL.Image.open` to load it. In the current version, the default parameters used for the "raw" decoder differs from that used for :py:func:`~PIL.Image.frombytes`. This is a @@ -2736,7 +2716,7 @@ def fromarray(obj, mode=None): Creates an image memory from an object exporting the array interface (using the buffer protocol). - If **obj** is not contiguous, then the tobytes method is called + If ``obj`` is not contiguous, then the ``tobytes`` method is called and :py:func:`~PIL.Image.frombuffer` is used. If you have an image in NumPy:: @@ -2779,7 +2759,7 @@ def fromarray(obj, mode=None): else: ndmax = 4 if ndim > ndmax: - raise ValueError("Too many dimensions: %d > %d." % (ndim, ndmax)) + raise ValueError(f"Too many dimensions: {ndim} > {ndmax}.") size = 1 if ndim == 1 else shape[1], shape[0] if strides is not None: @@ -2845,14 +2825,14 @@ def _decompression_bomb_check(size): if pixels > 2 * MAX_IMAGE_PIXELS: raise DecompressionBombError( - "Image size (%d pixels) exceeds limit of %d pixels, " - "could be decompression bomb DOS attack." % (pixels, 2 * MAX_IMAGE_PIXELS) + f"Image size ({pixels} pixels) exceeds limit of {2 * MAX_IMAGE_PIXELS} " + "pixels, could be decompression bomb DOS attack." ) if pixels > MAX_IMAGE_PIXELS: warnings.warn( - "Image size (%d pixels) exceeds limit of %d pixels, " - "could be decompression bomb DOS attack." % (pixels, MAX_IMAGE_PIXELS), + f"Image size ({pixels} pixels) exceeds limit of {MAX_IMAGE_PIXELS} pixels, " + "could be decompression bomb DOS attack.", DecompressionBombWarning, ) @@ -2869,7 +2849,7 @@ def open(fp, mode="r"): :param fp: A filename (string), pathlib.Path object or a file object. The file object must implement ``file.read``, - ``file.seek`, and ``file.tell`` methods, + ``file.seek``, and ``file.tell`` methods, and be opened in binary mode. :param mode: The mode. If given, this argument must be "r". :returns: An :py:class:`~PIL.Image.Image` object. @@ -2881,7 +2861,7 @@ def open(fp, mode="r"): """ if mode != "r": - raise ValueError("bad mode %r" % mode) + raise ValueError(f"bad mode {repr(mode)}") elif isinstance(fp, io.StringIO): raise ValueError( "StringIO cannot be used to open an image. " @@ -3262,13 +3242,13 @@ def _apply_env_variables(env=None): try: var = int(var) * units except ValueError: - warnings.warn("{} is not int".format(var_name)) + warnings.warn(f"{var_name} is not int") continue try: setter(var) except ValueError as e: - warnings.warn("{}: {}".format(var_name, e)) + warnings.warn(f"{var_name}: {e}") _apply_env_variables() @@ -3391,8 +3371,8 @@ class Exif(MutableMapping): if len(data) != size: warnings.warn( "Possibly corrupt EXIF MakerNote data. " - "Expecting to read %d bytes but only got %d." - " Skipping tag %s" % (size, len(data), ifd_tag) + f"Expecting to read {size} bytes but only got " + f"{len(data)}. Skipping tag {ifd_tag}" ) continue diff --git a/src/PIL/ImageChops.py b/src/PIL/ImageChops.py index 9fccedcb2..61d3a295b 100644 --- a/src/PIL/ImageChops.py +++ b/src/PIL/ImageChops.py @@ -313,8 +313,8 @@ def composite(image1, image2, mask): def offset(image, xoffset, yoffset=None): """Returns a copy of the image where data has been offset by the given - distances. Data wraps around the edges. If **yoffset** is omitted, it - is assumed to be equal to **xoffset**. + distances. Data wraps around the edges. If ``yoffset`` is omitted, it + is assumed to be equal to ``xoffset``. :param xoffset: The horizontal distance. :param yoffset: The vertical distance. If omitted, both diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 1c4ce5a08..3856cb843 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -250,7 +250,9 @@ class ImageCmsTransform(Image.ImagePointHandler): def get_display_profile(handle=None): - """ (experimental) Fetches the profile for the current display device. + """ + (experimental) Fetches the profile for the current display device. + :returns: ``None`` if the profile is not known. """ @@ -275,8 +277,8 @@ def get_display_profile(handle=None): class PyCMSError(Exception): - """ (pyCMS) Exception class. - This is used for all errors in the pyCMS API. """ + """(pyCMS) Exception class. + This is used for all errors in the pyCMS API.""" pass @@ -624,7 +626,7 @@ def applyTransform(im, transform, inPlace=False): :param im: An :py:class:`~PIL.Image.Image` object, and im.mode must be the same as the ``inMode`` supported by the transform. :param transform: A valid CmsTransform class object - :param inPlace: Bool. If ``True``, ``im` is modified in place and ``None`` is + :param inPlace: Bool. If ``True``, ``im`` is modified in place and ``None`` is returned, if ``False``, a new :py:class:`~PIL.Image.Image` object with the transform applied is returned (and ``im`` is not changed). The default is ``False``. @@ -678,8 +680,7 @@ def createProfile(colorSpace, colorTemp=-1): if colorSpace not in ["LAB", "XYZ", "sRGB"]: raise PyCMSError( - "Color space not supported for on-the-fly profile creation (%s)" - % colorSpace + f"Color space not supported for on-the-fly profile creation ({colorSpace})" ) if colorSpace == "LAB": @@ -687,7 +688,7 @@ def createProfile(colorSpace, colorTemp=-1): colorTemp = float(colorTemp) except (TypeError, ValueError) as e: raise PyCMSError( - 'Color temperature must be numeric, "%s" not valid' % colorTemp + f'Color temperature must be numeric, "{colorTemp}" not valid' ) from e try: @@ -732,7 +733,7 @@ def getProfileName(profile): return (profile.profile.profile_description or "") + "\n" if not manufacturer or len(model) > 30: return model + "\n" - return "{} - {}\n".format(model, manufacturer) + return f"{model} - {manufacturer}\n" except (AttributeError, OSError, TypeError, ValueError) as v: raise PyCMSError(v) from v diff --git a/src/PIL/ImageColor.py b/src/PIL/ImageColor.py index 9cf7a9912..909117449 100644 --- a/src/PIL/ImageColor.py +++ b/src/PIL/ImageColor.py @@ -113,7 +113,7 @@ def getrgb(color): m = re.match(r"rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color) if m: return (int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))) - raise ValueError("unknown color specifier: %r" % color) + raise ValueError(f"unknown color specifier: {repr(color)}") def getcolor(color, mode): diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index cbecf652d..6775ba43e 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -35,7 +35,6 @@ import numbers from . import Image, ImageColor - """ A simple 2D drawing interface for PIL images.

@@ -277,7 +276,7 @@ class ImageDraw: stroke_width=0, stroke_fill=None, *args, - **kwargs + **kwargs, ): if self._multiline_check(text): return self.multiline_text( diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index fd2e1bbde..49633ef51 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -40,6 +40,7 @@ MAXBLOCK = 65536 SAFEBLOCK = 1024 * 1024 LOAD_TRUNCATED_IMAGES = False +"""Whether or not to load truncated image files. User code may change this.""" ERRORS = { -1: "image buffer overrun error", @@ -48,6 +49,7 @@ ERRORS = { -8: "bad configuration", -9: "out of memory error", } +"""Dict of known error codes returned from :meth:`.PyDecoder.decode`.""" # @@ -61,7 +63,7 @@ def raise_oserror(error): except AttributeError: message = ERRORS.get(error) if not message: - message = "decoder error %d" % error + message = f"decoder error {error}" raise OSError(message + " when reading image file") @@ -95,6 +97,8 @@ class ImageFile(Image.Image): self.custom_mimetype = None self.tile = None + """ A list of tile descriptors, or ``None`` """ + self.readonly = 1 # until we know better self.decoderconfig = () @@ -199,7 +203,7 @@ class ImageFile(Image.Image): # use mmap, if possible import mmap - with open(self.filename, "r") as fp: + with open(self.filename) as fp: self.map = mmap.mmap( fp.fileno(), 0, access=mmap.ACCESS_READ ) @@ -254,7 +258,7 @@ class ImageFile(Image.Image): else: raise OSError( "image file is truncated " - "(%d bytes not processed)" % len(b) + f"({len(b)} bytes not processed)" ) b = b + s @@ -330,7 +334,7 @@ class StubImageFile(ImageFile): def load(self): loader = self._load() if loader is None: - raise OSError("cannot find loader for this %s file" % self.format) + raise OSError(f"cannot find loader for this {self.format} file") image = loader.load(self) assert image is not None # become the other object (!) @@ -505,7 +509,7 @@ def _save(im, fp, tile, bufsize=0): try: fh = fp.fileno() fp.flush() - except (AttributeError, io.UnsupportedOperation) as e: + except (AttributeError, io.UnsupportedOperation) as exc: # compress to Python file-compatible object for e, b, o, a in tile: e = Image._getencoder(im.mode, e, a, im.encoderconfig) @@ -522,7 +526,7 @@ def _save(im, fp, tile, bufsize=0): if s: break if s < 0: - raise OSError("encoder error %d when writing image file" % s) from e + raise OSError(f"encoder error {s} when writing image file") from exc e.cleanup() else: # slight speedup: compress to real file object @@ -537,7 +541,7 @@ def _save(im, fp, tile, bufsize=0): else: s = e.encode_to_file(fh, bufsize) if s < 0: - raise OSError("encoder error %d when writing image file" % s) + raise OSError(f"encoder error {s} when writing image file") e.cleanup() if hasattr(fp, "flush"): fp.flush() @@ -581,7 +585,7 @@ class PyCodecState: class PyDecoder: """ Python implementation of a format decoder. Override this class and - add the decoding logic in the `decode` method. + add the decoding logic in the :meth:`decode` method. See :ref:`Writing Your Own File Decoder in Python` """ @@ -613,9 +617,9 @@ class PyDecoder: Override to perform the decoding process. :param buffer: A bytes object with the data to be decoded. - :returns: A tuple of (bytes consumed, errcode). + :returns: A tuple of ``(bytes consumed, errcode)``. If finished with decoding return <0 for the bytes consumed. - Err codes are from `ERRORS` + Err codes are from :data:`.ImageFile.ERRORS`. """ raise NotImplementedError() diff --git a/src/PIL/ImageFilter.py b/src/PIL/ImageFilter.py index 18c550c84..9ca17d9ad 100644 --- a/src/PIL/ImageFilter.py +++ b/src/PIL/ImageFilter.py @@ -69,7 +69,7 @@ class Kernel(BuiltinFilter): class RankFilter(Filter): """ Create a rank filter. The rank filter sorts all pixels in - a window of the given size, and returns the **rank**'th value. + a window of the given size, and returns the ``rank``'th value. :param size: The kernel size, in pixels. :param rank: What pixel value to pick. Use 0 for a min filter, @@ -401,9 +401,8 @@ class Color3DLUT(MultibandFilter): raise ValueError( "The table should have either channels * size**3 float items " "or size**3 items of channels-sized tuples with floats. " - "Table should be: {}x{}x{}x{}. Actual length: {}".format( - channels, size[0], size[1], size[2], len(table) - ) + f"Table should be: {channels}x{size[0]}x{size[1]}x{size[2]}. " + f"Actual length: {len(table)}" ) self.table = table @@ -513,12 +512,12 @@ class Color3DLUT(MultibandFilter): def __repr__(self): r = [ - "{} from {}".format(self.__class__.__name__, self.table.__class__.__name__), + f"{self.__class__.__name__} from {self.table.__class__.__name__}", "size={:d}x{:d}x{:d}".format(*self.size), - "channels={:d}".format(self.channels), + f"channels={self.channels:d}", ] if self.mode: - r.append("target_mode={}".format(self.mode)) + r.append(f"target_mode={self.mode}") return "<{}>".format(" ".join(r)) def filter(self, image): diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 8f792d55b..63f819151 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -415,7 +415,7 @@ class FreeTypeFont: language=None, stroke_width=0, *args, - **kwargs + **kwargs, ): """ Create a bitmap for the text. @@ -638,7 +638,7 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None): This specifies the character set to use. It does not alter the encoding of any text provided in subsequent operations. :param layout_engine: Which layout engine to use, if available: - `ImageFont.LAYOUT_BASIC` or `ImageFont.LAYOUT_RAQM`. + :data:`.ImageFont.LAYOUT_BASIC` or :data:`.ImageFont.LAYOUT_RAQM`. You can check support for Raqm layout using :py:func:`PIL.features.check_feature` with ``feature="raqm"``. diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 3fa338b0a..b93ec3f2a 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -21,8 +21,8 @@ from . import Image if sys.platform == "darwin": import os - import tempfile import subprocess + import tempfile def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None): diff --git a/src/PIL/ImageMath.py b/src/PIL/ImageMath.py index 9a2d0b78e..7f9c88e14 100644 --- a/src/PIL/ImageMath.py +++ b/src/PIL/ImageMath.py @@ -41,7 +41,7 @@ class _Operand: elif im1.im.mode in ("I", "F"): return im1.im else: - raise ValueError("unsupported mode: %s" % im1.im.mode) + raise ValueError(f"unsupported mode: {im1.im.mode}") else: # argument was a constant if _isconstant(im1) and self.im.mode in ("1", "L", "I"): @@ -58,7 +58,7 @@ class _Operand: try: op = getattr(_imagingmath, op + "_" + im1.mode) except AttributeError as e: - raise TypeError("bad operand type for '%s'" % op) from e + raise TypeError(f"bad operand type for '{op}'") from e _imagingmath.unop(op, out.im.id, im1.im.id) else: # binary operation @@ -86,7 +86,7 @@ class _Operand: try: op = getattr(_imagingmath, op + "_" + im1.mode) except AttributeError as e: - raise TypeError("bad operand type for '%s'" % op) from e + raise TypeError(f"bad operand type for '{op}'") from e _imagingmath.binop(op, out.im.id, im1.im.id, im2.im.id) return _Operand(out) diff --git a/src/PIL/ImageMorph.py b/src/PIL/ImageMorph.py index d1ec09eac..b76dfa01f 100644 --- a/src/PIL/ImageMorph.py +++ b/src/PIL/ImageMorph.py @@ -28,36 +28,36 @@ MIRROR_MATRIX = [ class LutBuilder: """A class for building a MorphLut from a descriptive language - The input patterns is a list of a strings sequences like these:: + The input patterns is a list of a strings sequences like these:: - 4:(... - .1. - 111)->1 + 4:(... + .1. + 111)->1 - (whitespaces including linebreaks are ignored). The option 4 - describes a series of symmetry operations (in this case a - 4-rotation), the pattern is described by: + (whitespaces including linebreaks are ignored). The option 4 + describes a series of symmetry operations (in this case a + 4-rotation), the pattern is described by: - - . or X - Ignore - - 1 - Pixel is on - - 0 - Pixel is off + - . or X - Ignore + - 1 - Pixel is on + - 0 - Pixel is off - The result of the operation is described after "->" string. + The result of the operation is described after "->" string. - The default is to return the current pixel value, which is - returned if no other match is found. + The default is to return the current pixel value, which is + returned if no other match is found. - Operations: + Operations: - - 4 - 4 way rotation - - N - Negate - - 1 - Dummy op for no other operation (an op must always be given) - - M - Mirroring + - 4 - 4 way rotation + - N - Negate + - 1 - Dummy op for no other operation (an op must always be given) + - M - Mirroring - Example:: + Example:: - lb = LutBuilder(patterns = ["4:(... .1. 111)->1"]) - lut = lb.build_lut() + lb = LutBuilder(patterns = ["4:(... .1. 111)->1"]) + lut = lb.build_lut() """ diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 157da0b52..14602a5c8 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -61,10 +61,10 @@ def _lut(image, lut): # actions -def autocontrast(image, cutoff=0, ignore=None): +def autocontrast(image, cutoff=0, ignore=None, mask=None): """ Maximize (normalize) image contrast. This function calculates a - histogram of the input image, removes **cutoff** percent of the + histogram of the input image (or mask region), removes ``cutoff`` percent of the lightest and darkest pixels from the histogram, and remaps the image so that the darkest pixel becomes black (0), and the lightest becomes white (255). @@ -74,9 +74,12 @@ def autocontrast(image, cutoff=0, ignore=None): high ends. Either a tuple of (low, high), or a single number for both. :param ignore: The background pixel value (use None for no background). + :param mask: Histogram used in contrast operation is computed using pixels + within the mask. If no mask is given the entire image is used + for histogram computation. :return: An image. """ - histogram = image.histogram() + histogram = image.histogram(mask) lut = [] for layer in range(0, len(histogram), 256): h = histogram[layer : layer + 256] @@ -146,14 +149,14 @@ def colorize(image, black, white, mid=None, blackpoint=0, whitepoint=255, midpoi Colorize grayscale image. This function calculates a color wedge which maps all black pixels in the source image to the first color and all white pixels to the - second color. If **mid** is specified, it uses three-color mapping. - The **black** and **white** arguments should be RGB tuples or color names; - optionally you can use three-color mapping by also specifying **mid**. + second color. If ``mid`` is specified, it uses three-color mapping. + The ``black`` and ``white`` arguments should be RGB tuples or color names; + optionally you can use three-color mapping by also specifying ``mid``. Mapping positions for any of the colors can be specified - (e.g. **blackpoint**), where these parameters are the integer + (e.g. ``blackpoint``), where these parameters are the integer value corresponding to where the corresponding color should be mapped. These parameters must have logical order, such that - **blackpoint** <= **midpoint** <= **whitepoint** (if **mid** is specified). + ``blackpoint <= midpoint <= whitepoint`` (if ``mid`` is specified). :param image: The image to colorize. :param black: The color to use for black input pixels. @@ -312,7 +315,7 @@ def deform(image, deformer, resample=Image.BILINEAR): :param image: The image to deform. :param deformer: A deformer object. Any object that implements a - **getmesh** method can be used. + ``getmesh`` method can be used. :param resample: An optional resampling filter. Same values possible as in the PIL.Image.transform function. :return: An image. diff --git a/src/PIL/ImagePalette.py b/src/PIL/ImagePalette.py index 5dba6176f..d0604112f 100644 --- a/src/PIL/ImagePalette.py +++ b/src/PIL/ImagePalette.py @@ -111,7 +111,7 @@ class ImagePalette: self.dirty = 1 return index else: - raise ValueError("unknown color specifier: %r" % color) + raise ValueError(f"unknown color specifier: {repr(color)}") def save(self, fp): """Save palette to text file. @@ -123,12 +123,12 @@ class ImagePalette: if isinstance(fp, str): fp = open(fp, "w") fp.write("# Palette\n") - fp.write("# Mode: %s\n" % self.mode) + fp.write(f"# Mode: {self.mode}\n") for i in range(256): - fp.write("%d" % i) + fp.write(f"{i}") for j in range(i * len(self.mode), (i + 1) * len(self.mode)): try: - fp.write(" %d" % self.palette[j]) + fp.write(f" {self.palette[j]}") except IndexError: fp.write(" 0") fp.write("\n") diff --git a/src/PIL/ImageQt.py b/src/PIL/ImageQt.py index a15f4ab5e..91be53488 100644 --- a/src/PIL/ImageQt.py +++ b/src/PIL/ImageQt.py @@ -29,11 +29,11 @@ qt_versions.sort(key=lambda qt_version: qt_version[1] in sys.modules, reverse=Tr for qt_version, qt_module in qt_versions: try: if qt_module == "PyQt5": - from PyQt5.QtGui import QImage, qRgba, QPixmap from PyQt5.QtCore import QBuffer, QIODevice + from PyQt5.QtGui import QImage, QPixmap, qRgba elif qt_module == "PySide2": - from PySide2.QtGui import QImage, qRgba, QPixmap from PySide2.QtCore import QBuffer, QIODevice + from PySide2.QtGui import QImage, QPixmap, qRgba except (ImportError, RuntimeError): continue qt_is_installed = True @@ -145,7 +145,7 @@ def _toqclass_helper(im): data = im.tobytes("raw", "BGRA") format = QImage.Format_ARGB32 else: - raise ValueError("unsupported image mode %r" % im.mode) + raise ValueError(f"unsupported image mode {repr(im.mode)}") __data = data or align8to32(im.tobytes(), im.size[0], im.mode) return {"data": __data, "im": im, "format": format, "colortable": colortable} diff --git a/src/PIL/ImageShow.py b/src/PIL/ImageShow.py index 3ffb4d632..1ada8252c 100644 --- a/src/PIL/ImageShow.py +++ b/src/PIL/ImageShow.py @@ -123,9 +123,9 @@ class WindowsViewer(Viewer): def get_command(self, file, **options): return ( - 'start "Pillow" /WAIT "%s" ' + f'start "Pillow" /WAIT "{file}" ' "&& ping -n 2 127.0.0.1 >NUL " - '&& del /f "%s"' % (file, file) + f'&& del /f "{file}"' ) @@ -143,9 +143,7 @@ class MacViewer(Viewer): # on darwin open returns immediately resulting in the temp # file removal while app is opening command = "open -a Preview.app" - command = "({} {}; sleep 20; rm -f {})&".format( - command, quote(file), quote(file) - ) + command = f"({command} {quote(file)}; sleep 20; rm -f {quote(file)})&" return command def show_file(self, file, **options): @@ -153,7 +151,7 @@ class MacViewer(Viewer): fd, path = tempfile.mkstemp() with os.fdopen(fd, "w") as f: f.write(file) - with open(path, "r") as f: + with open(path) as f: subprocess.Popen( ["im=$(cat); open -a Preview.app $im; sleep 20; rm -f $im"], shell=True, @@ -173,14 +171,14 @@ class UnixViewer(Viewer): def get_command(self, file, **options): command = self.get_command_ex(file, **options)[0] - return "({} {}; rm -f {})&".format(command, quote(file), quote(file)) + return f"({command} {quote(file)}; rm -f {quote(file)})&" def show_file(self, file, **options): """Display given file""" fd, path = tempfile.mkstemp() with os.fdopen(fd, "w") as f: f.write(file) - with open(path, "r") as f: + with open(path) as f: command = self.get_command_ex(file, **options)[0] subprocess.Popen( ["im=$(cat);" + command + " $im; rm -f $im"], shell=True, stdin=f @@ -216,7 +214,7 @@ class XVViewer(UnixViewer): # imagemagick's display command instead. command = executable = "xv" if title: - command += " -name %s" % quote(title) + command += f" -name {quote(title)}" return command, executable diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index ee707cffb..62db7a717 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -41,7 +41,7 @@ def _pilbitmap_check(): if _pilbitmap_ok is None: try: im = Image.new("1", (1, 1)) - tkinter.BitmapImage(data="PIL:%d" % im.im.id) + tkinter.BitmapImage(data=f"PIL:{im.im.id}") _pilbitmap_ok = 1 except tkinter.TclError: _pilbitmap_ok = 0 @@ -69,7 +69,7 @@ class PhotoImage: image, pixels having alpha 0 are treated as transparent. The constructor takes either a PIL image, or a mode and a size. - Alternatively, you can use the **file** or **data** options to initialize + Alternatively, you can use the ``file`` or ``data`` options to initialize the photo image object. :param image: Either a PIL image, or a mode string. If a mode string is @@ -210,7 +210,7 @@ class BitmapImage: The given image must have mode "1". Pixels having value 0 are treated as transparent. Options, if any, are passed on to Tkinter. The most commonly - used option is **foreground**, which is used to specify the color for the + used option is ``foreground``, which is used to specify the color for the non-transparent parts. See the Tkinter documentation for information on how to specify colours. @@ -229,7 +229,7 @@ class BitmapImage: if _pilbitmap_check(): # fast way (requires the pilbitmap booster patch) image.load() - kw["data"] = "PIL:%d" % image.im.id + kw["data"] = f"PIL:{image.im.id}" self.__im = image # must keep a reference else: # slow but safe way diff --git a/src/PIL/ImageWin.py b/src/PIL/ImageWin.py index 2ca4acdf8..ca9b14c8a 100644 --- a/src/PIL/ImageWin.py +++ b/src/PIL/ImageWin.py @@ -59,7 +59,7 @@ class Dib: with 20 greylevels. To make sure that palettes work properly under Windows, you must call the - **palette** method upon certain events from Windows. + ``palette`` method upon certain events from Windows. :param image: Either a PIL image, or a mode string. If a mode string is used, a size must also be given. The mode can be one of "1", diff --git a/src/PIL/IptcImagePlugin.py b/src/PIL/IptcImagePlugin.py index 75e7b5a2a..f407b7e5f 100644 --- a/src/PIL/IptcImagePlugin.py +++ b/src/PIL/IptcImagePlugin.py @@ -18,7 +18,10 @@ import os import tempfile from . import Image, ImageFile -from ._binary import i8, i16be as i16, i32be as i32, o8 +from ._binary import i8 +from ._binary import i16be as i16 +from ._binary import i32be as i32 +from ._binary import o8 COMPRESSION = {1: "raw", 5: "jpeg"} @@ -181,9 +184,10 @@ def getiptcinfo(im): :returns: A dictionary containing IPTC information, or None if no IPTC information block was found. """ - from . import TiffImagePlugin, JpegImagePlugin import io + from . import JpegImagePlugin, TiffImagePlugin + data = None if isinstance(im, IptcImageFile): diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index f18eedb20..6145e5da7 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -40,7 +40,10 @@ import tempfile import warnings from . import Image, ImageFile, TiffImagePlugin -from ._binary import i8, i16be as i16, i32be as i32, o8 +from ._binary import i8 +from ._binary import i16be as i16 +from ._binary import i32be as i32 +from ._binary import o8 from .JpegPresets import presets # @@ -195,7 +198,7 @@ def SOF(self, marker): self.bits = i8(s[0]) if self.bits != 8: - raise SyntaxError("cannot handle %d-bit layers" % self.bits) + raise SyntaxError(f"cannot handle {self.bits}-bit layers") self.layers = i8(s[5]) if self.layers == 1: @@ -205,7 +208,7 @@ def SOF(self, marker): elif self.layers == 4: self.mode = "CMYK" else: - raise SyntaxError("cannot handle %d-layer images" % self.layers) + raise SyntaxError(f"cannot handle {self.layers}-layer images") if marker in [0xFFC2, 0xFFC6, 0xFFCA, 0xFFCE]: self.info["progressive"] = self.info["progression"] = 1 @@ -518,7 +521,7 @@ def _getmp(self): rawmpentries = mp[0xB002] for entrynum in range(0, quant): unpackedentry = struct.unpack_from( - "{}LLLHH".format(endianness), rawmpentries, entrynum * 16 + f"{endianness}LLLHH", rawmpentries, entrynum * 16 ) labels = ("Attribute", "Size", "DataOffset", "EntryNo1", "EntryNo2") mpentry = dict(zip(labels, unpackedentry)) @@ -613,7 +616,7 @@ def _save(im, fp, filename): try: rawmode = RAWMODE[im.mode] except KeyError as e: - raise OSError("cannot write mode %s as JPEG" % im.mode) from e + raise OSError(f"cannot write mode {im.mode} as JPEG") from e info = im.encoderinfo diff --git a/src/PIL/JpegPresets.py b/src/PIL/JpegPresets.py index 09691d79d..79d10ebb2 100644 --- a/src/PIL/JpegPresets.py +++ b/src/PIL/JpegPresets.py @@ -34,7 +34,7 @@ Possible subsampling values are 0, 1 and 2 that correspond to 4:4:4, 4:2:2 and 4:2:0. You can get the subsampling of a JPEG with the -`JpegImagePlugin.get_sampling(im)` function. +:func:`.JpegImagePlugin.get_sampling` function. In JPEG compressed data a JPEG marker is used instead of an EXIF tag. (ref.: https://www.exiv2.org/tags.html) @@ -64,7 +64,7 @@ The tables format between im.quantization and quantization in presets differ in 3. The zigzag order is remove in the preset (needed by libjpeg >= 6a). You can convert the dict format to the preset format with the -`JpegImagePlugin.convert_dict_qtables(dict_qtables)` function. +:func:`.JpegImagePlugin.convert_dict_qtables()` function. Libjpeg ref.: https://web.archive.org/web/20120328125543/http://www.jpegcameras.com/libjpeg/libjpeg-3.html diff --git a/src/PIL/MspImagePlugin.py b/src/PIL/MspImagePlugin.py index ca9572187..9dd6e9f32 100644 --- a/src/PIL/MspImagePlugin.py +++ b/src/PIL/MspImagePlugin.py @@ -27,7 +27,9 @@ import io import struct from . import Image, ImageFile -from ._binary import i8, i16le as i16, o16le as o16 +from ._binary import i8 +from ._binary import i16le as i16 +from ._binary import o16le as o16 # # read MSP files @@ -114,7 +116,7 @@ class MspDecoder(ImageFile.PyDecoder): try: self.fd.seek(32) rowmap = struct.unpack_from( - "<%dH" % (self.state.ysize), self.fd.read(self.state.ysize * 2) + f"<{self.state.ysize}H", self.fd.read(self.state.ysize * 2) ) except struct.error as e: raise OSError("Truncated MSP file in row map") from e @@ -143,7 +145,7 @@ class MspDecoder(ImageFile.PyDecoder): idx += runcount except struct.error as e: - raise OSError("Corrupted MSP file in row %d" % x) from e + raise OSError(f"Corrupted MSP file in row {x}") from e self.set_as_raw(img.getvalue(), ("1", 0, 1)) @@ -160,7 +162,7 @@ Image.register_decoder("MSP", MspDecoder) def _save(im, fp, filename): if im.mode != "1": - raise OSError("cannot write mode %s as MSP" % im.mode) + raise OSError(f"cannot write mode {im.mode} as MSP") # create MSP header header = [0] * 16 diff --git a/src/PIL/PSDraw.py b/src/PIL/PSDraw.py index 3cfcbaf28..c1bd933d3 100644 --- a/src/PIL/PSDraw.py +++ b/src/PIL/PSDraw.py @@ -25,7 +25,7 @@ from . import EpsImagePlugin class PSDraw: """ - Sets up printing to the given file. If **fp** is omitted, + Sets up printing to the given file. If ``fp`` is omitted, :py:data:`sys.stdout` is assumed. """ @@ -71,10 +71,10 @@ class PSDraw: """ if font not in self.isofont: # reencode font - self._fp_write("/PSDraw-{} ISOLatin1Encoding /{} E\n".format(font, font)) + self._fp_write(f"/PSDraw-{font} ISOLatin1Encoding /{font} E\n") self.isofont[font] = 1 # rough - self._fp_write("/F0 %d /PSDraw-%s F\n" % (size, font)) + self._fp_write(f"/F0 {size} /PSDraw-{font} F\n") def line(self, xy0, xy1): """ @@ -82,8 +82,7 @@ class PSDraw: PostScript point coordinates (72 points per inch, (0, 0) is the lower left corner of the page). """ - xy = xy0 + xy1 - self._fp_write("%d %d %d %d Vl\n" % xy) + self._fp_write("%d %d %d %d Vl\n" % (*xy0, *xy1)) def rectangle(self, box): """ @@ -107,8 +106,7 @@ class PSDraw: """ text = "\\(".join(text.split("(")) text = "\\)".join(text.split(")")) - xy = xy + (text,) - self._fp_write("%d %d M (%s) S\n" % xy) + self._fp_write(f"{xy[0]} {xy[1]} M ({text}) S\n") def image(self, box, im, dpi=None): """Draw a PIL image, centered in the given box.""" @@ -132,12 +130,12 @@ class PSDraw: y = ymax dx = (xmax - x) / 2 + box[0] dy = (ymax - y) / 2 + box[1] - self._fp_write("gsave\n{:f} {:f} translate\n".format(dx, dy)) + self._fp_write(f"gsave\n{dx:f} {dy:f} translate\n") if (x, y) != im.size: # EpsImagePlugin._save prints the image at (0,0,xsize,ysize) sx = x / im.size[0] sy = y / im.size[1] - self._fp_write("{:f} {:f} scale\n".format(sx, sy)) + self._fp_write(f"{sx:f} {sy:f} scale\n") EpsImagePlugin._save(im, self.fp, None, 0) self._fp_write("\ngrestore\n") diff --git a/src/PIL/PalmImagePlugin.py b/src/PIL/PalmImagePlugin.py index 9fc55d795..700f10e3f 100644 --- a/src/PIL/PalmImagePlugin.py +++ b/src/PIL/PalmImagePlugin.py @@ -8,7 +8,8 @@ ## from . import Image, ImageFile -from ._binary import o8, o16be as o16b +from ._binary import o8 +from ._binary import o16be as o16b # fmt: off _Palm8BitColormapValues = ( @@ -137,7 +138,7 @@ def _save(im, fp, filename): bpp = im.info["bpp"] im = im.point(lambda x, maxval=(1 << bpp) - 1: maxval - (x & maxval)) else: - raise OSError("cannot write mode %s as Palm" % im.mode) + raise OSError(f"cannot write mode {im.mode} as Palm") # we ignore the palette here im.mode = "P" @@ -153,7 +154,7 @@ def _save(im, fp, filename): else: - raise OSError("cannot write mode %s as Palm" % im.mode) + raise OSError(f"cannot write mode {im.mode} as Palm") # # make sure image data is available diff --git a/src/PIL/PcfFontFile.py b/src/PIL/PcfFontFile.py index f8836ad88..6a4eb22a6 100644 --- a/src/PIL/PcfFontFile.py +++ b/src/PIL/PcfFontFile.py @@ -19,7 +19,11 @@ import io from . import FontFile, Image -from ._binary import i8, i16be as b16, i16le as l16, i32be as b32, i32le as l32 +from ._binary import i8 +from ._binary import i16be as b16 +from ._binary import i16le as l16 +from ._binary import i32be as b32 +from ._binary import i32le as l32 # -------------------------------------------------------------------- # declarations diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py index f7ae3bf70..767f9945a 100644 --- a/src/PIL/PcxImagePlugin.py +++ b/src/PIL/PcxImagePlugin.py @@ -29,7 +29,10 @@ import io import logging from . import Image, ImageFile, ImagePalette -from ._binary import i8, i16le as i16, o8, o16le as o16 +from ._binary import i8 +from ._binary import i16le as i16 +from ._binary import o8 +from ._binary import o16le as o16 logger = logging.getLogger(__name__) @@ -132,7 +135,7 @@ def _save(im, fp, filename): try: version, bits, planes, rawmode = SAVE[im.mode] except KeyError as e: - raise ValueError("Cannot save %s images as PCX" % im.mode) from e + raise ValueError(f"Cannot save {im.mode} images as PCX") from e # bytes per plane stride = (im.size[0] * bits + 7) // 8 diff --git a/src/PIL/PdfImagePlugin.py b/src/PIL/PdfImagePlugin.py index 47500baf7..36c8fb849 100644 --- a/src/PIL/PdfImagePlugin.py +++ b/src/PIL/PdfImagePlugin.py @@ -77,7 +77,7 @@ def _save(im, fp, filename, save_all=False): existing_pdf.start_writing() existing_pdf.write_header() - existing_pdf.write_comment("created by Pillow {} PDF driver".format(__version__)) + existing_pdf.write_comment(f"created by Pillow {__version__} PDF driver") # # pages @@ -121,6 +121,7 @@ def _save(im, fp, filename, save_all=False): bits = 8 params = None + decode = None if im.mode == "1": filter = "ASCIIHexDecode" @@ -129,7 +130,7 @@ def _save(im, fp, filename, save_all=False): bits = 1 elif im.mode == "L": filter = "DCTDecode" - # params = "<< /Predictor 15 /Columns %d >>" % (width-2) + # params = f"<< /Predictor 15 /Columns {width-2} >>" colorspace = PdfParser.PdfName("DeviceGray") procset = "ImageB" # grayscale elif im.mode == "P": @@ -150,8 +151,9 @@ def _save(im, fp, filename, save_all=False): filter = "DCTDecode" colorspace = PdfParser.PdfName("DeviceCMYK") procset = "ImageC" # color images + decode = [1, 0, 1, 0, 1, 0, 1, 0] else: - raise ValueError("cannot save mode %s" % im.mode) + raise ValueError(f"cannot save mode {im.mode}") # # image @@ -173,7 +175,7 @@ def _save(im, fp, filename, save_all=False): elif filter == "RunLengthDecode": ImageFile._save(im, op, [("packbits", (0, 0) + im.size, 0, im.mode)]) else: - raise ValueError("unsupported PDF filter (%s)" % filter) + raise ValueError(f"unsupported PDF filter ({filter})") # # Get image characteristics @@ -189,6 +191,7 @@ def _save(im, fp, filename, save_all=False): Height=height, # * 72.0 / resolution, Filter=PdfParser.PdfName(filter), BitsPerComponent=bits, + Decode=decode, DecodeParams=params, ColorSpace=colorspace, ) diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py index 3c343c5e8..975905f96 100644 --- a/src/PIL/PdfParser.py +++ b/src/PIL/PdfParser.py @@ -183,8 +183,8 @@ class XrefTable: this_deleted_object_id = deleted_keys.pop(0) check_format_condition( object_id == this_deleted_object_id, - "expected the next deleted object ID to be %s, instead found %s" - % (object_id, this_deleted_object_id), + f"expected the next deleted object ID to be {object_id}, " + f"instead found {this_deleted_object_id}", ) try: next_in_linked_list = deleted_keys[0] @@ -218,7 +218,7 @@ class PdfName: return hash(self.name) def __repr__(self): - return "PdfName(%s)" % repr(self.name) + return f"PdfName({repr(self.name)})" @classmethod def from_pdf_stream(cls, data): @@ -315,7 +315,7 @@ class PdfStream: return zlib.decompress(self.buf, bufsize=int(expected_length)) else: raise NotImplementedError( - "stream filter %s unknown/unsupported" % repr(self.dictionary.Filter) + f"stream filter {repr(self.dictionary.Filter)} unknown/unsupported" ) @@ -423,7 +423,7 @@ class PdfParser: self.f.write(b"%PDF-1.4\n") def write_comment(self, s): - self.f.write(("% {}\n".format(s)).encode("utf-8")) + self.f.write(f"% {s}\n".encode("utf-8")) def write_catalog(self): self.del_root() @@ -966,9 +966,8 @@ class PdfParser: offset, generation = self.xref_table[ref[0]] check_format_condition( generation == ref[1], - "expected to find generation %s for object ID %s in xref table, " - "instead found generation %s at offset %s" - % (ref[1], ref[0], generation, offset), + f"expected to find generation {ref[1]} for object ID {ref[0]} in xref " + f"table, instead found generation {generation} at offset {offset}", ) value = self.get_value( self.buf, diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 9d048b211..bcc6b1c98 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -39,7 +39,12 @@ import warnings import zlib from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence -from ._binary import i8, i16be as i16, i32be as i32, o8, o16be as o16, o32be as o32 +from ._binary import i8 +from ._binary import i16be as i16 +from ._binary import i32be as i32 +from ._binary import o8 +from ._binary import o16be as o16 +from ._binary import o32be as o32 logger = logging.getLogger(__name__) @@ -159,7 +164,7 @@ class ChunkStream: if not is_cid(cid): if not ImageFile.LOAD_TRUNCATED_IMAGES: - raise SyntaxError("broken PNG file (chunk %s)" % repr(cid)) + raise SyntaxError(f"broken PNG file (chunk {repr(cid)})") return cid, pos, length @@ -196,10 +201,12 @@ class ChunkStream: crc1 = _crc32(data, _crc32(cid)) crc2 = i32(self.fp.read(4)) if crc1 != crc2: - raise SyntaxError("broken PNG file (bad header checksum in %r)" % cid) + raise SyntaxError( + f"broken PNG file (bad header checksum in {repr(cid)})" + ) except struct.error as e: raise SyntaxError( - "broken PNG file (incomplete checksum in %r)" % cid + f"broken PNG file (incomplete checksum in {repr(cid)})" ) from e def crc_skip(self, cid, data): @@ -351,8 +358,8 @@ class PngStream(ChunkStream): self.text_memory += chunklen if self.text_memory > MAX_TEXT_MEMORY: raise ValueError( - "Too much memory used in text chunks: %s>MAX_TEXT_MEMORY" - % self.text_memory + "Too much memory used in text chunks: " + f"{self.text_memory}>MAX_TEXT_MEMORY" ) def save_rewind(self): @@ -381,9 +388,7 @@ class PngStream(ChunkStream): logger.debug("Compression method %s", i8(s[i])) comp_method = i8(s[i]) if comp_method != 0: - raise SyntaxError( - "Unknown compression method %s in iCCP chunk" % comp_method - ) + raise SyntaxError(f"Unknown compression method {comp_method} in iCCP chunk") try: icc_profile = _safe_zlib_decompress(s[i + 2 :]) except ValueError: @@ -509,10 +514,11 @@ class PngStream(ChunkStream): v = b"" if k: k = k.decode("latin-1", "strict") - v = v.decode("latin-1", "replace") + v_str = v.decode("latin-1", "replace") - self.im_info[k] = self.im_text[k] = v - self.check_text_memory(len(v)) + self.im_info[k] = v if k == "exif" else v_str + self.im_text[k] = v_str + self.check_text_memory(len(v_str)) return s @@ -530,9 +536,7 @@ class PngStream(ChunkStream): else: comp_method = 0 if comp_method != 0: - raise SyntaxError( - "Unknown compression method %s in zTXt chunk" % comp_method - ) + raise SyntaxError(f"Unknown compression method {comp_method} in zTXt chunk") try: v = _safe_zlib_decompress(v[1:]) except ValueError: @@ -794,7 +798,7 @@ class PngImageFile(ImageFile.ImageFile): return else: if frame != self.__frame + 1: - raise ValueError("cannot seek to frame %d" % frame) + raise ValueError(f"cannot seek to frame {frame}") # ensure previous frame was loaded self.load() @@ -1104,7 +1108,10 @@ def _write_multiple_frames(im, fp, chunk, rawmode): # animation control chunk( - fp, b"acTL", o32(len(im_frames)), o32(loop), # 0: num_frames # 4: num_plays + fp, + b"acTL", + o32(len(im_frames)), # 0: num_frames + o32(loop), # 4: num_plays ) # default image IDAT (if it exists) @@ -1149,7 +1156,9 @@ def _write_multiple_frames(im, fp, chunk, rawmode): else: fdat_chunks = _fdat(fp, chunk, seq_num) ImageFile._save( - im_frame, fdat_chunks, [("zip", (0, 0) + im_frame.size, 0, rawmode)], + im_frame, + fdat_chunks, + [("zip", (0, 0) + im_frame.size, 0, rawmode)], ) seq_num = fdat_chunks.seq_num @@ -1186,7 +1195,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False): else: bits = 8 if bits != 8: - mode = "%s;%d" % (mode, bits) + mode = f"{mode};{bits}" # encoder options im.encoderconfig = ( @@ -1200,7 +1209,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False): try: rawmode, mode = _OUTMODES[mode] except KeyError as e: - raise OSError("cannot write mode %s as PNG" % mode) from e + raise OSError(f"cannot write mode {mode} as PNG") from e # # write minimal PNG file diff --git a/src/PIL/PpmImagePlugin.py b/src/PIL/PpmImagePlugin.py index 35a77bafb..abf4d651d 100644 --- a/src/PIL/PpmImagePlugin.py +++ b/src/PIL/PpmImagePlugin.py @@ -104,7 +104,7 @@ class PpmImageFile(ImageFile.ImageFile): # maxgrey if s > 255: if not mode == "L": - raise ValueError("Too many colors for band: %s" % s) + raise ValueError(f"Too many colors for band: {s}") if s < 2 ** 16: self.mode = "I" rawmode = "I;16B" @@ -135,7 +135,7 @@ def _save(im, fp, filename): elif im.mode == "RGBA": rawmode, head = "RGB", b"P6" else: - raise OSError("cannot write mode %s as PPM" % im.mode) + raise OSError(f"cannot write mode {im.mode} as PPM") fp.write(head + ("\n%d %d\n" % im.size).encode("ascii")) if head == b"P6": fp.write(b"255\n") diff --git a/src/PIL/PsdImagePlugin.py b/src/PIL/PsdImagePlugin.py index 80bc116fc..8d1dbf2b2 100644 --- a/src/PIL/PsdImagePlugin.py +++ b/src/PIL/PsdImagePlugin.py @@ -19,7 +19,9 @@ import io from . import Image, ImageFile, ImagePalette -from ._binary import i8, i16be as i16, i32be as i32 +from ._binary import i8 +from ._binary import i16be as i16 +from ._binary import i32be as i32 MODES = { # (photoshop mode, bits) -> (pil mode, required channels) diff --git a/src/PIL/SgiImagePlugin.py b/src/PIL/SgiImagePlugin.py index ec9855e77..f878fefa9 100644 --- a/src/PIL/SgiImagePlugin.py +++ b/src/PIL/SgiImagePlugin.py @@ -26,7 +26,9 @@ import os import struct from . import Image, ImageFile -from ._binary import i8, i16be as i16, o8 +from ._binary import i8 +from ._binary import i16be as i16 +from ._binary import o8 def _accept(prefix): @@ -158,9 +160,7 @@ def _save(im, fp, filename): # assert we've got the right number of bands. if len(im.getbands()) != z: raise ValueError( - "incorrect number of bands in SGI write: {} vs {}".format( - z, len(im.getbands()) - ) + f"incorrect number of bands in SGI write: {z} vs {len(im.getbands())}" ) # Minimum Byte value diff --git a/src/PIL/SpiderImagePlugin.py b/src/PIL/SpiderImagePlugin.py index 56aac2987..819f2ed0a 100644 --- a/src/PIL/SpiderImagePlugin.py +++ b/src/PIL/SpiderImagePlugin.py @@ -213,7 +213,7 @@ def loadImageSeries(filelist=None): imglist = [] for img in filelist: if not os.path.exists(img): - print("unable to find %s" % img) + print(f"unable to find {img}") continue try: with Image.open(img) as im: @@ -318,7 +318,7 @@ if __name__ == "__main__": # perform some image operation im = im.transpose(Image.FLIP_LEFT_RIGHT) print( - "saving a flipped version of %s as %s " - % (os.path.basename(filename), outfile) + f"saving a flipped version of {os.path.basename(filename)} " + f"as {outfile} " ) im.save(outfile, SpiderImageFile.format) diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index 566f0ac18..69b3e0678 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -20,7 +20,10 @@ import warnings from . import Image, ImageFile, ImagePalette -from ._binary import i8, i16le as i16, o8, o16le as o16 +from ._binary import i8 +from ._binary import i16le as i16 +from ._binary import o8 +from ._binary import o16le as o16 # # -------------------------------------------------------------------- @@ -168,7 +171,7 @@ def _save(im, fp, filename): try: rawmode, bits, colormaptype, imagetype = SAVE[im.mode] except KeyError as e: - raise OSError("cannot write mode %s as TGA" % im.mode) from e + raise OSError(f"cannot write mode {im.mode} as TGA") from e if "rle" in im.encoderinfo: rle = im.encoderinfo["rle"] diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index cb4addd18..ac309c560 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -286,7 +286,7 @@ _write_dispatch = {} class IFDRational(Rational): - """ Implements a rational class where 0/0 is a legal value to match + """Implements a rational class where 0/0 is a legal value to match the in the wild use of exif rationals. e.g., DigitalZoomRatio - 0.00/0.00 indicates that no digital zoom was used @@ -353,6 +353,8 @@ class IFDRational(Rational): return self._val.__hash__() def __eq__(self, other): + if isinstance(other, IFDRational): + other = other._val return self._val == other def _delegate(op): @@ -414,7 +416,7 @@ class ImageFileDirectory_v2(MutableMapping): The tiff metadata type of each item is stored in a dictionary of tag types in - `~PIL.TiffImagePlugin.ImageFileDirectory_v2.tagtype`. The types + :attr:`~PIL.TiffImagePlugin.ImageFileDirectory_v2.tagtype`. The types are read from a tiff file, guessed from the type added, or added manually. @@ -464,7 +466,7 @@ class ImageFileDirectory_v2(MutableMapping): :param prefix: Override the endianness of the file. """ if ifh[:4] not in PREFIXES: - raise SyntaxError("not a TIFF file (header %r not valid)" % ifh) + raise SyntaxError(f"not a TIFF file (header {repr(ifh)} not valid)") self._prefix = prefix if prefix is not None else ifh[:2] if self._prefix == MM: self._endian = ">" @@ -472,6 +474,8 @@ class ImageFileDirectory_v2(MutableMapping): self._endian = "<" else: raise SyntaxError("not a TIFF IFD") + self.tagtype = {} + """ Dictionary of tag types """ self.reset() (self.next,) = self._unpack("L", ifh[4:]) self._legacy_api = False @@ -565,7 +569,9 @@ class ImageFileDirectory_v2(MutableMapping): elif self.tagtype[tag] == TiffTags.RATIONAL: values = [float(v) if isinstance(v, int) else v for v in values] - values = tuple(info.cvt_enum(value) for value in values) + is_ifd = self.tagtype[tag] == TiffTags.LONG and isinstance(values, dict) + if not is_ifd: + values = tuple(info.cvt_enum(value) for value in values) dest = self._tags_v1 if legacy_api else self._tags_v2 @@ -574,7 +580,7 @@ class ImageFileDirectory_v2(MutableMapping): # Spec'd length == 1, Actual > 1, Warn and truncate. Formerly barfed. # No Spec, Actual length 1, Formerly (<4.2) returned a 1 element tuple. # Don't mess with the legacy api, since it's frozen. - if ( + if not is_ifd and ( (info.length == 1) or self.tagtype[tag] == TiffTags.BYTE or (info.length is None and len(values) == 1 and not legacy_api) @@ -590,8 +596,8 @@ class ImageFileDirectory_v2(MutableMapping): except ValueError: # We've got a builtin tag with 1 expected entry warnings.warn( - "Metadata Warning, tag %s had too many entries: %s, expected 1" - % (tag, len(values)) + f"Metadata Warning, tag {tag} had too many entries: " + f"{len(values)}, expected 1" ) dest[tag] = values[0] @@ -726,7 +732,7 @@ class ImageFileDirectory_v2(MutableMapping): if len(ret) != size: raise OSError( "Corrupt EXIF data. " - + "Expecting to read %d bytes but only got %d. " % (size, len(ret)) + f"Expecting to read {size} bytes but only got {len(ret)}. " ) return ret @@ -741,18 +747,18 @@ class ImageFileDirectory_v2(MutableMapping): tagname = TiffTags.lookup(tag).name typname = TYPES.get(typ, "unknown") - msg = "tag: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ) + msg = f"tag: {tagname} ({tag}) - type: {typname} ({typ})" try: unit_size, handler = self._load_dispatch[typ] except KeyError: - logger.debug(msg + " - unsupported type {}".format(typ)) + logger.debug(msg + f" - unsupported type {typ}") continue # ignore unsupported type size = count * unit_size if size > 4: here = fp.tell() (offset,) = self._unpack("L", data) - msg += " Tag Location: {} - Data Location: {}".format(here, offset) + msg += f" Tag Location: {here} - Data Location: {offset}" fp.seek(offset) data = ImageFile._safe_read(fp, size) fp.seek(here) @@ -762,8 +768,8 @@ class ImageFileDirectory_v2(MutableMapping): if len(data) != size: warnings.warn( "Possibly corrupt EXIF data. " - "Expecting to read %d bytes but only got %d." - " Skipping tag %s" % (size, len(data), tag) + f"Expecting to read {size} bytes but only got {len(data)}." + f" Skipping tag {tag}" ) logger.debug(msg) continue @@ -799,20 +805,33 @@ class ImageFileDirectory_v2(MutableMapping): if tag == STRIPOFFSETS: stripoffsets = len(entries) typ = self.tagtype.get(tag) - logger.debug("Tag {}, Type: {}, Value: {!r}".format(tag, typ, value)) - values = value if isinstance(value, tuple) else (value,) - data = self._write_dispatch[typ](self, *values) + logger.debug(f"Tag {tag}, Type: {typ}, Value: {repr(value)}") + is_ifd = typ == TiffTags.LONG and isinstance(value, dict) + if is_ifd: + if self._endian == "<": + ifh = b"II\x2A\x00\x08\x00\x00\x00" + else: + ifh = b"MM\x00\x2A\x00\x00\x00\x08" + ifd = ImageFileDirectory_v2(ifh) + for ifd_tag, ifd_value in self._tags_v2[tag].items(): + ifd[ifd_tag] = ifd_value + data = ifd.tobytes(offset) + else: + values = value if isinstance(value, tuple) else (value,) + data = self._write_dispatch[typ](self, *values) tagname = TiffTags.lookup(tag).name - typname = TYPES.get(typ, "unknown") - msg = "save: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ) + typname = "ifd" if is_ifd else TYPES.get(typ, "unknown") + msg = f"save: {tagname} ({tag}) - type: {typname} ({typ})" msg += " - value: " + ( "" % len(data) if len(data) >= 16 else str(values) ) logger.debug(msg) # count is sum of lengths for string and arbitrary data - if typ in [TiffTags.BYTE, TiffTags.ASCII, TiffTags.UNDEFINED]: + if is_ifd: + count = 1 + elif typ in [TiffTags.BYTE, TiffTags.ASCII, TiffTags.UNDEFINED]: count = len(data) else: count = len(values) @@ -833,9 +852,7 @@ class ImageFileDirectory_v2(MutableMapping): # pass 2: write entries to file for tag, typ, count, value, data in entries: - logger.debug( - "{} {} {} {} {}".format(tag, typ, count, repr(value), repr(data)) - ) + logger.debug(f"{tag} {typ} {count} {repr(value)} {repr(data)}") result += self._pack("HHL4s", tag, typ, count, value) # -- overwrite here for multi-page -- @@ -883,7 +900,7 @@ class ImageFileDirectory_v1(ImageFileDirectory_v2): ('Some Data',) Also contains a dictionary of tag types as read from the tiff image file, - `~PIL.TiffImagePlugin.ImageFileDirectory_v1.tagtype`. + :attr:`~PIL.TiffImagePlugin.ImageFileDirectory_v1.tagtype`. Values are returned as a tuple. @@ -897,9 +914,13 @@ class ImageFileDirectory_v1(ImageFileDirectory_v2): tags = property(lambda self: self._tags_v1) tagdata = property(lambda self: self._tagdata) + # defined in ImageFileDirectory_v2 + tagtype: dict + """Dictionary of tag types""" + @classmethod def from_v2(cls, original): - """ Returns an + """Returns an :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1` instance with the same data as is contained in the original :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2` @@ -916,7 +937,7 @@ class ImageFileDirectory_v1(ImageFileDirectory_v2): return ifd def to_v2(self): - """ Returns an + """Returns an :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2` instance with the same data as is contained in the original :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1` @@ -972,17 +993,25 @@ class TiffImageFile(ImageFile.ImageFile): format_description = "Adobe TIFF" _close_exclusive_fp_after_loading = False + def __init__(self, fp=None, filename=None): + self.tag_v2 = None + """ Image file directory (tag dictionary) """ + + self.tag = None + """ Legacy tag entries """ + + super().__init__(fp, filename) + def _open(self): """Open the first image in a TIFF file""" # Header ifh = self.fp.read(8) - # image file directory (tag dictionary) self.tag_v2 = ImageFileDirectory_v2(ifh) - # legacy tag/ifd entries will be filled in later - self.tag = self.ifd = None + # legacy IFD entries will be filled in later + self.ifd = None # setup frame pointers self.__first = self.__next = self.tag_v2.next @@ -992,8 +1021,8 @@ class TiffImageFile(ImageFile.ImageFile): self._n_frames = None logger.debug("*** TiffImageFile._open ***") - logger.debug("- __first: {}".format(self.__first)) - logger.debug("- ifh: {!r}".format(ifh)) # Use !r to avoid str(bytes) + logger.debug(f"- __first: {self.__first}") + logger.debug(f"- ifh: {repr(ifh)}") # Use repr to avoid str(bytes) # and load the first frame self._seek(0) @@ -1025,8 +1054,8 @@ class TiffImageFile(ImageFile.ImageFile): if not self.__next: raise EOFError("no more images in TIFF file") logger.debug( - "Seeking to frame %s, on frame %s, __next %s, location: %s" - % (frame, self.__frame, self.__next, self.fp.tell()) + f"Seeking to frame {frame}, on frame {self.__frame}, " + f"__next {self.__next}, location: {self.fp.tell()}" ) # reset buffered io handle in case fp # was passed to libtiff, invalidating the buffer @@ -1078,8 +1107,8 @@ class TiffImageFile(ImageFile.ImageFile): self._close_exclusive_fp_after_loading = True def _load_libtiff(self): - """ Overload method triggered when we detect a compressed tiff - Calls out to libtiff """ + """Overload method triggered when we detect a compressed tiff + Calls out to libtiff""" Image.Image.load(self) @@ -1183,18 +1212,18 @@ class TiffImageFile(ImageFile.ImageFile): fillorder = self.tag_v2.get(FILLORDER, 1) logger.debug("*** Summary ***") - logger.debug("- compression: {}".format(self._compression)) - logger.debug("- photometric_interpretation: {}".format(photo)) - logger.debug("- planar_configuration: {}".format(self._planar_configuration)) - logger.debug("- fill_order: {}".format(fillorder)) - logger.debug("- YCbCr subsampling: {}".format(self.tag.get(530))) + logger.debug(f"- compression: {self._compression}") + logger.debug(f"- photometric_interpretation: {photo}") + logger.debug(f"- planar_configuration: {self._planar_configuration}") + logger.debug(f"- fill_order: {fillorder}") + logger.debug(f"- YCbCr subsampling: {self.tag.get(530)}") # size xsize = int(self.tag_v2.get(IMAGEWIDTH)) ysize = int(self.tag_v2.get(IMAGELENGTH)) self._size = xsize, ysize - logger.debug("- size: {}".format(self.size)) + logger.debug(f"- size: {self.size}") sampleFormat = self.tag_v2.get(SAMPLEFORMAT, (1,)) if len(sampleFormat) > 1 and max(sampleFormat) == min(sampleFormat) == 1: @@ -1228,15 +1257,15 @@ class TiffImageFile(ImageFile.ImageFile): bps_tuple, extra_tuple, ) - logger.debug("format key: {}".format(key)) + logger.debug(f"format key: {key}") try: self.mode, rawmode = OPEN_INFO[key] except KeyError as e: logger.debug("- unsupported format") raise SyntaxError("unknown pixel mode") from e - logger.debug("- raw mode: {}".format(rawmode)) - logger.debug("- pil mode: {}".format(self.mode)) + logger.debug(f"- raw mode: {rawmode}") + logger.debug(f"- pil mode: {self.mode}") self.info["compression"] = self._compression @@ -1277,7 +1306,7 @@ class TiffImageFile(ImageFile.ImageFile): if fillorder == 2: # Replace fillorder with fillorder=1 key = key[:3] + (1,) + key[4:] - logger.debug("format key: {}".format(key)) + logger.debug(f"format key: {key}") # this should always work, since all the # fillorder==2 modes have a corresponding # fillorder=1 mode @@ -1401,7 +1430,7 @@ def _save(im, fp, filename): try: rawmode, prefix, photo, format, bits, extra = SAVE_INFO[im.mode] except KeyError as e: - raise OSError("cannot write mode %s as TIFF" % im.mode) from e + raise OSError(f"cannot write mode {im.mode} as TIFF") from e ifd = ImageFileDirectory_v2(prefix=prefix) @@ -1598,7 +1627,7 @@ def _save(im, fp, filename): if s: break if s < 0: - raise OSError("encoder error %d when writing image file" % s) + raise OSError(f"encoder error {s} when writing image file") else: offset = ifd.save(fp) @@ -1764,29 +1793,29 @@ class AppendingTiffWriter: self.f.seek(-2, os.SEEK_CUR) bytesWritten = self.f.write(struct.pack(self.longFmt, value)) if bytesWritten is not None and bytesWritten != 4: - raise RuntimeError("wrote only %u bytes but wanted 4" % bytesWritten) + raise RuntimeError(f"wrote only {bytesWritten} bytes but wanted 4") def rewriteLastShort(self, value): self.f.seek(-2, os.SEEK_CUR) bytesWritten = self.f.write(struct.pack(self.shortFmt, value)) if bytesWritten is not None and bytesWritten != 2: - raise RuntimeError("wrote only %u bytes but wanted 2" % bytesWritten) + raise RuntimeError(f"wrote only {bytesWritten} bytes but wanted 2") def rewriteLastLong(self, value): self.f.seek(-4, os.SEEK_CUR) bytesWritten = self.f.write(struct.pack(self.longFmt, value)) if bytesWritten is not None and bytesWritten != 4: - raise RuntimeError("wrote only %u bytes but wanted 4" % bytesWritten) + raise RuntimeError(f"wrote only {bytesWritten} bytes but wanted 4") def writeShort(self, value): bytesWritten = self.f.write(struct.pack(self.shortFmt, value)) if bytesWritten is not None and bytesWritten != 2: - raise RuntimeError("wrote only %u bytes but wanted 2" % bytesWritten) + raise RuntimeError(f"wrote only {bytesWritten} bytes but wanted 2") def writeLong(self, value): bytesWritten = self.f.write(struct.pack(self.longFmt, value)) if bytesWritten is not None and bytesWritten != 4: - raise RuntimeError("wrote only %u bytes but wanted 4" % bytesWritten) + raise RuntimeError(f"wrote only {bytesWritten} bytes but wanted 4") def close(self): self.finalize() diff --git a/src/PIL/WmfImagePlugin.py b/src/PIL/WmfImagePlugin.py index 024222c9b..87847a107 100644 --- a/src/PIL/WmfImagePlugin.py +++ b/src/PIL/WmfImagePlugin.py @@ -20,7 +20,10 @@ # http://wvware.sourceforge.net/caolan/ora-wmf.html from . import Image, ImageFile -from ._binary import i16le as word, i32le as dword, si16le as short, si32le as _long +from ._binary import i16le as word +from ._binary import i32le as dword +from ._binary import si16le as short +from ._binary import si32le as _long _handler = None diff --git a/src/PIL/XbmImagePlugin.py b/src/PIL/XbmImagePlugin.py index ead9722c8..644cfb39b 100644 --- a/src/PIL/XbmImagePlugin.py +++ b/src/PIL/XbmImagePlugin.py @@ -69,15 +69,15 @@ class XbmImageFile(ImageFile.ImageFile): def _save(im, fp, filename): if im.mode != "1": - raise OSError("cannot write mode %s as XBM" % im.mode) + raise OSError(f"cannot write mode {im.mode} as XBM") - fp.write(("#define im_width %d\n" % im.size[0]).encode("ascii")) - fp.write(("#define im_height %d\n" % im.size[1]).encode("ascii")) + fp.write(f"#define im_width {im.size[0]}\n".encode("ascii")) + fp.write(f"#define im_height {im.size[1]}\n".encode("ascii")) hotspot = im.encoderinfo.get("hotspot") if hotspot: - fp.write(("#define im_x_hot %d\n" % hotspot[0]).encode("ascii")) - fp.write(("#define im_y_hot %d\n" % hotspot[1]).encode("ascii")) + fp.write(f"#define im_x_hot {hotspot[0]}\n".encode("ascii")) + fp.write(f"#define im_y_hot {hotspot[1]}\n".encode("ascii")) fp.write(b"static char im_bits[] = {\n") diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index d225ed134..d4f5ea76a 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -39,7 +39,7 @@ if sys.version_info >= (3, 7): if name == "PILLOW_VERSION": _raise_version_warning() return __version__ - raise AttributeError("module '{}' has no attribute '{}'".format(__name__, name)) + raise AttributeError(f"module '{__name__}' has no attribute '{name}'") else: diff --git a/src/PIL/_util.py b/src/PIL/_util.py index 755b4b272..0c5d3892e 100644 --- a/src/PIL/_util.py +++ b/src/PIL/_util.py @@ -1,20 +1,9 @@ import os -import sys - -py36 = sys.version_info[0:2] >= (3, 6) +from pathlib import Path -if py36: - from pathlib import Path - - def isPath(f): - return isinstance(f, (bytes, str, Path)) - - -else: - - def isPath(f): - return isinstance(f, (bytes, str)) +def isPath(f): + return isinstance(f, (bytes, str, Path)) # Checks if an object is a string, and that it points to a directory. diff --git a/src/PIL/features.py b/src/PIL/features.py index 66b093350..fa86b9cb0 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -25,7 +25,7 @@ def check_module(feature): :raises ValueError: If the module is not defined in this version of Pillow. """ if not (feature in modules): - raise ValueError("Unknown module %s" % feature) + raise ValueError(f"Unknown module {feature}") module, ver = modules[feature] @@ -78,7 +78,7 @@ def check_codec(feature): :raises ValueError: If the codec is not defined in this version of Pillow. """ if feature not in codecs: - raise ValueError("Unknown codec %s" % feature) + raise ValueError(f"Unknown codec {feature}") codec, lib = codecs[feature] @@ -133,7 +133,7 @@ def check_feature(feature): :raises ValueError: If the feature is not defined in this version of Pillow. """ if feature not in features: - raise ValueError("Unknown feature %s" % feature) + raise ValueError(f"Unknown feature {feature}") module, flag, ver = features[feature] @@ -182,7 +182,7 @@ def check(feature): return check_codec(feature) if feature in features: return check_feature(feature) - warnings.warn("Unknown feature '%s'." % feature, stacklevel=2) + warnings.warn(f"Unknown feature '{feature}'.", stacklevel=2) return False @@ -230,18 +230,18 @@ def pilinfo(out=None, supported_formats=True): Image.init() print("-" * 68, file=out) - print("Pillow {}".format(PIL.__version__), file=out) + print(f"Pillow {PIL.__version__}", file=out) py_version = sys.version.splitlines() - print("Python {}".format(py_version[0].strip()), file=out) + print(f"Python {py_version[0].strip()}", file=out) for py_version in py_version[1:]: - print(" {}".format(py_version.strip()), file=out) + print(f" {py_version.strip()}", file=out) print("-" * 68, file=out) print( - "Python modules loaded from {}".format(os.path.dirname(Image.__file__)), + f"Python modules loaded from {os.path.dirname(Image.__file__)}", file=out, ) print( - "Binary modules loaded from {}".format(os.path.dirname(Image.core.__file__)), + f"Binary modules loaded from {os.path.dirname(Image.core.__file__)}", file=out, ) print("-" * 68, file=out) @@ -283,9 +283,9 @@ def pilinfo(out=None, supported_formats=True): extensions[i].append(ext) for i in sorted(Image.ID): - line = "{}".format(i) + line = f"{i}" if i in Image.MIME: - line = "{} {}".format(line, Image.MIME[i]) + line = f"{line} {Image.MIME[i]}" print(line, file=out) if i in extensions: diff --git a/src/decode.c b/src/decode.c index 25ce7316b..f60fb490e 100644 --- a/src/decode.c +++ b/src/decode.c @@ -807,7 +807,7 @@ PyImaging_XbmDecoderNew(PyObject* self, PyObject* args) #ifdef HAVE_LIBZ -#include "Zip.h" +#include "ZipCodecs.h" PyObject* PyImaging_ZipDecoderNew(PyObject* self, PyObject* args) diff --git a/src/encode.c b/src/encode.c index d64f47d2b..62a6ec387 100644 --- a/src/encode.c +++ b/src/encode.c @@ -578,7 +578,7 @@ PyImaging_XbmEncoderNew(PyObject* self, PyObject* args) #ifdef HAVE_LIBZ -#include "Zip.h" +#include "ZipCodecs.h" PyObject* PyImaging_ZipEncoderNew(PyObject* self, PyObject* args) diff --git a/src/libImaging/Zip.h b/src/libImaging/ZipCodecs.h similarity index 100% rename from src/libImaging/Zip.h rename to src/libImaging/ZipCodecs.h diff --git a/src/libImaging/ZipDecode.c b/src/libImaging/ZipDecode.c index b0f8ad326..a09ee82f7 100644 --- a/src/libImaging/ZipDecode.c +++ b/src/libImaging/ZipDecode.c @@ -20,7 +20,7 @@ #ifdef HAVE_LIBZ -#include "Zip.h" +#include "ZipCodecs.h" static const int OFFSET[] = { 7, 3, 3, 1, 1, 0, 0 }; static const int STARTING_COL[] = { 0, 4, 0, 2, 0, 1, 0 }; diff --git a/src/libImaging/ZipEncode.c b/src/libImaging/ZipEncode.c index 84ccb14ea..4e862af57 100644 --- a/src/libImaging/ZipEncode.c +++ b/src/libImaging/ZipEncode.c @@ -19,7 +19,7 @@ #ifdef HAVE_LIBZ -#include "Zip.h" +#include "ZipCodecs.h" int ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) diff --git a/winbuild/appveyor_build_msys2.sh b/winbuild/appveyor_build_msys2.sh deleted file mode 100644 index 489f9411e..000000000 --- a/winbuild/appveyor_build_msys2.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -cd /c/pillow && /mingw32/$EXECUTABLE setup.py install diff --git a/winbuild/appveyor_install_msys2_deps.sh b/winbuild/appveyor_install_msys2_deps.sh deleted file mode 100644 index 4cc01082d..000000000 --- a/winbuild/appveyor_install_msys2_deps.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -mkdir /var/cache/pacman/pkg -pacman -S --noconfirm mingw32/mingw-w64-i686-python3-pip \ - mingw32/mingw-w64-i686-python3-setuptools \ - mingw32/mingw-w64-i686-python3-pytest \ - mingw32/mingw-w64-i686-python3-pytest-cov \ - mingw-w64-i686-libjpeg-turbo \ - mingw-w64-i686-libimagequant - -C:/msys64/mingw32/bin/python3 -m pip install --upgrade pip - -/mingw32/bin/pip install olefile -/mingw32/bin/pip3 install olefile diff --git a/winbuild/build.rst b/winbuild/build.rst index aaed2c43f..ba568a030 100644 --- a/winbuild/build.rst +++ b/winbuild/build.rst @@ -81,6 +81,9 @@ Pillow for the selected version of Python. ``winbuild\build\build_pillow.cmd bdist_wheel`` will build wheels instead of installing Pillow. +You can also use ``winbuild\build\build_pillow.cmd --inplace develop`` to build +and install Pillow in develop mode (instead of ``pip install --editable``). + Testing Pillow -------------- diff --git a/winbuild/build_prepare.py b/winbuild/build_prepare.py index 6f9cf4038..e05ac1a29 100644 --- a/winbuild/build_prepare.py +++ b/winbuild/build_prepare.py @@ -205,7 +205,7 @@ deps = { # retarget to default toolset (selected by vcvarsall.bat) "v141": "$(DefaultPlatformToolset)", # noqa: E501 # retarget to latest (selected by vcvarsall.bat) - "8.1": "$(WindowsSDKVersion)", # noqa: E501 + "10.0.17134.0": "$(WindowsSDKVersion)", # noqa: E501 } }, "build": [ @@ -251,9 +251,9 @@ deps = { "libs": [r"*.lib"], }, "harfbuzz": { - "url": "https://github.com/harfbuzz/harfbuzz/archive/2.6.8.zip", - "filename": "harfbuzz-2.6.8.zip", - "dir": "harfbuzz-2.6.8", + "url": "https://github.com/harfbuzz/harfbuzz/archive/2.7.2.zip", + "filename": "harfbuzz-2.7.2.zip", + "dir": "harfbuzz-2.7.2", "build": [ cmd_cmake("-DHB_HAVE_FREETYPE:BOOL=TRUE"), cmd_nmake(target="clean"), @@ -356,8 +356,8 @@ def find_msvs(): def extract_dep(url, filename): - import urllib.request import tarfile + import urllib.request import zipfile file = os.path.join(depends_dir, filename) @@ -378,10 +378,10 @@ def extract_dep(url, filename): print("Extracting " + filename) if filename.endswith(".zip"): with zipfile.ZipFile(file) as zf: - zf.extractall(build_dir) + zf.extractall(sources_dir) elif filename.endswith(".tar.gz") or filename.endswith(".tgz"): with tarfile.open(file, "r:gz") as tgz: - tgz.extractall(build_dir) + tgz.extractall(sources_dir) else: raise RuntimeError("Unknown archive type: " + filename) @@ -416,11 +416,14 @@ def build_dep(name): extract_dep(dep["url"], dep["filename"]) for patch_file, patch_list in dep.get("patch", {}).items(): - patch_file = os.path.join(build_dir, dir, patch_file.format(**prefs)) - with open(patch_file, "r") as f: + patch_file = os.path.join(sources_dir, dir, patch_file.format(**prefs)) + with open(patch_file) as f: text = f.read() for patch_from, patch_to in patch_list.items(): - text = text.replace(patch_from.format(**prefs), patch_to.format(**prefs)) + patch_from = patch_from.format(**prefs) + patch_to = patch_to.format(**prefs) + assert patch_from in text + text = text.replace(patch_from, patch_to) with open(patch_file, "w") as f: f.write(text) @@ -429,7 +432,7 @@ def build_dep(name): "@echo " + ("=" * 70), f"@echo ==== {banner:<60} ====", "@echo " + ("=" * 70), - "cd /D %s" % os.path.join(build_dir, dir), + "cd /D %s" % os.path.join(sources_dir, dir), *prefs["header"], *dep.get("build", []), *get_footer(dep), @@ -478,6 +481,7 @@ if __name__ == "__main__": "ARCHITECTURE", "x86" if struct.calcsize("P") == 4 else "x64" ) build_dir = os.environ.get("PILLOW_BUILD", os.path.join(winbuild_dir, "build")) + sources_dir = "" for arg in sys.argv[1:]: if arg == "-v": verbose = True @@ -495,6 +499,8 @@ if __name__ == "__main__": architecture = arg[15:] elif arg.startswith("--dir="): build_dir = arg[6:] + elif arg == "--srcdir": + sources_dir = os.path.sep + "src" else: raise ValueError("Unknown parameter: " + arg) @@ -525,10 +531,13 @@ if __name__ == "__main__": lib_dir = os.path.join(build_dir, "lib") # build directory for *.bin files bin_dir = os.path.join(build_dir, "bin") + # directory for storing project files + sources_dir = build_dir + sources_dir shutil.rmtree(build_dir, ignore_errors=True) - for path in [build_dir, inc_dir, lib_dir, bin_dir]: - os.makedirs(path) + os.makedirs(build_dir, exist_ok=False) + for path in [inc_dir, lib_dir, bin_dir, sources_dir]: + os.makedirs(path, exist_ok=True) prefs = { # Python paths / preferences @@ -544,6 +553,7 @@ if __name__ == "__main__": "inc_dir": inc_dir, "lib_dir": lib_dir, "bin_dir": bin_dir, + "src_dir": sources_dir, # Compilers / Tools **msvs, "cmake": "cmake.exe", # TODO find CMAKE automatically @@ -554,5 +564,6 @@ if __name__ == "__main__": print() + write_script(".gitignore", ["*"]) build_dep_all() build_pillow()