Pillow for Artistic Image Transformations

Pillow for Artistic Image Transformations

Pillow is a powerful Python Imaging Library that provides extensive capabilities for image processing and manipulation. It serves as a fork of the original PIL (Python Imaging Library) and has been actively maintained, making it perfect for developers looking to perform various artistic transformations on images.

With its simple API, Pillow allows for easy integration into projects, enabling users to apply filters, modify colors, and create visually appealing graphics. Below are some of the key features and capabilities that Pillow offers:

  • Pillow provides tools to open, manipulate, and save images in various formats including JPEG, PNG, BMP, GIF, and TIFF.
  • The library includes a range of built-in image filters that can be applied to enhance or alter the appearance of images. Some common filters include blur, sharpen, and edge enhancement.
  • Users can manipulate shapes and colors with ease, allowing for adjustments in brightness, contrast, saturation, and hue.
  • Pillow enables the creation of custom graphics through its drawing API, allowing developers to add text, lines, shapes, and other visual elements to images.
  • The library supports not just standard image formats but also advanced features like transparency and animation in GIFs.

Pillow’s intuitive method calls allow users to perform complex image transformations with a minimal amount of code. Below is a simple example of how to open an image and apply a filter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image, ImageFilter
# Open an image file
image = Image.open('example.jpg')
# Apply a Gaussian Blur filter
blurred_image = image.filter(ImageFilter.GaussianBlur(5))
# Save the modified image
blurred_image.save('blurred_example.jpg')
from PIL import Image, ImageFilter # Open an image file image = Image.open('example.jpg') # Apply a Gaussian Blur filter blurred_image = image.filter(ImageFilter.GaussianBlur(5)) # Save the modified image blurred_image.save('blurred_example.jpg')
from PIL import Image, ImageFilter

# Open an image file
image = Image.open('example.jpg')

# Apply a Gaussian Blur filter
blurred_image = image.filter(ImageFilter.GaussianBlur(5))

# Save the modified image
blurred_image.save('blurred_example.jpg')

Overall, Pillow is a versatile and crucial tool for artists and developers alike, enabling them to create stunning visual content and push the boundaries of image transformations in their projects.

Installing Pillow for Image Manipulation

To get started with Pillow, the first step is to install the library. There are several ways to do this depending on your development environment. The most common method is to use pip, Python’s package installer. Below are the instructions for installing Pillow using pip and other methods.

  • Using pip:

    Pip is the easiest way to install Pillow. Simply open your command line or terminal and enter the following command:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    pip install Pillow
    pip install Pillow
    pip install Pillow

    This command will download and install the latest version of Pillow along with any of its dependencies.

  • Using conda:

    If you’re using Anaconda as your package manager, you can install Pillow using conda by running the following command:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    conda install pillow
    conda install pillow
    conda install pillow

    This will ensure that Pillow is installed in your Anaconda environment, and it will handle dependencies automatically.

  • Verifying Installation:

    After the installation, you can verify that Pillow has been installed correctly by running a simple Python script. Open your Python interpreter or create a new script and execute the following:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    from PIL import Image
    # Check the version of Pillow
    print(Image.__version__)
    from PIL import Image # Check the version of Pillow print(Image.__version__)
    from PIL import Image
    
    # Check the version of Pillow
    print(Image.__version__)

    If the installation was successful, this will print the version number of Pillow you installed.

  • Upgrading Pillow:

    If you already have Pillow installed but want to make sure you have the latest version, you can upgrade it by running:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    pip install --upgrade Pillow
    pip install --upgrade Pillow
    pip install --upgrade Pillow

    This command will fetch the latest version and update your current installation.

By following these installation steps, you will be well on your way to using the powerful features of Pillow for your artistic image manipulations.

Essential Functions for Artistic Transformations

Pillow provides a variety of essential functions that facilitate artistic transformations of images. Understanding these functions enables users to apply effects, modifications, and artistic filters to their images with ease. Below, we explore some of the most important functions available in Pillow, along with practical code examples.

  • Pillow includes several enhancement filters that can adjust the overall appearance of images. The ImageEnhance module provides enhancements for brightness, contrast, color, and sharpness. Here’s how you can use these enhancements:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image, ImageEnhance
