Drawing a line on an image with PIL

Drawing a line on an image with PIL

You can draw a line on an image using the Python Imaging Library (PIL), which is now known as the Pillow library. Here's how you can do it:

  1. Make sure you have Pillow installed. You can install it using pip if you haven't already:

    pip install Pillow 
  2. Create a Python script to draw a line on an image:

    from PIL import Image, ImageDraw # Open an image file image = Image.open('your_image.jpg') # Replace 'your_image.jpg' with your image file # Create a drawing object draw = ImageDraw.Draw(image) # Define the starting and ending points of the line start_point = (100, 100) # (x, y) end_point = (200, 200) # (x, y) # Define the line color (R, G, B) line_color = (255, 0, 0) # Red # Draw the line on the image draw.line([start_point, end_point], fill=line_color, width=2) # Save the modified image image.save('output_image.jpg') # Save the image with the line drawn # Display the image (optional) image.show() 

    In this code:

    • We open an image file using Image.open() from the Pillow library.

    • We create a drawing object using ImageDraw.Draw().

    • We define the starting and ending points of the line as tuples (x, y).

    • We define the line color as an RGB tuple (e.g., (255, 0, 0) for red).

    • We draw the line on the image using draw.line().

    • We save the modified image using image.save().

    • Optionally, we display the modified image using image.show().

  3. Replace 'your_image.jpg' with the path to your image file and customize the line color, starting point, ending point, and line width as needed.

  4. Run the Python script, and it will create a new image with the line drawn on it.

This code demonstrates how to draw a line on an image using Pillow (PIL). You can customize it further to draw lines with different colors, styles, and positions as needed.

Examples

  1. Drawing a straight line on an image using PIL in Python: Description: Learn how to draw a straight line on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) line_coordinates = [(50, 50), (200, 200)] # Draw the line on the image draw.line(line_coordinates, fill="red", width=2) # Save or display the modified image image.show() 
  2. Drawing a horizontal line on an image with PIL in Python: Description: Learn how to draw a horizontal line on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) x1, y = 50, 100 x2, _ = 200, 100 line_coordinates = [(x1, y), (x2, y)] # Draw the line on the image draw.line(line_coordinates, fill="blue", width=2) # Save or display the modified image image.show() 
  3. Drawing a vertical line on an image using PIL in Python: Description: Learn how to draw a vertical line on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) x, y1 = 100, 50 _, y2 = 100, 200 line_coordinates = [(x, y1), (x, y2)] # Draw the line on the image draw.line(line_coordinates, fill="green", width=2) # Save or display the modified image image.show() 
  4. Drawing a dashed line on an image using PIL in Python: Description: Learn how to draw a dashed line on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) line_coordinates = [(50, 50), (200, 200)] # Draw the dashed line on the image draw.line(line_coordinates, fill="red", width=2, joint="curve") # Save or display the modified image image.show() 
  5. Drawing a line with custom thickness on an image using PIL in Python: Description: Learn how to draw a line with custom thickness on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) line_coordinates = [(50, 50), (200, 200)] # Draw the line on the image with custom thickness draw.line(line_coordinates, fill="red", width=4) # Save or display the modified image image.show() 
  6. Drawing a line with arrowheads on an image using PIL in Python: Description: Learn how to draw a line with arrowheads on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) line_coordinates = [(50, 50), (200, 200)] # Draw the line with arrowheads on the image draw.line(line_coordinates, fill="red", width=2, arrow="last") # Save or display the modified image image.show() 
  7. Drawing multiple lines on an image using PIL in Python: Description: Learn how to draw multiple lines on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates for each line (start and end points) lines = [[(50, 50), (200, 200)], [(100, 100), (300, 300)], [(150, 150), (250, 250)]] # Draw the lines on the image for line in lines: draw.line(line, fill="blue", width=2) # Save or display the modified image image.show() 
  8. Drawing a line with a specific color on an image using PIL in Python: Description: Learn how to draw a line with a specific color on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) line_coordinates = [(50, 50), (200, 200)] # Draw the line with a specific color (e.g., cyan) on the image draw.line(line_coordinates, fill=(0, 255, 255), width=2) # Save or display the modified image image.show() 
  9. Drawing a line with a transparent color on an image using PIL in Python: Description: Learn how to draw a line with a transparent color on an image using the Python Imaging Library (PIL).

    from PIL import Image, ImageDraw # Open the image image = Image.open("input_image.jpg") # Initialize ImageDraw object draw = ImageDraw.Draw(image) # Define line coordinates (start and end points) line_coordinates = [(50, 50), (200, 200)] # Draw the line with a transparent color on the image draw.line(line_coordinates, fill=(0, 0, 0, 128), width=2) # Save or display the modified image image.show() 

More Tags

roguelike reveal.js feature-extraction android-radiobutton google-maps-api-3 parking android-livedata exchange-server chart.js resttemplate

More Python Questions

More Cat Calculators

More Pregnancy Calculators

More Bio laboratory Calculators

More Biology Calculators