0% found this document useful (0 votes)
6 views13 pages

Python Code 12

Learn python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Python Code 12

Learn python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

def upscale_image(self):

if not self.current_image:
return
self.add_to_history()
self.show_progress("Upscaling image...")

def upscale():
try:
# Simple upscaling (2x) using high-quality resampling
width, height = self.current_image.size
new_size = (width * 2, height * 2)
self.current_image =
self.current_image.resize(new_size, Image.Resampling.LANCZOS)

self.root.after(0, self.display_image)
self.root.after(0, self.update_image_info)
self.root.after(0, lambda:
self.status_label.configure(text="Image upscaled 2x"))
except Exception as e:
self.root.after(0, lambda:
messagebox.showerror("Error", str(e)))
finally:
self.root.after(0, self.hide_progress)

threading.Thread(target=upscale, daemon=True).start()

# AI Analysis with Gemini


def analyze_image(self):
if not self.current_image:
messagebox.showwarning("Warning", "Please load an image
first")
return

self.show_progress("Analyzing image with AI...")

def analyze():
try:
# Convert PIL image to base64 for Gemini API
buffered = io.BytesIO()
self.current_image.save(buffered, format="PNG")
img_base64 =
base64.b64encode(buffered.getvalue()).decode()

# Prepare the image for Gemini


image_data = {
'mime_type': 'image/png',
'data': img_base64
}

# Generate analysis
prompt = """Analyze this image in detail. Provide
information about:
1. Main subjects and objects
2. Colors and composition
3. Style and mood
4. Technical quality
5. Suggested improvements
6. Artistic elements
Please be descriptive and helpful."""

try:
response =
self.gemini_model.generate_content([prompt, image_data])
analysis_result = response.text
except:
# Fallback analysis if Gemini API fails
analysis_result =
self.generate_fallback_analysis()

self.root.after(0, lambda:
self.display_analysis(analysis_result))
self.root.after(0, lambda:
self.status_label.configure(text="Image analysis complete"))

except Exception as e:
fallback_analysis = self.generate_fallback_analysis()
self.root.after(0, lambda:
self.display_analysis(fallback_analysis))
self.root.after(0, lambda:
self.status_label.configure(text="Analysis complete (offline mode)"))
finally:
self.root.after(0, self.hide_progress)

threading.Thread(target=analyze, daemon=True).start()

def generate_fallback_analysis(self):
"""Generate basic analysis when AI is not available"""
if not self.current_image:
return "No image loaded"

width, height = self.current_image.size


mode = self.current_image.mode

# Basic color analysis


colors = self.current_image.getcolors(maxcolors=256*256*256)
dominant_color = max(colors, key=lambda x: x[0])[1] if colors
else "Unknown"

# Aspect ratio
aspect_ratio = width / height
orientation = "Landscape" if aspect_ratio > 1 else "Portrait"
if aspect_ratio < 1 else "Square"

analysis = f"""📊 BASIC IMAGE ANALYSIS

Technical Details:
• Dimensions: {width} x {height} pixels
• Color Mode: {mode}
• Orientation: {orientation}
• Aspect Ratio: {aspect_ratio:.2f}

🎨 Visual Analysis:
• Image appears to be in {mode} color mode
• Resolution: {'High' if width > 1920 else 'Medium' if width > 800
else 'Low'}
• File size suitable for {'print' if width > 2000 else 'web' if width
> 500 else 'thumbnail'} use

💡 Suggestions:
• Consider enhancing contrast for better visual impact
• Try artistic filters for creative effects
• Use upscaling if higher resolution is needed
• Experiment with color adjustments

Note: For detailed AI analysis, please configure Gemini API key."""

return analysis

def display_analysis(self, analysis_text):


self.analysis_text.delete("1.0", "end")
self.analysis_text.insert("1.0", analysis_text)

# Slider Event Handlers


def on_brightness_change(self, value):
if self.current_image and self.original_image:
enhancer = ImageEnhance.Brightness(self.original_image)
factor = 1.0 + (float(value) / 100.0)
self.current_image = enhancer.enhance(factor)
self.display_image()

def on_contrast_change(self, value):


if self.current_image and self.original_image:
enhancer = ImageEnhance.Contrast(self.original_image)
factor = 1.0 + (float(value) / 100.0)
self.current_image = enhancer.enhance(factor)
self.display_image()

def on_saturation_change(self, value):


if self.current_image and self.original_image:
enhancer = ImageEnhance.Color(self.original_image)
factor = 1.0 + (float(value) / 100.0)
self.current_image = enhancer.enhance(factor)
self.display_image()

# Adjustment Applications
def apply_brightness_adjustment(self, value):
if self.current_image:
self.add_to_history()
enhancer = ImageEnhance.Brightness(self.current_image)
self.current_image = enhancer.enhance(value)
self.display_image()

def apply_contrast_adjustment(self, value):


if self.current_image:
self.add_to_history()
enhancer = ImageEnhance.Contrast(self.current_image)
self.current_image = enhancer.enhance(value)
self.display_image()

def apply_saturation_adjustment(self, value):


if self.current_image:
self.add_to_history()
enhancer = ImageEnhance.Color(self.current_image)
self.current_image = enhancer.enhance(value)
self.display_image()

def apply_gamma_correction(self, gamma):


if self.current_image:
self.add_to_history()
# Convert to numpy for gamma correction
img_array = np.array(self.current_image)
gamma_corrected = np.power(img_array / 255.0, gamma) *
255.0
gamma_corrected = np.clip(gamma_corrected, 0,
255).astype(np.uint8)
self.current_image = Image.fromarray(gamma_corrected)
self.display_image()

# Dialog Helpers
def show_adjustment_dialog(self, title, callback, default=1.0,
range_=(0.1, 3.0)):
dialog = ctk.CTkToplevel(self.root)
dialog.title(f"Adjust {title}")
dialog.geometry("300x150")
dialog.transient(self.root)
dialog.grab_set()

# Center the dialog


dialog.geometry("+%d+%d" % (self.root.winfo_rootx() + 50,
self.root.winfo_rooty() + 50))
ctk.CTkLabel(dialog, text=f"Adjust {title}:", font=("Arial",
14)).pack(pady=10)

slider = ctk.CTkSlider(dialog, from_=range_[0], to=range_[1],


number_of_steps=100)
slider.set(default)
slider.pack(pady=10, padx=20, fill="x")

value_label = ctk.CTkLabel(dialog, text=f"{default:.2f}")


value_label.pack(pady=5)

def update_label(value):
value_label.configure(text=f"{float(value):.2f}")

slider.configure(command=update_label)

button_frame = ctk.CTkFrame(dialog)
button_frame.pack(pady=10)

def apply_and_close():
callback(slider.get())
dialog.destroy()

ctk.CTkButton(button_frame, text="Apply",
command=apply_and_close).pack(side="left", padx=5)
ctk.CTkButton(button_frame, text="Cancel",
command=dialog.destroy).pack(side="left", padx=5)

def show_text_input_dialog(self, title, prompt, callback):


dialog = ctk.CTkToplevel(self.root)
dialog.title(title)
dialog.geometry("400x200")
dialog.transient(self.root)
dialog.grab_set()

# Center the dialog


dialog.geometry("+%d+%d" % (self.root.winfo_rootx() + 50,
self.root.winfo_rooty() + 50))

ctk.CTkLabel(dialog, text=prompt, font=("Arial",


14)).pack(pady=10)

text_entry = ctk.CTkTextbox(dialog, height=80)


text_entry.pack(pady=10, padx=20, fill="both", expand=True)

button_frame = ctk.CTkFrame(dialog)
button_frame.pack(pady=10)

def submit_and_close():
text = text_entry.get("1.0", "end-1c").strip()
if text:
callback(text)
dialog.destroy()

ctk.CTkButton(button_frame, text="Generate",
command=submit_and_close).pack(side="left", padx=5)
ctk.CTkButton(button_frame, text="Cancel",
command=dialog.destroy).pack(side="left", padx=5)

text_entry.focus()

# Progress and Status


def show_progress(self, message):
self.status_label.configure(text=message)
self.progress_bar.set(0.5) # Indeterminate progress
self.root.update()

def hide_progress(self):
self.progress_bar.set(0)

# Mouse Events
def on_canvas_click(self, event):
self.canvas.scan_mark(event.x, event.y)

def on_canvas_drag(self, event):


self.canvas.scan_dragto(event.x, event.y, gain=1)

def on_mouse_wheel(self, event):


if event.state & 0x4: # Ctrl key pressed
if event.delta > 0:
self.zoom_in()
else:
self.zoom_out()
else:
self.canvas.yview_scroll(int(-1 * (event.delta / 120)),
"units")

# Utility Functions
def darken_color(self, hex_color):
"""Darken a hex color for hover effects"""
hex_color = hex_color.lstrip('#')
rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
darkened = tuple(max(0, int(c * 0.8)) for c in rgb)
return f"#{darkened[0]:02x}{darkened[1]:02x}{darkened[2]:02x}"

def load_sample_images(self):
"""Load sample images for demonstration"""
try:
# Create a sample image if no image is loaded
sample = Image.new('RGB', (800, 600), color='lightblue')

# Add some text


try:
from PIL import ImageDraw, ImageFont
draw = ImageDraw.Draw(sample)

# Try to use a default font


try:
font = ImageFont.truetype("arial.ttf", 48)
except:
font = ImageFont.load_default()

draw.text((200, 250), "AI Gallery", fill='darkblue',


font=font)
draw.text((250, 320), "Load an image to start",
fill='darkblue', font=font)

except ImportError:
pass # Skip text if ImageDraw not available

self.original_image = sample
self.current_image = sample.copy()
self.display_image()
self.update_image_info()

except Exception as e:
print(f"Could not create sample image: {e}")

class BatchProcessor:
"""Batch processing functionality"""
def __init__(self, parent_gallery):
self.gallery = parent_gallery

def show_batch_dialog(self):
dialog = ctk.CTkToplevel(self.gallery.root)
dialog.title("🔄 Batch Processing")
dialog.geometry("600x500")
dialog.transient(self.gallery.root)

# Folder selection
folder_frame = ctk.CTkFrame(dialog)
folder_frame.pack(fill="x", padx=20, pady=10)

ctk.CTkLabel(folder_frame, text="Select Input Folder:",


font=("Arial", 14)).pack(anchor="w")

folder_path_var = tk.StringVar()
folder_entry = ctk.CTkEntry(folder_frame,
textvariable=folder_path_var, width=400)
folder_entry.pack(side="left", padx=(0, 10), fill="x",
expand=True)

def browse_folder():
folder = filedialog.askdirectory()
if folder:
folder_path_var.set(folder)

ctk.CTkButton(folder_frame, text="Browse",
command=browse_folder).pack(side="right")

# Operations selection
ops_frame = ctk.CTkFrame(dialog)
ops_frame.pack(fill="both", expand=True, padx=20, pady=10)

ctk.CTkLabel(ops_frame, text="Select Operations:",


font=("Arial", 14)).pack(anchor="w", pady=(0, 10))

# Checkboxes for different operations


operations = {
"Auto Enhance": tk.BooleanVar(),
"Resize (50%)": tk.BooleanVar(),
"Convert to JPEG": tk.BooleanVar(),
"Add Watermark": tk.BooleanVar(),
"Sharpen": tk.BooleanVar(),
"Brightness +20%": tk.BooleanVar()
}

for op_name, var in operations.items():


ctk.CTkCheckBox(ops_frame, text=op_name,
variable=var).pack(anchor="w", pady=2)

# Progress section
progress_frame = ctk.CTkFrame(dialog)
progress_frame.pack(fill="x", padx=20, pady=10)

progress_label = ctk.CTkLabel(progress_frame, text="Ready to


process")
progress_label.pack(anchor="w")

progress_bar = ctk.CTkProgressBar(progress_frame)
progress_bar.pack(fill="x", pady=5)
progress_bar.set(0)

# Buttons
button_frame = ctk.CTkFrame(dialog)
button_frame.pack(fill="x", padx=20, pady=10)

def start_batch_processing():
folder_path = folder_path_var.get()
if not folder_path:
messagebox.showwarning("Warning", "Please select a
folder")
return

selected_ops = [op for op, var in operations.items() if


var.get()]
if not selected_ops:
messagebox.showwarning("Warning", "Please select at
least one operation")
return

self.process_batch(folder_path, selected_ops,
progress_label, progress_bar)

ctk.CTkButton(button_frame, text="Start Processing",


command=start_batch_processing).pack(side="left",
padx=5)
ctk.CTkButton(button_frame, text="Close",
command=dialog.destroy).pack(side="right",
padx=5)

def process_batch(self, folder_path, operations, progress_label,


progress_bar):
"""Process images in batch"""
def process():
try:
image_files = [f for f in os.listdir(folder_path)
if f.lower().endswith(('.jpg', '.jpeg',
'.png', '.bmp'))]

if not image_files:
messagebox.showinfo("Info", "No image files found
in the selected folder")
return

output_folder = os.path.join(folder_path, "processed")


os.makedirs(output_folder, exist_ok=True)

total_files = len(image_files)

for i, filename in enumerate(image_files):


try:
# Update progress
progress = (i + 1) / total_files
progress_bar.set(progress)
progress_label.configure(text=f"Processing
{filename}...")

# Load image
img_path = os.path.join(folder_path, filename)
img = Image.open(img_path)

# Apply operations
for operation in operations:
img = self.apply_batch_operation(img,
operation)

# Save processed image


output_path = os.path.join(output_folder,
f"processed_{filename}")
img.save(output_path)

except Exception as e:
print(f"Error processing {filename}: {e}")
continue

progress_label.configure(text=f"Completed! Processed
{total_files} images")
messagebox.showinfo("Success", f"Batch processing
completed!\nProcessed images saved to: {output_folder}")

except Exception as e:
messagebox.showerror("Error", f"Batch processing
failed: {str(e)}")

threading.Thread(target=process, daemon=True).start()

def apply_batch_operation(self, img, operation):


"""Apply a single operation to an image"""
if operation == "Auto Enhance":
img = ImageOps.autocontrast(img)
enhancer = ImageEnhance.Sharpness(img)
img = enhancer.enhance(1.1)
elif operation == "Resize (50%)":
width, height = img.size
img = img.resize((width//2, height//2),
Image.Resampling.LANCZOS)
elif operation == "Convert to JPEG":
if img.mode in ('RGBA', 'LA', 'P'):
img = img.convert('RGB')
elif operation == "Sharpen":
img = img.filter(ImageFilter.SHARPEN)
elif operation == "Brightness +20%":
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(1.2)
elif operation == "Add Watermark":
# Simple watermark
try:
from PIL import ImageDraw
draw = ImageDraw.Draw(img)
width, height = img.size
draw.text((width-150, height-30), "AI Gallery",
fill=(255,255,255,128))
except:
pass

return img

def main():
"""Main function to run the AI Gallery"""
try:
# Check for required packages
required_packages = ['customtkinter', 'PIL', 'numpy', 'cv2']
missing_packages = []

for package in required_packages:


try:
if package == 'PIL':
from PIL import Image
elif package == 'cv2':
import cv2
elif package == 'customtkinter':
import customtkinter
elif package == 'numpy':
import numpy
except ImportError:
missing_packages.append(package)

if missing_packages:
print(f"Missing required packages: {',
'.join(missing_packages)}")
print("Please install them using: pip install
customtkinter pillow numpy opencv-python")
return

# Create and run the application


app = AIGallery()

# Add batch processor


app.batch_processor = BatchProcessor(app)

# Add batch processing button to toolbar


batch_btn = ctk.CTkButton(
app.main_frame.winfo_children()[0], # toolbar
text="🔄 Batch",
command=app.batch_processor.show_batch_dialog,
width=80,
height=35
)
batch_btn.pack(side="left", padx=5)

# Start the application


print("🎨 Starting AI Gallery...")
print("Features available:")
print("✅ 20+ AI-powered image editing tools")
print("✅ Real-time image adjustments")
print("✅ Batch processing")
print("✅ AI image analysis")
print("✅ Multiple artistic filters")
print("✅ Professional image enhancement")

app.root.mainloop()

except Exception as e:
print(f"Error starting AI Gallery: {e}")
print("Please ensure all required packages are installed.")

if __name__ == "__main__":
main()

🎨 AI Gallery Features Overview:


20 AI Features Included:
🌟 Enhancement (5 features)
1. Auto Enhance - Automatic contrast and color balance
2. Brightness Adjustment - Smart brightness control
3. Contrast Enhancement - Dynamic contrast adjustment
4. Saturation Control - Color saturation enhancement
5. Gamma Correction - Professional gamma adjustment
🎪 Filters (5 features)
6. Gaussian Blur - Professional blur effects
7. Sharpen Filter - Image sharpening
8. Edge Detection - Advanced edge detection
9. Emboss Effect - 3D emboss filter
10. Crystallize Effect - Unique crystallization
🎨 Artistic (5 features)
11. Oil Painting - Realistic oil painting effect
12. Pencil Sketch - Artistic pencil drawing
13. Cartoon Effect - Advanced cartoon conversion
14. Pop Art Style - Vibrant pop art transformation
15. Vintage Effect - Classic vintage look
🤖 AI Generation (5 features)
16. AI Image Generation - Text-to-image with Gemini
17. Style Transfer - Artistic style application
18. Background Change - AI background replacement
19. Portrait Enhancement - AI-powered portrait improvement
20. Image Upscaling - AI-enhanced resolution increase
Additional Premium Features:
 🔍 AI Image Analysis - Detailed image analysis with Gemini API
 📊 Real-time Adjustments - Live preview sliders
 🔄 Batch Processing - Process multiple images
 📁 Smart Gallery - Folder navigation
 ⚡ Quick Actions - One-click enhancements
 💾 Multiple Export Formats - Save in various formats
 🎯 Zoom & Pan - Professional image viewing
 📈 Image History - Undo/redo functionality
Installation & Setup:
# Install required packages
pip install customtkinter pillow numpy opencv-python google-
generativeai

# For advanced features


pip install opencv-contrib-python

# Run the application


python ai_gallery.py

Gemini API Setup:


1. Get your Gemini API key from Google AI Studio
2. Replace YOUR_GEMINI_API_KEY_HERE with your actual API key
3. The app will work with basic features even without the API key
Color Scheme:
 Enhancement: Red tones (#FF6B6B)
 Filters: Teal tones (#4ECDC4)
 Artistic: Blue tones (#45B7D1)
 AI Features: Green tones (#96CEB4)
This AI Gallery provides a professional-grade image editing experience with
modern UI, real-time processing, and advanced AI capabilities!

You might also like