I am trying to draw a circle with PIL, but I get an attribute error.
Current code is for square.
Part of code to draw:
JavaScript
x
18
18
1
youtube = Image.open(f"cache/thumb{videoid}.png")
2
image1 = changeImageSize(1280, 720, youtube)
3
image2 = image1.convert("RGBA")
4
background = image2.filter(filter=ImageFilter.BoxBlur(30))
5
enhancer = ImageEnhance.Brightness(background)
6
background = enhancer.enhance(0.6)
7
Xcenter = youtube.width / 2
8
Ycenter = youtube.height / 2
9
x1 = Xcenter - 250
10
y1 = Ycenter - 250
11
x2 = Xcenter + 250
12
y2 = Ycenter + 250
13
logo = youtube.crop((x1, y1, x2, y2))
14
logo.thumbnail((520, 520), Image.ANTIALIAS)
15
logo = ImageOps.expand(logo, border=15, fill="pink")
16
background.paste(logo, (50, 100))
17
draw = ImageDraw.Draw(background)
18
My whole code:
Advertisement
Answer
You can use either ImageDraw.arc()
or ImageDraw.ellipse
.
JavaScript
1
24
24
1
from PIL import Image, ImageDraw
2
3
# Image size
4
W, H = 100, 100
5
6
# Bounding box points
7
X0 = int(W / 4)
8
X1 = int(X0 * 3)
9
Y0 = int(H / 4)
10
Y1 = int(X0 * 3)
11
12
# Bounding box
13
bbox = [X0, Y0, X1, Y1]
14
15
# Set up
16
im = Image.new("RGB", (W, H))
17
draw = ImageDraw.Draw(im)
18
19
# Draw a circle
20
draw.arc(bbox, 0, 360)
21
22
# Show the image
23
im.show()
24
Or:
JavaScript
1
3
1
# Draw a circle
2
draw.ellipse(bbox)
3