# Open an image file
image = Image.open('example.jpg')
# Enhance brightness
enhancer = ImageEnhance.Brightness(image)
bright_image = enhancer.enhance(1.5) # Increase brightness by 50%
bright_image.save('bright_example.jpg')
# Enhance contrast
enhancer = ImageEnhance.Contrast(image)
contrast_image = enhancer.enhance(2.0) # Double the contrast
contrast_image.save('contrast_example.jpg')
from PIL import Image, ImageEnhance # Open an image file image = Image.open('example.jpg') # Enhance brightness enhancer = ImageEnhance.Brightness(image) bright_image = enhancer.enhance(1.5) # Increase brightness by 50% bright_image.save('bright_example.jpg') # Enhance contrast enhancer = ImageEnhance.Contrast(image) contrast_image = enhancer.enhance(2.0) # Double the contrast contrast_image.save('contrast_example.jpg')
from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('example.jpg')

# Enhance brightness
enhancer = ImageEnhance.Brightness(image)
bright_image = enhancer.enhance(1.5)  # Increase brightness by 50%
bright_image.save('bright_example.jpg')

# Enhance contrast
enhancer = ImageEnhance.Contrast(image)
contrast_image = enhancer.enhance(2.0)  # Double the contrast
contrast_image.save('contrast_example.jpg')
  • Filters can be applied to images to create different artistic effects. The ImageFilter module contains several built-in filters like blur, edge detection, and others:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image, ImageFilter
# Open an image file
image = Image.open('example.jpg')
# Apply an edge enhancement filter
edge_enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE())
edge_enhanced_image.save('edge_enhanced_example.jpg')
from PIL import Image, ImageFilter # Open an image file image = Image.open('example.jpg') # Apply an edge enhancement filter edge_enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE()) edge_enhanced_image.save('edge_enhanced_example.jpg')
from PIL import Image, ImageFilter

# Open an image file
image = Image.open('example.jpg')

# Apply an edge enhancement filter
edge_enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE())
edge_enhanced_image.save('edge_enhanced_example.jpg')
  • Pillow allows for direct color manipulation using the convert method, which can transform images to different color modes, such as RGB, CMYK, or grayscale:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Open an image file
image = Image.open('example.jpg')
# Convert image to grayscale
gray_image = image.convert('L')
gray_image.save('gray_example.jpg')
# Open an image file image = Image.open('example.jpg') # Convert image to grayscale gray_image = image.convert('L') gray_image.save('gray_example.jpg')
# Open an image file
image = Image.open('example.jpg')

# Convert image to grayscale
gray_image = image.convert('L')
gray_image.save('gray_example.jpg')
  • Using the ImageDraw module, you can add shapes and text to images. This allows artists to create custom graphics directly on their images:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import ImageDraw, ImageFont
# Create a blank image
canvas = Image.new('RGB', (400, 200), 'white')
draw = ImageDraw.Draw(canvas)
# Draw a rectangle
draw.rectangle([50, 50, 350, 150], fill='blue', outline='black')
# Add text
font = ImageFont.load_default()
draw.text((60, 90), 'Hello, Pillow!', fill='white', font=font)
canvas.save('drawn_example.jpg')
from PIL import ImageDraw, ImageFont # Create a blank image canvas = Image.new('RGB', (400, 200), 'white') draw = ImageDraw.Draw(canvas) # Draw a rectangle draw.rectangle([50, 50, 350, 150], fill='blue', outline='black') # Add text font = ImageFont.load_default() draw.text((60, 90), 'Hello, Pillow!', fill='white', font=font) canvas.save('drawn_example.jpg')
from PIL import ImageDraw, ImageFont

# Create a blank image
canvas = Image.new('RGB', (400, 200), 'white')
draw = ImageDraw.Draw(canvas)

# Draw a rectangle
draw.rectangle([50, 50, 350, 150], fill='blue', outline='black')

# Add text
font = ImageFont.load_default()
draw.text((60, 90), 'Hello, Pillow!', fill='white', font=font)

canvas.save('drawn_example.jpg')
  • Pillow provides functions for resizing and cropping images, essential for creating compositions and fitting images into specific dimensions:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Open an image file
