We shall extend the code in Part 1for five spokes using the following code. After which we rotate.
------
from PIL import Image, ImageDraw
import numpy as np
def create_five_spoke_image(filename="five_spokes.png"):
img_size = 500
center = (img_size // 2, img_size // 2)
radius = img_size // 4
2 img = Image.new("RGB", (img_size, img_size), "white")
draw = ImageDraw.Draw(img)
# Draw the circle
draw.ellipse((center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius), fill="black")
# Draw FIVE spokes
num_spokes = 5
for i in range(num_spokes):
angle = (2 * np.pi * i / num_spokes) # Calculate angle for each spoke
x1 = center[0]
y1 = center[1]
x2 = center[0] + int(radius * np.cos(angle))
y2 = center[1] + int(radius * np.sin(angle))
draw.line((x1, y1, x2, y2), fill="red", width=3)
img.save(filename)
print(f"Saved: {filename}")
img.show()
if __name__ == "__main__":
create_five_spoke_image()
-----------------
Here is a detailed explanation of steps:
Imports:
from PIL import Image, ImageDraw: Imports the Image and ImageDraw modules from the Pillow (PIL) library. Pillow is used for image manipulation.
import numpy as np: Imports the NumPy library, which is used for numerical operations, specifically for calculating trigonometric functions.
create_five_spoke_image(filename="five_spokes.png")
Function:
def create_five_spoke_image(filename="five_spokes.png"):
img_size = 500
center = (img_size // 2, img_size // 2)
radius = img_size // 4
This function creates an image with a black circle and five red spokes radiating from the center.
img_size = 500: Sets the size of the image to 500x500 pixels.
center = (img_size // 2, img_size // 2): Calculates the center coordinates of the image.
radius = img_size // 4: Calculates the radius of the circle, which is one-fourth of the image size.
img = Image.new("RGB", (img_size, img_size), "white"): Creates a new RGB image with a white background.
draw = ImageDraw.Draw(img): Creates a Draw object, which is used to draw shapes on the image.
Drawing the Circle:
draw.ellipse((center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius), fill="black"): Draws a black ellipse (which looks like a circle because the radius is equal in both dimensions) at the center of the image. The coordinates define the bounding box of the ellipse.
Drawing the Spokes:
num_spokes = 5: Sets the number of spokes to 5.
for i in range(num_spokes):: Loops through the desired number of spokes.
angle = (2 * np.pi * i / num_spokes): Calculates the angle for each spoke. This evenly distributes the spokes around the circle. np.pi is the mathematical constant pi.
x1 = center[0], y1 = center[1]: Sets the starting coordinates of the spoke to the center of the image.
x2 = center[0] + int(radius * np.cos(angle)), y2 = center[1] + int(radius * np.sin(angle)): Calculates the ending coordinates of the spoke using trigonometric functions (cosine and sine). This places the end of each spoke on the circumference of the circle.
draw.line((x1, y1, x2, y2), fill="red", width=3): Draws a red line (the spoke) between the starting and ending coordinates with a width of 3 pixels.
Saving and Displaying the Image:
img.save(filename): Saves the image to a file with the specified filename.
print(f"Saved: {filename}"): Prints a message indicating that the image has been saved.
img.show(): Displays the image.
if __name__ == "__main__": Block:
create_five_spoke_image(): Calls the create_five_spoke_image() function when the script is executed directly. This ensures that the image is created only when the script is run, not when it's imported as a module.
When the code is run the image of a circle with five spokes apppear as shown:
C:\Users\hoden\AppData\Local\Programs\Python\Python312\python.exe C:\Users\hoden\PycharmProjects\exploreImage\Images\CircleFiveSpokes.py
Saved: five_spokes.png
Process finished with exit code 0