Sure! Below is a complete Flask app that takes an input image, upscales it based on the scaling factor provided, and displays a beautiful website with an option to compare the original and upscaled images using a slider.
Here's the complete code with embedded HTML, CSS, and JavaScript:
app.py
from flask import Flask, render_template, request
from PIL import Image
import io
import base64
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
original_img_data = None
upscaled_img_data = None
if request.method == 'POST':
# Get the uploaded image file
file = request.files.get('image')
if file:
# Open the image
img = Image.open(file)
# Get the scaling factor from the form
scale = request.form.get('scale', type=float)
if not scale or scale <= 0:
scale = 1.0 # Default scaling factor
# Upscale the image
width, height = img.size
new_size = (int(width * scale), int(height * scale))
upscaled_img = img.resize(new_size, Image.ANTIALIAS)
# Convert images to base64 to display in HTML
img_bytes = io.BytesIO()
img.save(img_bytes, format='PNG')
original_img_data = base64.b64encode(img_bytes.getvalue()).decode('ascii')
upscaled_bytes = io.BytesIO()
upscaled_img.save(upscaled_bytes, format='PNG')
upscaled_img_data = base64.b64encode(upscaled_bytes.getvalue()).decode('ascii')
return render_template('index.html', original_img_data=original_img_data, upscaled_img_data=upscaled_img_data)
if __name__ == '__main__':
app.run(debug=True)
templates/index.html
Create a folder named templates in the same directory as app.py, and inside it, create a file named index.html with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cool Image Upscaler</title>
<!-- Google Fonts for better typography -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;500;700&display=swap" rel="stylesheet">
<!-- Inline CSS for styling -->
<style>
body {
background-color: #0F2027;
background-image: linear-gradient(315deg, #0F2027 0%, #203A43 50%, #2C5364 100%);
font-family: 'Poppins', sans-serif;
margin: 0;
color: #fff;
}
.container {
width: 90%;
max-width: 900px;
margin: auto;
padding: 40px 0;
text-align: center;
}
h1 {
font-weight: 700;
margin-bottom: 10px;
}
p {
font-weight: 300;
margin-bottom: 30px;
font-size: 18px;
}
form {
margin-bottom: 40px;
}
input[type="file"], input[type="number"], input[type="submit"] {
font-family: 'Poppins', sans-serif;
padding: 12px 20px;
margin: 10px 5px;
border: none;
border-radius: 5px;
font-size: 16px;
}
input[type="file"] {
background-color: #fff;
color: #333;
cursor: pointer;
}
input[type="number"] {
width: 150px;
}
input[type="submit"] {
background-color: #1FA2FF;
background-image: linear-gradient(315deg, #1FA2FF 0%, #12D8FA 51%, #A6FFCB 100%);
color: #fff;
cursor: pointer;
transition: background 0.3s;
}
input[type="submit"]:hover {
background-position: right center;
}
.slider-container {
position: relative;
max-width: 800px;
margin: auto;
}
.img-original, .img-upscaled {
width: 100%;
display: block;
}
.img-upscaled {
position: absolute;
top: 0;
left: 0;
overflow: hidden;
width: 100%;
clip: rect(0px, 50%, 1000px, 0px);
}
.slider {
-webkit-appearance: none;
appearance: none;
width: 100%;
height: 4px;
background: #ddd;
outline: none;
opacity: 0.7;
transition: opacity .2s;
position: absolute;
bottom: -25px;
left: 0;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
background: #1FA2FF;
cursor: pointer;
border-radius: 50%;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
background: #1FA2FF;
cursor: pointer;
border-radius: 50%;
}
.label-scale {
display: block;
margin-top: 20px;
font-weight: 500;
}
footer {
text-align: center;
margin-top: 60px;
font-size: 14px;
color: #aaa;
}
footer a {
color: #1FA2FF;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<h1>Cool Image Upscaler</h1>
<p>Upscale your images instantly! Upload an image, choose a scaling factor, and compare the results.</p>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*" required>
<br>
<label for="scale" class="label-scale">Scaling Factor (e.g., 2 for double size):</label>
<input type="number" step="0.1" name="scale" min="0.1" max="10" value="1.0" required>
<br>
<input type="submit" value="Upscale Image">
</form>
{% if original_img_data and upscaled_img_data %}
<div class="slider-container">
<img src="data:image/png;base64,{{ original_img_data }}" class="img-original" id="originalImage">
<div class="img-upscaled" id="upscaledDiv">
<img src="data:image/png;base64,{{ upscaled_img_data }}" id="upscaledImage">
</div>
<input type="range" min="0" max="100" value="50" class="slider" id="slider">
</div>
<p>Use the slider to compare the original and upscaled images.</p>
{% endif %}
</div>
<!-- JavaScript for the comparison slider -->
<script>
const slider = document.getElementById('slider');
const upscaledDiv = document.getElementById('upscaledDiv');
const upscaledImage = document.getElementById('upscaledImage');
const originalImage = document.getElementById('originalImage');
slider.oninput = function() {
const sliderValue = this.value;
upscaledDiv.style.clip = `rect(0px, ${sliderValue}%, 1000px, 0px)`;
}
// Adjust the height of upscaled image container to match original image
window.onload = function() {
upscaledDiv.style.height = originalImage.offsetHeight + 'px';
upscaledImage.style.width = originalImage.offsetWidth + 'px';
upscaledImage.style.height = originalImage.offsetHeight + 'px';
}
window.onresize = function() {
upscaledDiv.style.height = originalImage.offsetHeight + 'px';
upscaledImage.style.width = originalImage.offsetWidth + 'px';
upscaledImage.style.height = originalImage.offsetHeight + 'px';
}
</script>
<footer>
<p>Created with ❤ by <a href="#">Your Name</a></p>
</footer>
</body>
</html>
Explanation:
-
app.py:
- We use Flask to handle web requests and render templates.
PIL (Pillow) library is used to handle image operations like opening, resizing, and saving images.
- The uploaded image is resized based on the scaling factor provided by the user.
- The images are converted to Base64 strings so they can be embedded directly into the HTML.
-
index.html:
- The HTML page includes embedded CSS for styling, making the website visually appealing with gradients and modern fonts.
- A form is provided for the user to upload an image and input a scaling factor.
- If images are available, a comparison slider is displayed allowing the user to slide between the original and upscaled images.
- JavaScript is used to handle the slider functionality, updating the clip of the upscaled image to reveal more or less of it based on the slider value.
Instructions to Run the App:
-
Install the required packages:
pip install flask Pillow
-
Run the Flask app:
python app.py
-
Open the app in your browser:
Navigate to http://localhost:5000 (or the port specified in your terminal).
Screenshots of the App:
Since I can't display images here, just imagine a beautiful gradient background with a clean and modern interface.
Customization:
- You can enhance the image upscaling by integrating advanced algorithms or machine learning models.
- Feel free to modify the CSS to match your aesthetic preferences.
- Replace
"Your Name" in the footer with your actual name or a link to your portfolio.
Notes:
- This app uses basic resizing for upscaling images. For higher-quality upscaling, consider using specialized libraries or models.
- Ensure that the
templates folder is correctly placed in the same directory as app.py for Flask to render the template.
Enjoy your cool, beautiful image upscaling website!