image = Image.open('example.jpg')
# Resize the image
resized_image = image.resize((200, 100)) # Resize to 200x100 pixels
resized_image.save('resized_example.jpg')
# Crop the image
cropped_image = image.crop((100, 50, 400, 300)) # Define the box to crop
cropped_image.save('cropped_example.jpg')
# Open an image file image = Image.open('example.jpg') # Resize the image resized_image = image.resize((200, 100)) # Resize to 200x100 pixels resized_image.save('resized_example.jpg') # Crop the image cropped_image = image.crop((100, 50, 400, 300)) # Define the box to crop cropped_image.save('cropped_example.jpg')
# Open an image file
image = Image.open('example.jpg')

# Resize the image
resized_image = image.resize((200, 100))  # Resize to 200x100 pixels
resized_image.save('resized_example.jpg')

# Crop the image
cropped_image = image.crop((100, 50, 400, 300))  # Define the box to crop
cropped_image.save('cropped_example.jpg')

These essential functions enable users to perform a wide range of artistic transformations on images using Pillow. By using these functionalities, you can create unique and eye-catching visual compositions that stand out in any creative project.

Advanced Techniques for Custom Effects

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Import necessary modules
from PIL import Image, ImageFilter, ImageEnhance, ImageDraw, ImageFont
# Open an image file for transformation
image_path = 'example.jpg'
image = Image.open(image_path)
# Apply multiple filters for artistic effects
# Step 1: Apply a Gaussian Blur filter
blurred_image = image.filter(ImageFilter.GaussianBlur(5))
blurred_image.save('blurred_example.jpg')
# Step 2: Enhance the image's contrast
enhancer = ImageEnhance.Contrast(blurred_image)
contrast_image = enhancer.enhance(1.5) # 50% more contrast
contrast_image.save('contrast_blurred_example.jpg')
# Step 3: Draw a shape and text on the image
draw = ImageDraw.Draw(contrast_image)
draw.rectangle([50, 50, 350, 150], fill='blue', outline='black')
# Load default font
font = ImageFont.load_default()
draw.text((60, 90), 'Artistic Image!', fill='white', font=font)
# Save the final transformed image
contrast_image.save('final_artistic_example.jpg')
# Import necessary modules from PIL import Image, ImageFilter, ImageEnhance, ImageDraw, ImageFont # Open an image file for transformation image_path = 'example.jpg' image = Image.open(image_path) # Apply multiple filters for artistic effects # Step 1: Apply a Gaussian Blur filter blurred_image = image.filter(ImageFilter.GaussianBlur(5)) blurred_image.save('blurred_example.jpg') # Step 2: Enhance the image's contrast enhancer = ImageEnhance.Contrast(blurred_image) contrast_image = enhancer.enhance(1.5) # 50% more contrast contrast_image.save('contrast_blurred_example.jpg') # Step 3: Draw a shape and text on the image draw = ImageDraw.Draw(contrast_image) draw.rectangle([50, 50, 350, 150], fill='blue', outline='black') # Load default font font = ImageFont.load_default() draw.text((60, 90), 'Artistic Image!', fill='white', font=font) # Save the final transformed image contrast_image.save('final_artistic_example.jpg')
# Import necessary modules
from PIL import Image, ImageFilter, ImageEnhance, ImageDraw, ImageFont

# Open an image file for transformation
image_path = 'example.jpg'
image = Image.open(image_path)

# Apply multiple filters for artistic effects
# Step 1: Apply a Gaussian Blur filter
blurred_image = image.filter(ImageFilter.GaussianBlur(5))
blurred_image.save('blurred_example.jpg')

# Step 2: Enhance the image's contrast
enhancer = ImageEnhance.Contrast(blurred_image)
contrast_image = enhancer.enhance(1.5)  # 50% more contrast
contrast_image.save('contrast_blurred_example.jpg')

# Step 3: Draw a shape and text on the image
draw = ImageDraw.Draw(contrast_image)
draw.rectangle([50, 50, 350, 150], fill='blue', outline='black')

# Load default font
font = ImageFont.load_default()
draw.text((60, 90), 'Artistic Image!', fill='white', font=font)

# Save the final transformed image
contrast_image.save('final_artistic_example.jpg')

