![]() |
VOOZH | about |
In this article, we will learn to crop an image circularly using a pillow library. Cropping an image circularly means selecting a circular region inside an image and removing everything outside the circle.
Approach:
Let's take this initial image :
👁 ImageStep 1: Import the module and read the image.
Output:
👁 ImageStep 2: Create an image.
We will use pieslice() function to get the circular part of the image in white, then we will superimpose the original image and the luminous image.
ImageDraw.Draw.pieslice() Same as arc, but also draws straight lines between the endpoints and the center of the bounding box.
Syntax: PIL.ImageDraw.Draw.pieslice(xy, start, end, fill=None, outline=None)
Parameters:
xy – Four points to define the bounding box. Sequence of [(x0, y0), (x1, y1)] or [x0, y0, x1, y1].
start – Starting angle, in degrees. Angles are measured from 3 o’clock, increasing clockwise.
end – Ending angle, in degrees.
fill – Color to use for the fill.
outline – Color to use for the outline.Returns: An Image object in pieslice shape.
Code:
Output:
Step 3: Stack these two arrays together to crop out only the circular middle part.
Output:
👁 ImageBelow is the full implementation: