Create Jpeg to png converter webapp

 To create a simple website app in Python for converting JPEG to PNG images, you can use a web framework like Flask. Below is a basic example of how to build such an app. We'll use Flask for the web server and the Pillow library for image processing. Ensure you have Flask and Pillow installed. You can install them using `pip`:


```bash

pip install Flask Pillow

```


Now, create a Flask app:


1. **app.py**: This is the main Flask application file.


```python

import os

from flask import Flask, render_template, request, redirect, url_for, send_from_directory

from PIL import Image


app = Flask(__name__)


# Define the upload folder and allowed extensions

UPLOAD_FOLDER = 'uploads'

ALLOWED_EXTENSIONS = {'jpg', 'jpeg'}


app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def allowed_file(filename):

    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/')

def index():

    return render_template('index.html')


@app.route('/upload', methods=['POST'])

def upload_file():

    if 'file' not in request.files:

        return redirect(request.url)

    file = request.files['file']

    if file.filename == '':

        return redirect(request.url)

    if file and allowed_file(file.filename):

        filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)

        file.save(filename)


        # Convert JPEG to PNG

        image = Image.open(filename)

        png_filename = os.path.join(app.config['UPLOAD_FOLDER'], os.path.splitext(file.filename)[0] + '.png')

        image.save(png_filename, 'PNG')


        return redirect(url_for('uploaded_file', filename=os.path.basename(png_filename)))

    else:

        return 'Invalid file format'


@app.route('/uploads/<filename>')

def uploaded_file(filename):

    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


if __name__ == '__main__':

    os.makedirs(UPLOAD_FOLDER, exist_ok=True)

    app.run(debug=True)

```


2. **templates/index.html**: This is an HTML template for the app.


```html

<!DOCTYPE html>

<html>

<head>

    <title>JPEG to PNG Converter</title>

</head>

<body>

    <h1>JPEG to PNG Converter</h1>

    <form action="/upload" method="post" enctype="multipart/form-data">

        <input type="file" name="file">

        <input type="submit" value="Convert to PNG">

    </form>

</body>

</html>

```


3. **Run the Application**:


To run the application, execute the `app.py` script. The web app will be accessible at `http://localhost:5000`.


This is a basic example, and you can further enhance it by adding error handling, security measures, and additional features as needed. You can also style the HTML template to improve the user interface.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

GitLab pipeline stopped working with invalid yaml error

How do I add a printer in OpenSUSE which is being shared by a CUPS print server?