Pillow provides a variety of advanced techniques that can be used to create custom effects, allowing for even more creativity in artistic image manipulations. Below we explore several techniques that can transform images in unique ways:

  • Developers can create their own filters by manipulating pixel data directly. This allows for a high degree of customization.
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    from PIL import ImageFilter, Image
    class CustomFilter(ImageFilter.Filter):
    name = "Custom Filter"
    def __init__(self, matrix):
    self.matrix = matrix
    self.size = (3, 3) # A 3x3 filter
    def filter(self, image):
    return image.filter(self)
    # Define a sample kernel for the filter
    custom_matrix = [
    -1, -1, -1,
    -1, 9, -1,
    -1, -1, -1,
    ]
    # Apply the custom filter
    image = Image.open('example.jpg')
    custom_filtered_image = image.filter(CustomFilter(custom_matrix))
    custom_filtered_image.save('custom_filtered_example.jpg')
    from PIL import ImageFilter, Image class CustomFilter(ImageFilter.Filter): name = "Custom Filter" def __init__(self, matrix): self.matrix = matrix self.size = (3, 3) # A 3x3 filter def filter(self, image): return image.filter(self) # Define a sample kernel for the filter custom_matrix = [ -1, -1, -1, -1, 9, -1, -1, -1, -1, ] # Apply the custom filter image = Image.open('example.jpg') custom_filtered_image = image.filter(CustomFilter(custom_matrix)) custom_filtered_image.save('custom_filtered_example.jpg')
    from PIL import ImageFilter, Image
    
    class CustomFilter(ImageFilter.Filter):
        name = "Custom Filter"
        
        def __init__(self, matrix):
            self.matrix = matrix
            self.size = (3, 3)  # A 3x3 filter
    
        def filter(self, image):
            return image.filter(self)
    
    # Define a sample kernel for the filter
    custom_matrix = [
        -1, -1, -1,
        -1,  9, -1,
        -1, -1, -1,
    ]
    
    # Apply the custom filter
    image = Image.open('example.jpg')
    custom_filtered_image = image.filter(CustomFilter(custom_matrix))
    custom_filtered_image.save('custom_filtered_example.jpg')
    
  • By blending multiple images together, you can create artistic compositions that highlight the best features of each image. That is accomplished using the Image.alpha_composite method.
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    # Open two images to compose
    background_image = Image.open('background.png').convert('RGBA')
    foreground_image = Image.open('foreground.png').convert('RGBA')
    # Composite the background with the foreground image
    composed_image = Image.alpha_composite(background_image, foreground_image)
    composed_image.save('composed_example.png')
    # Open two images to compose background_image = Image.open('background.png').convert('RGBA') foreground_image = Image.open('foreground.png').convert('RGBA') # Composite the background with the foreground image composed_image = Image.alpha_composite(background_image, foreground_image) composed_image.save('composed_example.png')
    # Open two images to compose
    background_image = Image.open('background.png').convert('RGBA')
    foreground_image = Image.open('foreground.png').convert('RGBA')
    
    # Composite the background with the foreground image
    composed_image = Image.alpha_composite(background_image, foreground_image)
    composed_image.save('composed_example.png')
    
  • You can create visually striking effects by manipulating the individual color channels (R, G, B). The PixelAccess object allows direct access to pixel data.
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    # Open an image file
    image = Image.open('example.jpg')
    # Convert the image to RGB
    image = image.convert("RGB")
    # Access pixel data to manipulate colors
    pixels = image.load()
    # Loop over each pixel to modify color channels
    for i in range(image.width):
    for j in range(image.height):
    r, g, b = pixels[i, j]
    # Invert colors
    pixels[i, j] = (255 - r, 255 - g, 255 - b)
    image.save('color_inverted_example.jpg')
    # Open an image file image = Image.open('example.jpg') # Convert the image to RGB image = image.convert("RGB") # Access pixel data to manipulate colors pixels = image.load() # Loop over each pixel to modify color channels for i in range(image.width): for j in range(image.height): r, g, b = pixels[i, j] # Invert colors pixels[i, j] = (255 - r, 255 - g, 255 - b) image.save('color_inverted_example.jpg')
    # Open an image file
    image = Image.open('example.jpg')
    
    # Convert the image to RGB
    image = image.convert("RGB")
    
    # Access pixel data to manipulate colors
    pixels = image.load()
    
    # Loop over each pixel to modify color channels
    for i in range(image.width):
        for j in range(image.height):
            r, g, b = pixels[i, j]
            # Invert colors
            pixels[i, j] = (255 - r, 255 - g, 255 - b)
    
    image.save('color_inverted_example.jpg')
    
  • You can use affine transformations to rotate, scale, or skew images, adding more dynamic aspects to your transformations.
  • Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    # Open an image file
    image = Image.open('example.jpg')
    # Apply rotation and translation
    transformed_image = image.transform(image.size, Image.AFFINE, (1, 0, 50, 0, 1, 30)) # Move image by (50, 30)
    transformed_image.save('transformed_example.jpg')
    # Open an image file image = Image.open('example.jpg') # Apply rotation and translation transformed_image = image.transform(image.size, Image.AFFINE, (1, 0, 50, 0, 1, 30)) # Move image by (50, 30) transformed_image.save('transformed_example.jpg')
    # Open an image file
    image = Image.open('example.jpg')
    
    # Apply rotation and translation
    transformed_image = image.transform(image.size, Image.AFFINE, (1, 0, 50, 0, 1, 30))  # Move image by (50, 30)
    transformed_image.save('transformed_example.jpg')
    

