Skip to content

Commit e8aad5b

Browse files
Merge pull request #307 from durveshnp/main
PicEdit Project
2 parents eb9d774 + f9b642a commit e8aad5b

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

PicEdit/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PicEdit
2+
3+
**Running this project: **
4+
1. Install python3 on your system
5+
2. pip install --upgrade pip
6+
3. pip install flask opencv-python rembg
7+
4. python -u main.py

PicEdit/main.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from flask import Flask, render_template, request, flash
2+
from werkzeug.utils import secure_filename
3+
from rembg import remove
4+
import os
5+
import cv2
6+
7+
UPLOAD_FOLDER = 'uploads'
8+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
9+
10+
app = Flask(__name__)
11+
app.secret_key = 'super secret key'
12+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
13+
14+
def allowed_file(filename):
15+
return '.' in filename and \
16+
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
17+
18+
def processImage(filename, operation):
19+
print(f"the operation is {operation} and filename is {filename}")
20+
img = cv2.imread(f"uploads/{filename}")
21+
match operation:
22+
case "cgrey":
23+
newFilename = f"static/{filename}"
24+
imgProcessed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
25+
cv2.imwrite(newFilename,imgProcessed)
26+
return newFilename
27+
case "cwebp":
28+
newFilename = f"static/{filename.split('.')[0]}.webp"
29+
cv2.imwrite(newFilename,img)
30+
return newFilename
31+
case "cpng":
32+
newFilename = f"static/{filename.split('.')[0]}.png"
33+
cv2.imwrite(newFilename,img)
34+
return newFilename
35+
case "cjpg":
36+
newFilename = f"static/{filename.split('.')[0]}.jpg"
37+
cv2.imwrite(newFilename,img)
38+
return newFilename
39+
case "rembg":
40+
newFilename = f"static/{filename}"
41+
imgProcessed = remove(img)
42+
cv2.imwrite(newFilename,imgProcessed)
43+
return newFilename
44+
45+
pass
46+
47+
@app.route("/")
48+
def home():
49+
return render_template("index.html")
50+
51+
@app.route("/about")
52+
def about():
53+
return render_template("about.html")
54+
55+
@app.route("/how")
56+
def how():
57+
return render_template("how.html")
58+
59+
@app.route("/contact")
60+
def contact():
61+
return render_template("contact.html")
62+
63+
@app.route("/edit", methods=["GET","POST"])
64+
def edit():
65+
if request.method == "POST" :
66+
operation = request.form.get("operation")
67+
# check if the post request has the file part
68+
if 'file' not in request.files:
69+
flash('No file part')
70+
return "error"
71+
file = request.files['file']
72+
# If the user does not select a file, the browser submits an
73+
# empty file without a filename.
74+
if file.filename == '':
75+
flash('No selected file')
76+
return "error - no selected file"
77+
if file and allowed_file(file.filename):
78+
filename = secure_filename(file.filename)
79+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
80+
new = processImage(filename, operation)
81+
flash(f"Your image has been processed and is available <a href='/{new}' target='_blank'>here</a>")
82+
return render_template("index.html")
83+
84+
return render_template("index.html")
85+
86+
app.run(debug=True, port=5001)

PicEdit/templates/about.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>About</title>
9+
</head>
10+
11+
<body>
12+
<h1>about</h1>
13+
</body>
14+
15+
</html>

PicEdit/templates/contact.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Contact us</title>
9+
</head>
10+
11+
<body>
12+
<h1>contact</h1>
13+
</body>
14+
15+
</html>

PicEdit/templates/how.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>How to use</title>
9+
</head>
10+
11+
<body>
12+
<h1>how...</h1>
13+
</body>
14+
15+
</html>

PicEdit/templates/index.html

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>PicEdit - Edit your images online</title>
8+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
9+
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
10+
</head>
11+
12+
<body>
13+
<nav class="navbar navbar-expand-lg bg-body-tertiary">
14+
<div class="container-fluid">
15+
<a class="navbar-brand" href="#">PicEdit</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
17+
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
18+
aria-label="Toggle navigation">
19+
<span class="navbar-toggler-icon"></span>
20+
</button>
21+
<div class="collapse navbar-collapse" id="navbarSupportedContent">
22+
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
23+
<li class="nav-item">
24+
<a class="nav-link active" aria-current="page" href="/">Home</a>
25+
</li>
26+
<li class="nav-item">
27+
<a class="nav-link" href="/about">About</a>
28+
</li>
29+
<li class="nav-item">
30+
<a class="nav-link" href="/how">How to use</a>
31+
</li>
32+
<li class="nav-item">
33+
<a class="nav-link" href="/contact">Contact Us</a>
34+
</li>
35+
</ul>
36+
</div>
37+
</div>
38+
</nav>
39+
40+
{% with messages = get_flashed_messages(with_categories=true) %}
41+
{% if messages %}
42+
<ul class=flashes>
43+
{% for category, message in messages %}
44+
<div class="alert alert-success alert-dismissible fade show" role="alert">
45+
<strong>Success!</strong> {{ message | safe}}
46+
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
47+
</div>
48+
{% endfor %}
49+
</ul>
50+
{% endif %}
51+
{% endwith %}
52+
53+
<div class="container my-4">
54+
<h1 class="fs-2 text">PicEdit - Edit your images online</h1>
55+
56+
<form action="/edit" method="post" enctype="multipart/form-data">
57+
<div class="mb-3">
58+
<label for="formFile" class="form-label">Select an image to edit</label>
59+
<input class="form-control" type="file" name="file" id="formFile">
60+
</div>
61+
62+
<div class="mb-3">
63+
<div class="form-floating">
64+
<select name="operation" class="form-select" id="floatingSelect"
65+
aria-label="Floating label select example">
66+
<option selected>Choose an operation</option>
67+
<option value="cpng">Convert to PNG</option>
68+
<option value="cgrey">Convert to Greyscale</option>
69+
<option value="cjpg">Convert to JPG</option>
70+
<option value="cwebp">Convert to WEBP</option>
71+
<option value="rembg">Remove image background</option>
72+
</select>
73+
<label for="floatingSelect">Select an editing operation</label>
74+
</div>
75+
</div>
76+
<button type="submit" class="btn btn-success">Submit</button>
77+
</form>
78+
79+
</div>
80+
81+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
82+
integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz"
83+
crossorigin="anonymous"></script>
84+
</body>
85+
86+
</html>

0 commit comments

Comments
 (0)