Python Image is a library that allows users to manipulate and edit images in various ways. It provides access to a wide range of image processing capabilities, such as resizing, cropping, filtering, and more. The library is commonly used for image processing in machine learning and computer vision applications.
Example 1: Resizing an Image The following example shows how to resize an image using Python Image:
from PIL import Image
# Open an image file img = Image.open("example.jpg")
# Resize the image resized_img = img.resize((400, 300))
# Save the resized image resized_img.save("resized_example.jpg")
In this example, we use the open() function to load an image file, and the resize() method to resize the image. Finally, we save the resized image using the save() method.
Package Library: Pillow (Python Imaging Library)
Example 2: Adding Text to an Image The following example shows how to add text to an image using Python Image:
from PIL import Image from PIL import ImageDraw from PIL import ImageFont
# Open an image file img = Image.open("example.jpg")
# Create a drawing context draw = ImageDraw.Draw(img)
# Define the font and size font = ImageFont.truetype("arial.ttf", 36)
# Draw the text on the image draw.text((50, 50), "Hello, World!", font=font, fill=(255, 255, 255))
# Save the image with text img.save("text_example.jpg")
In this example, we use the ImageDraw module to create a drawing context, and the ImageFont module to define a font and size. We then use the text() method to draw the text onto the image, and the save() method to save the image.
Package Library: Pillow (Python Imaging Library)
Python Image - 60 examples found. These are the top rated real world Python examples of Image from package shap extracted from open source projects. You can rate examples to help us improve the quality of examples.