By employing these advanced techniques, artists and developers can experiment and develop unique custom effects using Pillow, opening new avenues for creativity in image manipulation. These tools allow for a greater level of artistic expression and can bring a distinctive flair to any project.

Practical Examples: Create Stunning Artworks with Pillow

Pillow offers a wide range of practical examples that demonstrate its capabilities for creating stunning artworks. Here are some hands-on projects using Pillow that can help you understand how to leverage its features effectively:

  • You can create collages by combining multiple images into a single canvas. Below is an example of how to create a simple collage using Pillow:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image
# Create a blank canvas
collage_width = 800
collage_height = 600
collage = Image.new('RGB', (collage_width, collage_height), 'white')
# Open images
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')
image3 = Image.open('image3.jpg')
# Resize images
image1 = image1.resize((200, 200))
image2 = image2.resize((200, 200))
image3 = image3.resize((200, 200))
# Paste images onto canvas
collage.paste(image1, (50, 50))
collage.paste(image2, (300, 50))
collage.paste(image3, (550, 50))
# Save the collage
collage.save('collage_example.jpg')
from PIL import Image # Create a blank canvas collage_width = 800 collage_height = 600 collage = Image.new('RGB', (collage_width, collage_height), 'white') # Open images image1 = Image.open('image1.jpg') image2 = Image.open('image2.jpg') image3 = Image.open('image3.jpg') # Resize images image1 = image1.resize((200, 200)) image2 = image2.resize((200, 200)) image3 = image3.resize((200, 200)) # Paste images onto canvas collage.paste(image1, (50, 50)) collage.paste(image2, (300, 50)) collage.paste(image3, (550, 50)) # Save the collage collage.save('collage_example.jpg')
from PIL import Image

# Create a blank canvas
collage_width = 800
collage_height = 600
collage = Image.new('RGB', (collage_width, collage_height), 'white')

# Open images
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')
image3 = Image.open('image3.jpg')

# Resize images
image1 = image1.resize((200, 200))
image2 = image2.resize((200, 200))
image3 = image3.resize((200, 200))

# Paste images onto canvas
collage.paste(image1, (50, 50))
collage.paste(image2, (300, 50))
collage.paste(image3, (550, 50))

# Save the collage
collage.save('collage_example.jpg')
  • You can create artistic effects by applying filters like contour or emboss. Here’s an example:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image, ImageFilter
# Open an image file
image = Image.open('example.jpg')
# Apply contour filter
contour_image = image.filter(ImageFilter.CONTOUR)
contour_image.save('contour_example.jpg')
# Apply emboss filter
emboss_image = image.filter(ImageFilter.EMBOSS)
emboss_image.save('emboss_example.jpg')
from PIL import Image, ImageFilter # Open an image file image = Image.open('example.jpg') # Apply contour filter contour_image = image.filter(ImageFilter.CONTOUR) contour_image.save('contour_example.jpg') # Apply emboss filter emboss_image = image.filter(ImageFilter.EMBOSS) emboss_image.save('emboss_example.jpg')
from PIL import Image, ImageFilter

