### Full Windows Build Example Script Source: https://github.com/python-pillow/pillow/blob/main/winbuild/build.rst A comprehensive example script demonstrating the complete process of building Pillow on Windows, including setting Python path, preparing the build, building dependencies, installing Pillow, setting the PATH for binaries, running self-tests, and creating a wheel. ```batch set PYTHON=C:\Python310\bin cd /D C:\Pillow\winbuild %PYTHON%\python.exe build_prepare.py -v --depends C:\pillow-depends build\build_dep_all.cmd build\build_env.cmd cd .. %PYTHON%\python.exe -m pip install -v -C raqm=vendor -C fribidi=vendor . path C:\Pillow\winbuild\build\bin;%PATH% %PYTHON%\python.exe selftest.py %PYTHON%\python.exe -m pytest -vv -x --cov PIL --cov Tests --cov-report term --cov-report xml Tests %PYTHON%\python.exe -m pip wheel -v -C raqm=vendor -C fribidi=vendor . ``` -------------------------------- ### Sample Pillow Installation with Feature Enable Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Example command to upgrade Pillow and enable a specific feature during the build process. ```bash python3 -m pip install --upgrade Pillow -C [feature]=enable ``` -------------------------------- ### Install libraqm Dependencies on Ubuntu Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Meson build system and provides instructions to install libraqm using a provided script. ```bash sudo apt-get install meson # Then see depends/install_raqm.sh ``` -------------------------------- ### Build and Install Raqm Source: https://github.com/python-pillow/pillow/blob/main/src/thirdparty/raqm/README.md Builds and installs the Raqm library using Meson and Ninja build system. ```bash $ meson build $ ninja -C build $ ninja -C build install ``` -------------------------------- ### Install Raqm Dependencies on Ubuntu Source: https://github.com/python-pillow/pillow/blob/main/src/thirdparty/raqm/README.md Installs the necessary development libraries for building Raqm on Ubuntu systems. ```bash sudo apt-get install libfreetype6-dev libharfbuzz-dev libfribidi-dev meson gtk-doc-tools ``` -------------------------------- ### Install Pillow from Source Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Pillow using a local source directory. Ensure you have the necessary build tools and dependencies installed. ```bash python3 -m pip install . ``` -------------------------------- ### Install Prerequisites on Alpine Linux Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs development packages for building Pillow on Alpine Linux using apk. ```bash sudo apk add tiff-dev jpeg-dev openjpeg-dev zlib-dev freetype-dev lcms2-dev \ libwebp-dev tcl-dev tk-dev harfbuzz-dev fribidi-dev libimagequant-dev \ libxcb-dev libpng-dev ``` -------------------------------- ### Install Pillow with documentation dependencies Source: https://github.com/python-pillow/pillow/blob/main/docs/releasenotes/9.1.0.rst Use this command to install Pillow along with all necessary dependencies for building documentation. ```bash python3 -m pip install .[docs] ``` -------------------------------- ### Install Pillow with all dependencies Source: https://github.com/python-pillow/pillow/blob/main/docs/releasenotes/9.1.0.rst Use this command to install Pillow with all available dependencies for both tests and documentation. ```bash python3 -m pip install .[docs,tests] ``` -------------------------------- ### Install Prerequisites on FreeBSD 10/11 Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs necessary image processing libraries for Pillow on FreeBSD 10 or 11. ```bash sudo pkg install jpeg-turbo tiff webp lcms2 freetype2 openjpeg harfbuzz fribidi libxcb libavif ``` -------------------------------- ### Install Raqm Dependencies on Fedora Source: https://github.com/python-pillow/pillow/blob/main/src/thirdparty/raqm/README.md Installs the necessary development libraries for building Raqm on Fedora systems. ```bash sudo dnf install freetype-devel harfbuzz-devel fribidi-devel meson gtk-doc ``` -------------------------------- ### Install libavif Build Prerequisites on Ubuntu Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs CMake, Ninja, and NASM, which are build prerequisites for libavif on Ubuntu. ```bash sudo apt-get install cmake ninja-build nasm # Then see depends/install_libavif.sh to build and install libavif. ``` -------------------------------- ### Install Pillow from PyPI with Source Build Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs or upgrades Pillow from PyPI, forcing a build from source code. ```bash python3 -m pip install --upgrade Pillow --no-binary :all: ``` -------------------------------- ### SpamImagePlugin.py Example Source: https://github.com/python-pillow/pillow/blob/main/docs/handbook/writing-your-own-image-plugin.rst A sample plugin for a custom 'SPAM' raster image format. It includes format identification, header parsing, and tile descriptor setup. Ensure this plugin is imported manually after Pillow version 2.1.0. ```python from PIL import Image, ImageFile def _accept(prefix: bytes) -> bool: return prefix.startswith(b"SPAM") class SpamImageFile(ImageFile.ImageFile): format = "SPAM" format_description = "Spam raster image" def _open(self) -> None: header = self.fp.read(128).split() # size in pixels (width, height) self._size = int(header[1]), int(header[2]) # mode setting bits = int(header[3]) if bits == 1: self._mode = "1" elif bits == 8: self._mode = "L" elif bits == 24: self._mode = "RGB" else: msg = "unknown number of bits" raise SyntaxError(msg) # data descriptor self.tile = [ImageFile._Tile("raw", (0, 0) + self.size, 128, (self.mode, 0, 1))] Image.register_open(SpamImageFile.format, SpamImageFile, _accept) Image.register_extensions( SpamImageFile.format, [ ".spam", ".spa", # DOS version ], ) ``` -------------------------------- ### Install Libraries on macOS using Homebrew Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs common image processing libraries on macOS using Homebrew. ```bash brew install libavif libjpeg libraqm libtiff little-cms2 openjpeg webp ``` -------------------------------- ### Install Pillow with test dependencies Source: https://github.com/python-pillow/pillow/blob/main/docs/releasenotes/9.1.0.rst Use this command to install Pillow along with all necessary dependencies for running tests. ```bash python3 -m pip install .[tests] ``` -------------------------------- ### Install Prerequisites on Windows with MSYS2/MinGW 64-bit Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs essential image processing libraries for Pillow on 64-bit Windows using MSYS2 MinGW. ```bash pacman -S \ mingw-w64-x86_64-libjpeg-turbo \ mingw-w64-x86_64-zlib \ mingw-w64-x86_64-libtiff \ mingw-w64-x86_64-freetype \ mingw-w64-x86_64-lcms2 \ mingw-w64-x86_64-libwebp \ mingw-w64-x86_64-openjpeg2 \ mingw-w64-x86_64-libimagequant \ mingw-w64-x86_64-libraqm \ mingw-w64-x86_64-libavif ``` -------------------------------- ### Install Python and Build Tools on Windows with MSYS2/MinGW Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Python, pip, and setuptools for 64-bit Windows using MSYS2 MinGW. ```bash pacman -S \ mingw-w64-x86_64-gcc \ mingw-w64-x86_64-python \ mingw-w64-x86_64-python-pip \ mingw-w64-x86_64-python-setuptools ``` -------------------------------- ### Install Python Development Libraries on Alpine Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Python development headers and setuptools for building Pillow on Alpine Linux. ```bash sudo apk add python3-dev py3-setuptools ``` -------------------------------- ### Install Optional Dependencies with pip Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/basic-installation.rst Installs optional dependencies defusedxml and olefile for enhanced image data reading capabilities. ```bash python3 -m pip install --upgrade defusedxml olefile ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/python-pillow/pillow/blob/main/Tests/README.rst Installs the necessary pytest packages for running tests. Ensure you are using python3. ```bash python3 -m pip install pytest pytest-cov pytest-timeout ``` -------------------------------- ### Install Prerequisites on Fedora/CentOS/RHEL Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs necessary development packages for building Pillow on Fedora, CentOS, or RHEL systems using yum or DNF. ```bash sudo yum install -y gcc libjpeg-devel zlib-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel harfbuzz-devel fribidi-devel libraqm-devel libimagequant-devel libxcb-devel ``` -------------------------------- ### Install Pillow from Local Source Code Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Pillow from a local copy of the source code after cloning the repository or extracting an archive. ```bash python3 -m pip install --upgrade pip ``` -------------------------------- ### Install Python Development Libraries on Fedora Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Python development headers and related packages for building Pillow on Fedora. ```bash sudo dnf install python3-devel redhat-rpm-config ``` -------------------------------- ### Install Python Development Libraries on Debian/Ubuntu Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Python development headers and setuptools for building Pillow on Debian or Ubuntu systems. ```bash sudo apt-get install python3-dev python3-setuptools ``` -------------------------------- ### Install Raqm Dependencies on macOS with Homebrew Source: https://github.com/python-pillow/pillow/blob/main/src/thirdparty/raqm/README.md Installs the necessary development libraries for building Raqm on macOS using Homebrew. It also sets an environment variable for XML catalog files required for documentation. ```bash brew install freetype harfbuzz fribidi meson gtk-doc export XML_CATALOG_FILES="/usr/local/etc/xml/catalog" # for the docs ``` -------------------------------- ### Install Python Development Libraries on FreeBSD Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Python development libraries required for building Pillow on FreeBSD 10 or 11. ```bash sudo pkg install python3 ``` -------------------------------- ### Install Xcode Command Line Tools on macOS Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs the necessary Xcode command line tools for compiling Pillow on macOS. ```bash xcode-select --install ``` -------------------------------- ### Install Pillow from Source with Custom CFLAGS Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Pillow from source, specifying custom CFLAGS to include libraries in non-standard locations. ```bash CFLAGS="-I/usr/pkg/include" python3 -m pip install --upgrade Pillow --no-binary :all: ``` -------------------------------- ### Install Pillow Build Prerequisites on Ubuntu Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs essential development libraries for Pillow features like TIFF, JPEG, OpenJPEG, zlib, FreeType, LittleCMS, WebP, Tk, HarfBuzz, FriBiDi, and XCB on Ubuntu. ```bash sudo apt-get install libtiff5-dev libjpeg8-dev libopenjp2-7-dev zlib1g-dev \ libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk \ libharfbuzz-dev libfribidi-dev libxcb1-dev ``` -------------------------------- ### Install Pillow Build Prerequisites on Red Hat/CentOS/Fedora Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs development libraries for Pillow features like TIFF, JPEG, OpenJPEG, zlib, FreeType, LittleCMS, and WebP on Red Hat-based systems. ```bash sudo dnf install libtiff-devel libjpeg-devel openjpeg2-devel zlib-devel \ freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel ``` -------------------------------- ### Display an image Source: https://github.com/python-pillow/pillow/blob/main/docs/handbook/tutorial.rst The show() method displays the image. Note that this method is not very efficient and requires an external utility to be installed. ```python im.show() ``` -------------------------------- ### Install Pillow on FreeBSD via Ports Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/basic-installation.rst Installs Pillow on FreeBSD using the Ports collection. This method compiles Pillow from source. ```bash cd /usr/ports/graphics/py-pillow && make install clean ``` -------------------------------- ### Python Encoder Example (BlpImagePlugin) Source: https://github.com/python-pillow/pillow/blob/main/docs/handbook/writing-your-own-image-plugin.rst An example of a Python-based encoder class for BLP images. It demonstrates subclassing PyEncoder and implementing the encode method. ```python class BlpEncoder(ImageFile.PyEncoder): def encode(self, buffer, output): # ... encode image data to BLP format ... output.write(encoded_data) return b"" ``` -------------------------------- ### Install Pillow with Pip Source: https://github.com/python-pillow/pillow/blob/main/winbuild/build.rst Installs Pillow using pip after setting up the build environment. The -v flag enables verbose output, and -C flags specify vendored dependencies. ```bash winbuild\build\build_env.cmd python.exe -m pip install -v -C raqm=vendor -C fribidi=vendor . ``` -------------------------------- ### Install Pillow on FreeBSD via Packages Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/basic-installation.rst Installs Pillow on FreeBSD using the pkg package manager. This is a pre-compiled binary installation. ```bash pkg install py38-pillow ``` -------------------------------- ### Create and Activate Debug Virtual Environment Source: https://github.com/python-pillow/pillow/blob/main/docs/reference/c_extension_debugging.rst Set up a virtual environment using a debug Python build to ensure your C extension is compiled with symbols. Install dependencies and build the extension within this environment. ```bash virtualenv -p python3.8-dbg ~/vpy38-dbg source ~/vpy38-dbg/bin/activate cd ~/Pillow && make install ``` -------------------------------- ### Build Preparation Script Usage Source: https://github.com/python-pillow/pillow/blob/main/winbuild/build.rst This command configures the build by downloading and generating scripts for Pillow dependencies. Use the -v flag for verbose output and specify directories for build and dependencies. ```bash usage: winbuild\build_prepare.py [-h] [-v] [-d PILLOW_BUILD] [--depends PILLOW_DEPS] [--architecture {x86,AMD64,ARM64}] [--nmake] [--no-imagequant] [--no-fribidi] [--no-avif] Download and generate build scripts for Pillow dependencies. options: -h, --help show this help message and exit -v, --verbose print generated scripts -d PILLOW_BUILD, --dir PILLOW_BUILD, --build-dir PILLOW_BUILD build directory (default: 'winbuild\build') --depends PILLOW_DEPS directory used to store cached dependencies (default: 'winbuild\depends') --architecture {x86,AMD64,ARM64} build architecture (default: same as host Python) --nmake build dependencies using NMake instead of Ninja --no-imagequant skip GPL-licensed optional dependency libimagequant --no-fribidi, --no-raqm skip LGPL-licensed optional dependency FriBiDi --no-avif skip optional dependency libavif ``` -------------------------------- ### Install Debug Symbols for Libraries Source: https://github.com/python-pillow/pillow/blob/main/docs/reference/c_extension_debugging.rst On Ubuntu, install debug symbols for libraries like libtiff5 to aid in debugging. ```bash sudo apt-get install libtiff5-dbgsym ``` -------------------------------- ### Install libavif Codec Dependencies on macOS Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs specific codec dependencies for libavif on macOS if a more advanced build is desired. ```bash brew install aom dav1d rav1e svt-av1 ``` -------------------------------- ### Build Raqm Documentation Source: https://github.com/python-pillow/pillow/blob/main/src/thirdparty/raqm/README.md Builds the documentation for the Raqm library by passing the '-Ddocs=true' flag to the Meson build system. ```bash To build the documentation, pass -Ddocs=true to the `meson`. ``` -------------------------------- ### Install Pillow with pip Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/basic-installation.rst Installs or upgrades pip and Pillow using the pip package manager. This is the primary method for most users. ```bash python3 -m pip install --upgrade pip python3 -m pip install --upgrade Pillow ``` -------------------------------- ### Image Resizing with ImageOps and thumbnail Source: https://github.com/python-pillow/pillow/blob/main/docs/handbook/tutorial.rst Demonstrates resizing images using ImageOps.contain, ImageOps.cover, ImageOps.fit, ImageOps.pad, and the in-place im.thumbnail method. These methods allow resizing relative to a given size. ```python from PIL import Image, ImageOps size = (100, 150) with Image.open("hopper.webp") as im: ImageOps.contain(im, size).save("imageops_contain.webp") ImageOps.cover(im, size).save("imageops_cover.webp") ImageOps.fit(im, size).save("imageops_fit.webp") ImageOps.pad(im, size, color="#f00").save("imageops_pad.webp") # thumbnail() can also be used, # but will modify the image object in place im.thumbnail(size) im.save("image_thumbnail.webp") ``` -------------------------------- ### Install Pillow in Editable Mode Source: https://github.com/python-pillow/pillow/blob/main/winbuild/build.rst Installs Pillow in editable mode using pip after setting up the build environment. This is useful for development. ```bash winbuild\build\build_env.cmd python.exe -m pip install -v -C raqm=vendor -C fribidi=vendor -e . ``` -------------------------------- ### Install Pillow with test dependencies on macOS Source: https://github.com/python-pillow/pillow/blob/main/docs/releasenotes/9.1.0.rst On macOS, you may need to wrap the extras in quotes when installing Pillow with test dependencies. ```bash python3 -m pip install ".[tests]" ``` -------------------------------- ### buildProofTransformFromOpenProfiles Function Source: https://github.com/python-pillow/pillow/blob/main/docs/reference/ImageCms.rst Builds a proofing color transformation from open profiles. ```APIDOC def PIL.ImageCms.buildProofTransformFromOpenProfiles(source_profile, destination_profile, proof_intent): """Builds a proofing color transformation from open profiles.""" pass ``` -------------------------------- ### Build a Binary Wheel Source: https://github.com/python-pillow/pillow/blob/main/winbuild/build.rst Builds a binary wheel distribution of Pillow using pip. Ensure the build environment is set up first. ```bash winbuild\build\build_env.cmd python.exe -m pip wheel -v -C raqm=vendor -C fribidi=vendor . ``` -------------------------------- ### Import PIL._imaging Core Module Source: https://github.com/python-pillow/pillow/blob/main/docs/porting.rst Import the core imaging module from the PIL.Image namespace, as its location has changed from PIL._imaging. ```python from PIL.Image import core as _imaging ``` -------------------------------- ### Use WalImageFile Loader for Quake 2 Textures Source: https://github.com/python-pillow/pillow/blob/main/CHANGES.rst Import WalImageFile and call its 'open' method to load Quake 2 texture files (WAL format). ```python # To use this loader, import WalImageFile and call the "open" method in that # module. ``` -------------------------------- ### Importing and Opening a Custom Image Plugin Source: https://github.com/python-pillow/pillow/blob/main/docs/handbook/writing-your-own-image-plugin.rst Demonstrates how to import a custom image plugin and open an image file using it. This is necessary for Pillow to recognize and use the plugin. ```python from PIL import Image import SpamImagePlugin with Image.open("hopper.spam") as im: pass ``` -------------------------------- ### Install Dependencies in Termux for Android Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Installs Python, NDK, Clang, Make, and libjpeg-turbo for building Pillow within the Termux environment on Android. ```bash pkg install -y python ndk-sysroot clang make \ libjpeg-turbo ``` -------------------------------- ### Enable a Feature Build Source: https://github.com/python-pillow/pillow/blob/main/docs/installation/building-from-source.rst Requires a specific feature to be built. The build will fail if the necessary libraries are not found. Example shown for zlib. ```bash python3 -m pip install . -C zlib=enable ``` -------------------------------- ### Open, Rotate, and Display Image Source: https://github.com/python-pillow/pillow/blob/main/docs/reference/Image.rst Opens an image file, rotates it by 45 degrees, and displays it using the default system viewer. Ensure the image file exists in the specified path. ```python from PIL import Image with Image.open("hopper.jpg") as im: im.rotate(45).show() ``` -------------------------------- ### Getting IFD Tag Name by Value Source: https://github.com/python-pillow/pillow/blob/main/docs/reference/ExifTags.rst Shows how to import the IFD enum and get the string name of an IFD tag from its integer value. ```python from PIL.ExifTags import IFD >>> IFD(34665).name 'Exif' ``` -------------------------------- ### Image Resizing and Manipulation with ImageOps Source: https://github.com/python-pillow/pillow/blob/main/docs/reference/ImageOps.rst Demonstrates various image resizing and padding techniques using ImageOps functions like contain, cover, fit, and pad. Also shows the in-place thumbnail method. ```Python from PIL import Image, ImageOps size = (100, 150) with Image.open("Tests/images/hopper.webp") as im: ImageOps.contain(im, size).save("imageops_contain.webp") ImageOps.cover(im, size).save("imageops_cover.webp") ImageOps.fit(im, size).save("imageops_fit.webp") ImageOps.pad(im, size, color="#f00").save("imageops_pad.webp") # thumbnail() can also be used, # but will modify the image object in place im.thumbnail(size) im.save("image_thumbnail.webp") ``` -------------------------------- ### Python Decoder Example (DdsImagePlugin) Source: https://github.com/python-pillow/pillow/blob/main/docs/handbook/writing-your-own-image-plugin.rst An example of a Python-based decoder class for DDS images. It demonstrates subclassing PyDecoder and implementing the decode method. ```python class DdsDecoder(ImageFile.PyDecoder): def decode(self, buffer): # ... decode DDS data ... self.set_as_raw(raw_data, size, mode) return b"" ``` -------------------------------- ### Run Raqm Tests Source: https://github.com/python-pillow/pillow/blob/main/src/thirdparty/raqm/README.md Executes the test suite for the Raqm library using the Ninja build system. ```bash $ ninja -C build test ``` -------------------------------- ### ImageDraw.arc(xy, start, end, fill=None, width=0) Source: https://github.com/python-pillow/pillow/blob/main/docs/reference/ImageDraw.rst Draws an arc, which is a portion of a circle's outline, within a specified bounding box between given start and end angles. ```APIDOC ## ImageDraw.arc(xy, start, end, fill=None, width=0) ### Description Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box. ### Parameters * **xy**: Two points to define the bounding box. Sequence of ``[(x0, y0), (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and ``y1 >= y0``. * **start**: Starting angle, in degrees. Angles are measured from 3 o'clock, increasing clockwise. * **end**: Ending angle, in degrees. * **fill**: Color to use for the arc. * **width**: The line width, in pixels. (Added in version 5.3.0) ```