-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidImageResizer.py
More file actions
89 lines (70 loc) · 2.91 KB
/
AndroidImageResizer.py
File metadata and controls
89 lines (70 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from tkinter import Tk, filedialog, messagebox
from tkinter.ttk import Button, Label, Style
from PIL import Image
import os
# Predefined sizes for Android app development
ANDROID_SIZES = {
"xxxhdpi": (192, 192),
"xxhdpi": (144, 144),
"xhdpi": (96, 96),
"hdpi": (72, 72),
"mdpi": (48, 48)
}
def select_image():
file_path = filedialog.askopenfilename(
title="Select an Image",
filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.webp")]
)
if file_path:
selected_image_label.config(text=f"Selected: {os.path.basename(file_path)}")
select_image.file_path = file_path
def select_output_folder():
folder_path = filedialog.askdirectory(title="Select Output Folder")
if folder_path:
output_folder_label.config(text=f"Output: {folder_path}")
select_output_folder.folder_path = folder_path
def resize_image():
try:
input_image = getattr(select_image, 'file_path', None)
output_folder = getattr(select_output_folder, 'folder_path', None)
if not input_image or not output_folder:
messagebox.showerror("Error", "Please select an image and output folder.")
return
# Open the image
img = Image.open(input_image)
# Create resized images
for size_name, dimensions in ANDROID_SIZES.items():
resized_img = img.resize(dimensions, Image.LANCZOS)
output_path = os.path.join(output_folder, f"{size_name}_{os.path.basename(input_image)}")
resized_img.save(output_path)
messagebox.showinfo("Success", f"Images resized and saved in {output_folder}")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
# Initialize the GUI
root = Tk()
root.title("Android Image Resizer")
root.geometry("400x300")
root.configure(bg="#f0f4f7")
# Set Custom Icon
root.iconbitmap(os.path.join(os.path.dirname(__file__), 'app_icon.ico'))
# Style Configuration
style = Style()
style.theme_use("clam")
style.configure("TButton", font=("Arial", 12), padding=10, background="#0078d7", foreground="white")
style.map("TButton", background=[("active", "#005bb5")])
style.configure("TLabel", font=("Arial", 10), background="#f0f4f7", foreground="#333")
# UI Elements
title_label = Label(root, text="Android Image Resizer", font=("Arial Bold", 16))
title_label.pack(pady=10)
select_image_button = Button(root, text="Select Image", command=select_image)
select_image_button.pack(pady=5)
selected_image_label = Label(root, text="No image selected")
selected_image_label.pack(pady=5)
select_output_folder_button = Button(root, text="Select Output Folder", command=select_output_folder)
select_output_folder_button.pack(pady=5)
output_folder_label = Label(root, text="No output folder selected")
output_folder_label.pack(pady=5)
resize_button = Button(root, text="Resize Image", command=resize_image)
resize_button.pack(pady=20)
# Run the GUI
root.mainloop()