# Open an image file
image = Image.open('example.jpg')

# Apply contour filter
contour_image = image.filter(ImageFilter.CONTOUR)
contour_image.save('contour_example.jpg')

# Apply emboss filter
emboss_image = image.filter(ImageFilter.EMBOSS)
emboss_image.save('emboss_example.jpg')
  • You can simulate a watercolor effect using blurring and edge detection. Here’s how it can be done:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image, ImageFilter
# Open an image file
image = Image.open('example.jpg')
# Apply a Gaussian blur
blurred_image = image.filter(ImageFilter.GaussianBlur(5))
# Apply edge detection
watercolor_effect = blurred_image.filter(ImageFilter.FIND_EDGES)
watercolor_effect.save('watercolor_example.jpg')
from PIL import Image, ImageFilter # Open an image file image = Image.open('example.jpg') # Apply a Gaussian blur blurred_image = image.filter(ImageFilter.GaussianBlur(5)) # Apply edge detection watercolor_effect = blurred_image.filter(ImageFilter.FIND_EDGES) watercolor_effect.save('watercolor_example.jpg')
from PIL import Image, ImageFilter

# Open an image file
image = Image.open('example.jpg')

# Apply a Gaussian blur
blurred_image = image.filter(ImageFilter.GaussianBlur(5))

# Apply edge detection
watercolor_effect = blurred_image.filter(ImageFilter.FIND_EDGES)
watercolor_effect.save('watercolor_example.jpg')
  • You can add textures to images for artistic effects. Below is an example of applying a texture to an image:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image
# Open base image and texture
base_image = Image.open('base_image.jpg')
texture = Image.open('texture.png')
# Resize texture to fit the base image
texture = texture.resize(base_image.size)
# Blend the base image with the texture
blended_image = Image.blend(base_image.convert('RGBA'), texture.convert('RGBA'), alpha=0.5)
blended_image.save('textured_example.png')
from PIL import Image # Open base image and texture base_image = Image.open('base_image.jpg') texture = Image.open('texture.png') # Resize texture to fit the base image texture = texture.resize(base_image.size) # Blend the base image with the texture blended_image = Image.blend(base_image.convert('RGBA'), texture.convert('RGBA'), alpha=0.5) blended_image.save('textured_example.png')
from PIL import Image

# Open base image and texture
base_image = Image.open('base_image.jpg')
texture = Image.open('texture.png')

# Resize texture to fit the base image
texture = texture.resize(base_image.size)

# Blend the base image with the texture
blended_image = Image.blend(base_image.convert('RGBA'), texture.convert('RGBA'), alpha=0.5)
blended_image.save('textured_example.png')
  • Below is an example of how to create a cartoon effect by combining edge enhancement with color manipulation:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from PIL import Image, ImageFilter, ImageEnhance
# Open an image file
image = Image.open('example.jpg')
# Apply edge enhancement
edges = image.filter(ImageFilter.EDGE_ENHANCE())
# Enhance color
enhancer = ImageEnhance.Color(edges)
cartoon_image = enhancer.enhance(2.0) # Double the color saturation
cartoon_image.save('cartoon_example.jpg')
from PIL import Image, ImageFilter, ImageEnhance # Open an image file image = Image.open('example.jpg') # Apply edge enhancement edges = image.filter(ImageFilter.EDGE_ENHANCE()) # Enhance color enhancer = ImageEnhance.Color(edges) cartoon_image = enhancer.enhance(2.0) # Double the color saturation cartoon_image.save('cartoon_example.jpg')
from PIL import Image, ImageFilter, ImageEnhance

# Open an image file
image = Image.open('example.jpg')

# Apply edge enhancement
edges = image.filter(ImageFilter.EDGE_ENHANCE())

# Enhance color
enhancer = ImageEnhance.Color(edges)
cartoon_image = enhancer.enhance(2.0)  # Double the color saturation
cartoon_image.save('cartoon_example.jpg')

These examples demonstrate the versatility of Pillow in creating unique and artistic images. By combining various techniques and effects, you can unleash your creativity and produce visually appealing artwork. Each step demonstrates how simple commands can lead to impressive results when manipulating images with Pillow.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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