Python Pillow – Environment Setup


Python Pillow – Environment Setup



”;


To set up the environment for Pillow, it is recommended to use the pip package manager, as it simplifies downloading and installing Python packages. In this chapter, we will cover how to install the Pillow package on your computer.

Installing Pillow using pip

To install pillow using pip, just run the below command in your command prompt −


python -m pip install pip
python -m pip install pillow

In case, if pip and pillow are already installed in your computer, above commands will simply mention the requirement already satisfied as shown below −


Requirement

How to Install Pillow on MacOS?

Installing Pillow in a Python environment on macOS is straightforward and can be done using package management tools like pip or conda.

Follow the below steps to set up Pillow on macOS −

Open Terminal: We can access the Terminal application on macOS from the Applications folder under Utilities.

Check Python Version: It”s a good practice to ensure we have Python installed. To check our Python version to run the following command −


python --version

Install ”pip” (if not already installed): Most recent versions of Python come with pip pre-installed. To check if pip is installed we can run −


pip --version

If it”s not installed, we can install it by downloading the get-pip.py script and running it with Python. Download the script using curl


curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Then run the following script.


python get-pip.py

Install Pillow: And finally install Pillow using pip by simply run the following command.


pip install Pillow

This command will download and install Pillow and its dependencies.

Now we have successfully set up the Pillow library in our macOS Python environment. We can now start using Pillow for image processing and manipulation tasks.


How to Install Pillow on Windows?

To install the Python Imaging Library (PIL) or its more up-to-date fork on Windows we can follow these steps −

Install Python: If we haven”t already installed Python on our Windows system we can do so by following these steps −

  • Download the latest Python installer for Windows from the official Python website (https://www.python.org/downloads/windows/).

  • Run the installer and during installation and make sure to check the option Add Python X.Y to PATH (replace X.Y with the Python version we are installing).

Verify Python Installation: Open a command prompt and check that Python has been installed successfully by running the below code −


python --version

Install Pillow (Recommended): Pillow is the modern fork of PIL and is more actively maintained. To install Pillow open a command prompt and run the following command −


pip install Pillow

This command will download and install Pillow and its dependencies on our Windows system.

How to Install Pillow on Linux?

Setting up the Pillow library in a Python environment on a Linux-based operating system is similar to the process on macOS. Here are the steps for setting up the environment for Pillow on Linux.

Check Python Installation: Most Linux distributions come with Python pre-installed. To check if Python is installed first open the terminal and run the following code.


python --version

Make sure we have Python 3.x installed as Python 2 is no longer supported.

Install ”pip” (if not already installed): Most recent versions of Python come with pip pre-installed. To check if pip is installed, run the following code.


pip --version

If it”s not installed we can install it using the package manager specific to our Linux distribution. For example on Debian/Ubuntu-based systems run the following commands based on the OS.


sudo apt-get install python3-pip

On Red Hat/CentOS-based systems


sudo yum install python3-pip

Install Pillow: To install Pillow using pip and run the below command −


pip install Pillow

This command will download and install Pillow and its dependencies.

Verifying Installation

To verify that Pillow has been successfully installed in our system, we can create a Python script and import the library −

Example


from PIL import Image

#Create an Image object
img = Image.new(''RGB'', (100, 100), color=''blue'')

#Display the image
img.show()

Output


blue color

Save the script with a .py extension and execute it using Python. If Pillow is installed correctly an image window displaying a red square should appear.

Virtual Environment (Optional but Recommended)

It”s a good practice to create a virtual environment for our Python projects to keep dependencies isolated. We can create a virtual environment using the following command −


python -m venv myenv

Replace myenv with the name we want for our virtual environment. To activate the virtual environment run the below code −


source myenv/bin/activate

Now we can use our virtual environment to work on Python projects that require Pillow.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *