The Wayback Machine - https://web.archive.org/web/20250114233733/https://www.geeksforgeeks.org/python-pil-imagedraw-draw-line/
Open In App

Python PIL | ImageDraw.Draw.line()

Last Updated : 02 Aug, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageDraw module provide simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.

ImageDraw.Draw.line() Draws a line between the coordinates in the xy list.

Syntax: PIL.ImageDraw.Draw.line(xy, fill=None, width=0)

Parameters:
xy – Sequence of either 2-tuples like [(x, y), (x, y), …] or numeric values like [x, y, x, y, …].
fill – Color to use for the line.
width –The line width, in pixels. Note that line joins are not handled well, so wide polylines will not look good.

Returns: An Image object in ellipse shape.




   
  
# importing image object from PIL
import math
from PIL import Image, ImageDraw
  
w, h = 220, 190
shape = [(40, 40), (w - 10, h - 10)]
  
# creating new Image object
img = Image.new("RGB", (w, h))
  
# create line image
img1 = ImageDraw.Draw(img)  
img1.line(shape, fill ="none", width = 0)
img.show()



Another Example: Here we use different colour for filling.




   
  
# importing image object from PIL
import math
from PIL import Image, ImageDraw
  
w, h = 220, 190
shape = [(40, 40), (w - 10, h - 10)]
  
# creating new Image object
img = Image.new("RGB", (w, h))
  
# create line image
img1 = ImageDraw.Draw(img)  
img1.line(shape, fill ="red", width = 0)
img.show()


Output:


Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg