How to correctly install the Pillow library and configure environment variables
The Pillow library is a very powerful image processing library in Python. It provides a wealth of functions, such as image processing Open, save, crop, rotate, scale and more. The installation and configuration of the Pillow library is relatively simple. This article will introduce how to correctly install the Pillow library and configure environment variables.
1. Install the Pillow library
Use pip to install: Open the command line tool and enter the following command:
pip install pillow
This will automatically install from the Python software Download and install the Pillow library from the warehouse. If the network environment is not good, you can use domestic mirror sources for installation.
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow
Install using conda: If you use Anaconda as your Python environment management tool, you can use the following command to install the Pillow library:
conda install pillow
This will install it from Anaconda’s software repository Download and install the Pillow library.
2. Configure environment variables
The purpose of configuring environment variables is to enable the operating system to correctly find the Pillow library. The following describes the environment variable configuration methods for Windows and Mac operating systems respectively.
Configure Windows environment variables:
a. Use Explorer to open the Control Panel.
b. Click "System and Security".
c. Click "System".
d. In the system window, click "Advanced System Settings" on the left.
e. In the pop-up "System Properties" window, click the "Advanced" tab.
f. Click the "Environment Variables" button.
g. In "User Variables" or "System Variables", find the variable named "Path" and double-click to open it.
h. Click "New" and add the installation path of the Pillow library, such as "C:Python36Libsite-packagesPIL".
i. Click "OK" to close all windows.
Configure Mac environment variables:
a. Open the terminal.
b. Enter the following command to open the environment variable configuration file:
nano ~/.bash_profile
c. In the opened file, add the following code, save and exit:
export PYTHONPATH="${PYTHONPATH}:/Library/Python/3.6/site-packages/PIL"
d. Enter the following command to make the environment variables take effect immediately:
source ~/.bash_profile
At this point, the installation and configuration of the Pillow library has been completed. Now you can use the functionality of the Pillow library in Python scripts.
The following is a simple code example that demonstrates how to use the Pillow library to open an image, crop it to a specified size, and save it as a new image:
from PIL import Image # 打开原始图片 image = Image.open("original.jpg") # 裁剪图片 cropped_image = image.crop((100, 100, 300, 300)) # 保存裁剪后的图片 cropped_image.save("cropped.jpg")
The above code will open the name For the image file "original.jpg", crop out the part with the upper left corner coordinates (100, 100) and the lower right corner coordinates (300, 300), and save the cropped image as "cropped.jpg".
I hope this article can help you install and configure the Pillow library correctly, and use its functions smoothly. I wish you good results when using the Pillow library to process images!
The above is the detailed content of Pillow library installation and environment variable configuration guide. For more information, please follow other related articles on the PHP Chinese website!