Directly add code
from PIL import Image, ImageDraw, ImageTk
import tkinter as tk
from tkinter import filedialog
def convert_to_circle(image_path):
# Open the image and convert it to RGBA mode
image = Image.open(image_path).convert("RGBA")
# Create a transparent background image with the same size as the image
circle_image = Image.new('RGBA', image.size, (0, 0, 0, 0))
# Create a drawing pen
draw = ImageDraw.Draw(circle_image)
# Draw a circle
draw.ellipse((0, 0, image.size[0], image.size[1]), fill=(255, 255, 255, 255))
# Apply the original image to the mask
circle_image.paste(image, (0, 0), mask=circle_image)
# Return the circular edge image
return circle_image
def select_image():
# Open the file dialog and select an image file
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.jpeg;*.png")])
# If an image file is selected, convert it and display it in the program
if file_path:
circle_image = convert_to_circle(file_path)
circle_image.thumbnail((300, 300)) # Shrink the image to fit the display area
photo = ImageTk.PhotoImage(circle_image) # Convert the image to a PhotoImage object
image_label.configure(image=photo)
image_label.image = photo # Keep a reference to the image
image_label.circle_image = circle_image # Save the original PIL image
def save_image():
# Get the currently displayed image
circle_image = image_label.circle_image
# If there is an image, save it as a file
if circle_image:
save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
if save_path:
circle_image.save(save_path)
print("Avatar saved as:", save_path)
# Create the main window
window = tk.Tk()
window.title("Circular Avatar Converter")
window.geometry("400x400")
# Create the select button
select_button = tk.Button(window, text="Select Image", command=select_image)
select_button.pack(pady=10)
# Create the image display area
image_label = tk.Label(window)
image_label.pack()
# Create the save button
save_button = tk.Button(window, text="Save Image", command=save_image)
save_button.pack(pady=10)
# Run the main loop
window.mainloop()
First, we import the necessary libraries. The PIL library is used for image processing, and the tkinter library is used to create the GUI interface.
Next, we define a function called convert_to_circle, which is used to convert the selected image into a circular image. This function takes an image path as a parameter and returns the processed circular image.
In the convert_to_circle function, we first open the image and convert it to the RGBA mode. Then, we create a transparent background image with the same size as the original image. Next, we create a drawing pen object and use it to draw a circle, with the position and size of the circle being the same as the original image, and the fill color being white. Finally, we apply the original image to the transparent background image using the transparent background image as a mask. The resulting circular edge image is then returned.
Next, we define a function called select_image, which is used to select an image file and convert it into a circular image. In this function, we open a file dialog, select an image file, and get the selected file path. If an image file is selected, we call the convert_to_circle function to convert the image into a circular image. Then, we shrink the image to fit the display area and display it on the image label in the program.
Finally, we define a function called save_image, which is used to save the currently displayed circular image as a file. In this function, we get the currently displayed circular image and open a file dialog to select a save path. If a save path is selected, we save the image as a file.
In the main program, we create a main window and set the title and size of the window. Then, we create a select button that calls the select_image function when clicked. Next, we create a label for displaying the image. Finally, we create a save button that calls the save_image function when clicked.
Finally, we run the main loop to start the application and wait for user interaction.
The code uses the PIL library (Python Imaging Library) for image processing and the tkinter library for creating the graphical user interface. It displays the image by converting it to a PhotoImage object and assigning it to the image attribute of the image display area. At the same time, it keeps a reference to the original PIL image for use when saving the image.