From 1741678cd6e380d6daf2ee7db3368374f5853ef0 Mon Sep 17 00:00:00 2001 From: PragatiBasnet29 Date: Wed, 23 Oct 2024 23:27:15 +0545 Subject: [PATCH] Initial commit for WebP to PNG/SVG converter app --- projects/webp to png,svg converter/README.md | 83 +++++++++++++++++++ projects/webp to png,svg converter/app.py | 49 +++++++++++ projects/webp to png,svg converter/convert.py | 41 +++++++++ 3 files changed, 173 insertions(+) create mode 100644 projects/webp to png,svg converter/README.md create mode 100644 projects/webp to png,svg converter/app.py create mode 100644 projects/webp to png,svg converter/convert.py diff --git a/projects/webp to png,svg converter/README.md b/projects/webp to png,svg converter/README.md new file mode 100644 index 000000000..19a073283 --- /dev/null +++ b/projects/webp to png,svg converter/README.md @@ -0,0 +1,83 @@ +# WebP to PNG/SVG Converter + +This is a simple Flask web application that allows users to convert images from WebP format to PNG or SVG format. + +## Features + +- Convert WebP images to PNG format +- Convert WebP images to SVG format +- Easy-to-use web interface + +## Requirements + +- Python 3.7+ +- Flask +- Pillow +- CairoSVG (for SVG conversion) +- Docker (optional, if using Docker to run the app) + +## Installation + +### 1. Clone the Repository + +```bash +git clone https://github.com/yourusername/converter-app.git +cd converter-app +``` + +### 2. Set Up a Virtual Environment (optional but recommended) + +```bash +python3 -m venv venv +source venv/bin/activate # On Windows: venv\Scripts\activate +``` + +### 3. Install Required Packages + +```bash +pip install -r requirements.txt +``` + +Make sure the following libraries are listed in `requirements.txt`: + +- Flask +- Pillow +- CairoSVG + +### 4. Running the App + +To start the Flask application, run the following command: + +```bash +python app.py +``` + +You can access the app in your browser at: `http://127.0.0.1:5000` + +## Usage + +1. Open the app in your web browser. +2. Upload a WebP image file. +3. Select the output format (PNG or SVG). +4. Click **Convert**. +5. The converted image will be available for download. + +## Troubleshooting + +- **ModuleNotFoundError**: If you get a `ModuleNotFoundError` for `Pillow` or `CairoSVG`, ensure they are properly installed: + ```bash + pip install Pillow CairoSVG + ``` + +## License + +This project is licensed under the MIT License. + + +### Key Sections: +- **Features**: Describes the capabilities of the app. +- **Requirements**: Lists all required libraries and tools. +- **Installation**: Step-by-step guide to set up and run the app. +- **Usage**: Instructions for converting images. +- **Troubleshooting**: Helpful for resolving common issues. +- **License**: Ensures the project is open and shared under MIT License. diff --git a/projects/webp to png,svg converter/app.py b/projects/webp to png,svg converter/app.py new file mode 100644 index 000000000..4c60da42c --- /dev/null +++ b/projects/webp to png,svg converter/app.py @@ -0,0 +1,49 @@ +from flask import Flask, request, send_file +from convert import convert_webp_to_png, convert_webp_to_svg +import os + +app = Flask(__name__) + +UPLOAD_FOLDER = 'uploads' +if not os.path.exists(UPLOAD_FOLDER): + os.makedirs(UPLOAD_FOLDER) + + +@app.route('/') +def home(): + return ''' +

Convert WEBP to PNG or SVG

+
+

+

+ +
+ ''' + + +@app.route('/convert', methods=['POST']) +def convert_image(): + file = request.files['file'] + output_format = request.form['format'] + + if file and output_format: + input_path = os.path.join(UPLOAD_FOLDER, file.filename) + file.save(input_path) + + if output_format == 'png': + output_path = input_path.replace('.webp', '.png') + convert_webp_to_png(input_path, output_path) + elif output_format == 'svg': + output_path = input_path.replace('.webp', '.svg') + convert_webp_to_svg(input_path, output_path) + + return send_file(output_path, as_attachment=True) + + return "Failed to convert" + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/projects/webp to png,svg converter/convert.py b/projects/webp to png,svg converter/convert.py new file mode 100644 index 000000000..2442b7882 --- /dev/null +++ b/projects/webp to png,svg converter/convert.py @@ -0,0 +1,41 @@ +from PIL import Image + +import cairosvg + + +def convert_webp_to_png(input_path, output_path): + """ + Convert a WEBP image to PNG format. + + :param input_path: Path to the input WEBP file + :param output_path: Path to save the output PNG file + """ + try: + # Open the WEBP image + img = Image.open(input_path) + + # Save the image as PNG + img.save(output_path, 'PNG') + print(f"Image saved as {output_path}") + except Exception as e: + print(f"Error converting image: {e}") + + +def convert_webp_to_svg(input_path, output_path): + """ + Convert a WEBP image to SVG format. + + :param input_path: Path to the input WEBP file + :param output_path: Path to save the output SVG file + """ + try: + # Convert the WEBP to PNG first (as SVG needs a format like PNG or JPEG) + img = Image.open(input_path) + png_temp_path = input_path.replace('.webp', '.png') + img.save(png_temp_path, 'PNG') + + # Use cairosvg to convert PNG to SVG + cairosvg.svg2png(url=png_temp_path, write_to=output_path) + print(f"Image saved as {output_path}") + except Exception as e: + print(f"Error converting image: {e}")