Skip to content
Advertisement

Draw circle with PIL (Old ones doesn’t works)

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:

youtube = Image.open(f"cache/thumb{videoid}.png")
image1 = changeImageSize(1280, 720, youtube)
image2 = image1.convert("RGBA")
background = image2.filter(filter=ImageFilter.BoxBlur(30))
enhancer = ImageEnhance.Brightness(background)
background = enhancer.enhance(0.6)
Xcenter = youtube.width / 2
Ycenter = youtube.height / 2
x1 = Xcenter - 250
y1 = Ycenter - 250
x2 = Xcenter + 250
y2 = Ycenter + 250
logo = youtube.crop((x1, y1, x2, y2))
logo.thumbnail((520, 520), Image.ANTIALIAS)
logo = ImageOps.expand(logo, border=15, fill="pink")
background.paste(logo, (50, 100))
draw = ImageDraw.Draw(background)

My whole code:

Code

Advertisement

Answer

You can use either ImageDraw.arc() or ImageDraw.ellipse.

from PIL import Image, ImageDraw

# Image size
W, H = 100, 100

# Bounding box points
X0 = int(W / 4)
X1 = int(X0 * 3)
Y0 = int(H / 4)
Y1 = int(X0 * 3)

# Bounding box
bbox = [X0, Y0, X1, Y1]

# Set up
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)

# Draw a circle
draw.arc(bbox, 0, 360)

# Show the image
im.show()

a white circle on a black background

Or:

# Draw a circle
draw.ellipse(bbox)

another white circle on a black background

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement