Разработка веб-приложений на языке Python с использованием PIL
PIL (Python Imaging Library)
PIL is a library for working with images in the Python programming language. It provides a wide range of functionality for creating, modifying, and processing various types of images. PIL allows you to load and save images in different formats, manipulate their sizes, apply effects and filters, as well as perform other operations related to graphic processing.
1. Installation:
To start working with PIL, you need to install the library using the pip package manager:
pip install pillow
2. Loading and displaying an image:
To load an image, use the following code:
from PIL import Image
# Load the image
image = Image.open("image.jpg")
# Display the image
image.show()
3. Resizing an image:
To resize an image, use the following code:
from PIL import Image
# Load the image
image = Image.open("image.jpg")
# Resize the image
resized_image = image.resize((800, 600))
# Save the resized image
resized_image.save("resized_image.jpg")
4. Applying a filter to an image:
To apply a filter to an image, use the following code:
from PIL import Image, ImageFilter
# Load the image
image = Image.open("image.jpg")
# Apply a filter
filtered_image = image.filter(ImageFilter.BLUR)
# Save the processed image
filtered_image.save("filtered_image.jpg")
5. Creating a new image:
To create a new image, use the following code:
from PIL import Image, ImageDraw
# Create a new black image
new_image = Image.new("RGB", (800, 600), "black")
# Create a drawing object
draw = ImageDraw.Draw(new_image)
# Draw a rectangle on the image
draw.rectangle([(100, 100), (700, 500)], outline="white")
# Save the new image
new_image.save("new_image.jpg")
These are just some examples of using the PIL library. It has many other functions, such as adding text, rotating an image, changing color space, etc. PIL is a powerful tool for working with images in Python, allowing you to implement a wide range of graphic operations.