프로젝트 구성
app_image.py
from flask import Flask, render_template, request
from flask_ngrok import run_with_ngrok
import os
from PIL import Image
app = Flask(__name__)
run_with_ngrok(app)
'''
이미지 처리 함수
'''
def image_resize(image, width, height):
return image.resize((int(width), int(height)))
def image_rotate(image):
return image.transpose(Image.ROTATE_180)
def image_change_bw(image):
return image.convert('L')
'''
플라스크
'''
@app.route("/")
def index():
return render_template('image.html')
@app.route('/image_preprocess', methods=['POST'])
def preprocessing():
if request.method == 'POST':
file = request.files['uploaded_image']
if not file: return render_template('index.html', label="No Files")
img = Image.open(file)
is_rotate_180 = request.form.get('pre_toggle_0')
is_change_bw = request.form.get('pre_toggle_1')
is_change_size = request.form.get('pre_toggle_2')
if is_rotate_180 == 'on':
img = image_rotate(img)
if is_change_bw == 'on':
img = image_change_bw(img)
if is_change_size == 'on':
img = image_resize(img, request.form.get('changed_width'), request.form.get('changed_height'))
img.save('static/result_image.png')
# 결과 리턴
return render_template('image.html', label=image_path)
if __name__ == '__main__':
app.run()
index.html
<form action="/image_preprocess" method="POST" enctype="multipart/form-data">
<h1>이미지 업로드 하기</h1>
<input type="file" name="uploaded_image">
<h1>이미지 전처리 종류 선택</h1>
<input type="checkbox" name="pre_toggle_0">
<span>180도 회전 </span>
<br>
<input type="checkbox" name="pre_toggle_1">
<span>흑백 변경 </span>
<br>
<input type="checkbox" name="pre_toggle_2">
<span>이미지 사이즈 변경 </span>
<br>
<button>변경</button>
</form>
<html>
<head>
<title>전처리 페이지 UI</title>
</head>
<body>
<form action="/get_selected_table" method="POST" enctype="multipart/form-data">
<input type="text" name="table_name" placeholder="테이블 명" required="required" />
<button type="submit">선택</button>
{% if label %}
<span>
{{ label }}
</span>
{% endif %}
</form>
<form action="/get_column_name_change" method="POST" enctype="multipart/form-data">
<h1>컬럼 이름 변경</h1>
<input type="text" name="before_column_name" placeholder="변경 전 컬럼명" required="required" />
<input type="text" name="after_column_name" placeholder="변경 후 컬럼명"required="required" />
<br>
<br>
<button type="submit">변경</button>
</form>
<form action="/get_image_pre_status" method="POST" enctype="multipart/form-data">
<h1>이미지 전처리 종류 선택</h1>
<input type="checkbox" name="pre_toggle_0">
<span>180도 회전 </span>
<br>
<input type="checkbox" name="pre_toggle_1">
<span>흑백 변경 </span>
<br>
<input type="checkbox" name="pre_toggle_2">
<span>이미지 사이즈 변경 </span>
<br>
<button type="submit">변경</button>
</form>
<form action="/upload_image" method="POST" enctype="multipart/form-data">
<h1>이미지 업로드 하기</h1>
<input type="file" name="uploaded_image">
<button>이미지 업로드</button>
{% if label %}
<span>
{{ label }}
</span>
{% endif %}
</form>
</body>
</html>
templates/data.html
<html>
<head>
<title>SQL 처리 페이지</title>
</head>
<body>
<h1>SQL 처리 페이지</h1>
<form action="/dbsql" method="POST">
<h2>데이터베이스 이름</h2>
<span> <input type="text" name="db_name" placeholder="ex)name.db"> </span>
<h2>SQL (명령어 한 줄만 가능)</h2>
<textarea name="sql" cols="40" rows="10" placeholder="ex) SELECT * FROM table_name"></textarea>
<br>
{% if label %}
<span class="result_lable">
{% block content %}
{{ label }}
{% endblock %}
</span>
<br>
{% endif %}
<button type="submit">SQL 전송</button>
</form>
</body>