Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for WebP to PNG/SVG converter app #794

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions projects/webp to png,svg converter/README.md
Original file line number Diff line number Diff line change
@@ -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.
49 changes: 49 additions & 0 deletions projects/webp to png,svg converter/app.py
Original file line number Diff line number Diff line change
@@ -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 '''
<h1>Convert WEBP to PNG or SVG</h1>
<form method="POST" enctype="multipart/form-data" action="/convert">
<input type="file" name="file"><br><br>
<select name="format">
<option value="png">PNG</option>
<option value="svg">SVG</option>
</select><br><br>
<input type="submit" value="Convert">
</form>
'''


@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)
41 changes: 41 additions & 0 deletions projects/webp to png,svg converter/convert.py
Original file line number Diff line number Diff line change
@@ -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}")
Loading