Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Create a Python app for image optimization


Create a Python app for image optimization


Sure, here's an example of a Python app for image optimization:

Image Optimizer App

This Python app allows you to optimize images by applying various optimization techniques, such as lossy and lossless compression, resizing, and format conversion.

Features

- Supported Image Formats: JPEG, PNG, GIF, BMP, TIFF
- Optimization Techniques:
- Lossy compression (JPEG)
- Lossless compression (PNG, GIF)
- Resizing
- Format conversion (e.g., JPEG to WebP)
- Batch processing: Optimize multiple images at once
- Customizable settings: Adjust compression quality, resize dimensions, and output format
- Graphical User Interface (GUI): Provides a user-friendly interface for easy operation

Requirements

- Python 3.6 or higher
- Pillow library
- Tinify API key (for Tinify compression)

Installation

1. Install the required Python packages:


pip install pillow tinify

2. Obtain a Tinify API key from the Tinify website and save it for later use.

Usage

1. Run the Python script to launch the Image Optimizer App:


python image_optimizer.py

2. The GUI will appear, allowing you to perform the following actions:

- Select Images: Click the "Select Images" button to choose the files you want to optimize.
- Optimize Images: Adjust the optimization settings (compression quality, resize dimensions, output format) and click the "Optimize Images" button to start the process.
- Save Optimized Images: The optimized images will be saved in a new directory named "optimized_images" within the same directory as the script.

Example Code

Here's a basic implementation of the Image Optimizer App using the Tkinter GUI library and the Pillow and Tinify libraries:

python
import os
import tkinter as tk
from tkinter import filedialog
from PIL import Image
import tinify

class ImageOptimizer:
    def __init__(self, master):
        self.master = master
        master.title("Image Optimizer")

        # Create widgets
        self.select_button = tk.Button(master, text="Select Images", command=self.select_images)
        self.select_button.pack()

        self.quality_label = tk.Label(master, text="Compression Quality (JPEG):")
        self.quality_label.pack()
        self.quality_scale = tk.Scale(master, from_=50, to=100, orient=tk.HORIZONTAL)
        self.quality_scale.pack()

        self.resize_label = tk.Label(master, text="Resize Dimensions (pixels):")
        self.resize_label.pack()
        self.resize_width = tk.Entry(master)
        self.resize_width.pack()
        self.resize_height = tk.Entry(master)
        self.resize_height.pack()

        self.format_label = tk.Label(master, text="Output Format:")
        self.format_label.pack()
        self.format_var = tk.StringVar(master)
        self.format_var.set("JPEG")
        self.format_dropdown = tk.OptionMenu(master, self.format_var, "JPEG", "PNG", "WebP")
        self.format_dropdown.pack()

        self.optimize_button = tk.Button(master, text="Optimize Images", command=self.optimize_images)
        self.optimize_button.pack()

        self.images = []

    def select_images(self):
        self.images = filedialog.askopenfilenames(title="Select Images")

    def optimize_images(self):
        if not self.images:
            return

        os.makedirs("optimized_images", exist_ok=True)

        for image_path in self.images:
            image = Image.open(image_path)

            # Resize image
            width = int(self.resize_width.get()) if self.resize_width.get() else image.width
            height = int(self.resize_height.get()) if self.resize_height.get() else image.height
            image = image.resize((width, height))

            # Optimize image
            output_format = self.format_var.get()
            if output_format == "JPEG":
                quality = self.quality_scale.get()
                image.save(os.path.join("optimized_images", os.path.basename(image_path)), optimize=True, quality=quality)
            elif output_format == "PNG":
                image.save(os.path.join("optimized_images", os.path.basename(image_path)), optimize=True, compress_level=6)
            elif output_format == "WebP":
                image.save(os.path.join("optimized_images", os.path.splitext(os.path.basename(image_path))[0] + ".webp"), optimize=True, quality=self.quality_scale.get())

root = tk.Tk()
app = ImageOptimizer(root)
root.mainloop()

This code provides a basic GUI for the Image Optimizer App, allowing users to select images, adjust optimization settings, and save the optimized images. You can further enhance the app by adding more features, such as support for additional image formats, batch processing, and integration with the Tinify API for lossy compression.