Using Pillow module to draw figures with Python
Tags: python, programming, beginners
Published: 2022-11-10 08:57:55

## 1. How to draw a line

```python
from PIL import Image, ImageDraw
  
im = Image.new("RGB", (400, 300))  
dr = ImageDraw.Draw(im)
dr.line([(50,200), (350,200)], fill ="white", width = 5)
im.show()
```

- `ImageDraw.Draw` - create drawing object,
- `.line(` - draws a line,
- `(50,200), (350,200)` - starting and ending coordinates of a line.

[Open original](https://onelinerhub.com/python-pillow/how-to-draw-a-line) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-pillow/how-to-draw-a-line.md).

## 2. How to draw a point

```python
from PIL import Image, ImageDraw
  
im = Image.new("RGB", (400, 300))  
dr = ImageDraw.Draw(im)
dr.point((50,50), fill="white")
im.show()
```

- `.point(` - draws a single point,
- `(50,50)` - point coordinates.

[Open original](https://onelinerhub.com/python-pillow/how-to-draw-a-point) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-pillow/how-to-draw-a-point.md).

## 3. How to draw a triangle

```python
from PIL import Image, ImageDraw
  
im = Image.new("RGB", (400, 300))  
dr = ImageDraw.Draw(im)
dr.polygon([(50,50), (350,150), (200, 250)], outline="white")
im.show()
```

- `.polygon(` - draws polygon from given coordinates (with any number of dots),
- `(50,50), (350,150), (200, 250)` - triangle dots coordinates.

[Open original](https://onelinerhub.com/python-pillow/how-to-draw-a-triangle) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-pillow/how-to-draw-a-triangle.md).

## 4. How to draw an arc

```python
from PIL import Image, ImageDraw
  
im = Image.new("RGB", (400, 300))  
dr = ImageDraw.Draw(im)
dr.arc([(50,50), (350,250)], start = 20, end = 230, fill ="white")
im.show()
```

- `.arc(` - draws an arc,
- `(50,50), (350,250)` - coordinates of an arc bounding rectangle,
- `start = 20` - starting angle (starts from 3pm),
- `end = 230` - ending angle.

[Open original](https://onelinerhub.com/python-pillow/how-to-draw-an-arc) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-pillow/how-to-draw-an-arc.md).

## 5. How to draw an ellipse

```python
from PIL import Image, ImageDraw
  
im = Image.new("RGB", (400, 300))  
dr = ImageDraw.Draw(im)
dr.ellipse([(50,50), (350,250)], outline ="white")
im.show()
```

- `.ellipse(` - draws an ellipse,
- `(50,50), (350,250)` - bounding rectangle to fit ellipse into.

[Open original](https://onelinerhub.com/python-pillow/how-to-draw-ellipse) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-pillow/how-to-draw-ellipse.md).

## 6. How to draw rectangle

```python
from PIL import Image, ImageDraw
  
im = Image.new("RGB", (400, 300))  
dr = ImageDraw.Draw(im)
dr.rectangle([(50,50), (350,250)], outline="white")
im.show()
```

- `.rectangle(` - draws rectangle,
- `(50,50), (350,250)` - rectangle top left and bottom right points coordinates.

[Open original](https://onelinerhub.com/python-pillow/how-to-draw-rectangle) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-pillow/how-to-draw-rectangle.md).