-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
171 lines (124 loc) · 4.55 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import boto3
from dotenv import load_dotenv
from flask import Flask, request, jsonify
from flask_cors import CORS
from sqlalchemy.exc import IntegrityError
from PIL import Image
from PIL.ExifTags import TAGS
from models import db, connect_db, Photo
from urllib.request import urlopen
from filter_functions import b_and_w, posterize
from utils import create_standardized_image
load_dotenv()
CURR_USER_KEY = "curr_user"
BUCKET_NAME = os.environ['BUCKET_NAME']
REGION = os.environ['REGION']
app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
"DATABASE_URL", 'postgresql:///pixly')
app.config['SQLALCHEMY_ECHO'] = True
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
connect_db(app)
s3 = boto3.client(
"s3",
"us-east-1",
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
)
response = s3.list_buckets()
# Output the bucket names
print('Existing buckets:')
for bucket in response['Buckets']:
print(f' {bucket["Name"]}')
@app.post('/upload')
def add_photo():
""" Add photo data to database and upload to AWS
Returns json:
{id: 1, url:"http://....", fileName:"bw-img.jpg", make:"Nikon",
model:"D70", date:"12-03-22"
"""
uploaded_photo = request.files["photo"]
file_name = uploaded_photo.filename
image = Image.open(uploaded_photo)
img_data = create_standardized_image(image, file_name)
metadata = image.getexif()
data_with_tags = {}
# iterating over all EXIF data fields
for tag_id in metadata:
# get the tag name, instead of human unreadable tag id
tag = TAGS.get(tag_id, tag_id)
data = metadata.get(tag_id)
# decode bytes
if isinstance(data, bytes):
data = data.decode()
data_with_tags[tag] = data
make = data_with_tags.get('Make')
model = data_with_tags.get('Model')
date = data_with_tags.get("DateTime")
try:
photo = Photo.add_image(url=img_data['url'],
file_name=img_data['file_name'], make=make, model=model, date=date)
db.session.commit()
s3.upload_fileobj(img_data["image"],
BUCKET_NAME, img_data["file_name"])
photo_serialized = photo.serialize()
return (jsonify(photo=photo_serialized), 201)
except IntegrityError as e:
print("Error: ", e)
db.session.rollback()
return (jsonify(error="Duplicate file name"), 400)
@app.get("/photos")
def get_pictures():
""" Gets image data from database and returns json:
{id: 1, url:"http://....", fileName:"bw-img.jpg", make:"Nikon",
model:"D70", date:"12-03-22"} """
photos = Photo.query.all()
serialized = [p.serialize() for p in photos]
return jsonify(photos=serialized)
@app.post("/edit")
def edit_photo():
""" takes json data: {'fileName': 'image.jpg' 'method': 'bw'}.
Applies filter method specified by method.
Returns json {id: 10, url:"http://....", fileName:"bw-img.jpg",
make:"Nikon",
model:"D70", date:"12-03-22"}"""
file_name = request.json["fileName"]
method = request.json["method"]
img_to_edit = Image.open(
urlopen(f"https://s3.amazonaws.com/evanhesketh-pix.ly/{file_name}"))
metadata = img_to_edit.getexif()
data_with_tags = {}
# iterating over all EXIF data fields
for tag_id in metadata:
# get the tag name, instead of human unreadable tag id
tag = TAGS.get(tag_id, tag_id)
data = metadata.get(tag_id)
# decode bytes
if isinstance(data, bytes):
data = data.decode()
data_with_tags[tag] = data
# print(f"{tag:25}: {data}")
edited_file_data = ""
if method == 'bw':
edited_file_data = b_and_w(file_name, img_to_edit)
if method == 'posterize':
edited_file_data = posterize(file_name, img_to_edit)
url = edited_file_data['url']
file_name = edited_file_data['file_name']
make = data_with_tags.get('Make')
model = data_with_tags.get('Model')
date = data_with_tags.get("DateTime")
try:
photo = Photo.add_image(
url=url, file_name=file_name, make=make, model=model, date=date)
db.session.commit()
s3.upload_fileobj(
edited_file_data["image"], BUCKET_NAME, edited_file_data['file_name'])
photo_serialized = photo.serialize()
return (jsonify(photo=photo_serialized), 201)
except IntegrityError as e:
print("Error: ", e)
db.session.rollback()
return (jsonify(error="Duplicate file name"), 400)