Merge pull request #1 from jeena/edit

Crop the picture and add metadata
This commit is contained in:
Jeena 2021-09-08 10:41:24 +09:00 committed by GitHub
commit 58ec300845
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 304 additions and 24 deletions

View file

@ -7,6 +7,10 @@ name = "pypi"
google-auth-oauthlib = "*"
psycopg2-binary = "*"
google-api-python-client = "*"
opencv-python = "*"
numpy = "*"
exif = "*"
geopy = "*"
[dev-packages]

135
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "b35e976d9f0bbffebb2b0df18c4fb553d9ccf4cb3721df71dc60b46f44de7726"
"sha256": "bc276a09e3e32bf9f2527093f719e6b16d5fe0db5f59e428c282089bd8e83f86"
},
"pipfile-spec": 6,
"requires": {
@ -39,30 +39,53 @@
"markers": "python_version >= '3'",
"version": "==2.0.4"
},
"google-api-core": {
"exif": {
"hashes": [
"sha256:428805de17b48ca1af2fdb5bbfc2334e1bbcb0ea4a16506fa1337fb29c8f37c8",
"sha256:bd9eb0709f4e10dd6fddb32fd0788a190b434426c258be6e00ef40da136d768d"
],
"markers": "python_version >= '3.6'",
"version": "==2.0.0"
},
"google-api-python-client": {
"hashes": [
"sha256:20bc8f711883f9f8f96d96bc44b40ad0e2b0a9ef7df53e7c738948584fddd1c2",
"sha256:26402b51b21480e5f2de2650f4dc41d19db031ce28d26047f30dad0803fbf27d"
"sha256:7a93ced12bca131b04c7e92c13a16a40685b9eb83414f9ed479d38df1c686633",
"sha256:d9bae4fd7737a879402dd6e5d1675e230185e1221c3df62238faf2c8f08756d4"
],
"index": "pypi",
"version": "==2.17.0"
"version": "==1.3.2"
},
"google-auth": {
"geographiclib": {
"hashes": [
"sha256:c012c8be7c442c8309ca8fa0876fef33f5fd977c467be1e1c1c2f721e8ebd73c",
"sha256:ea1af050b3e06eb73e4470f704d23007307bc0e87c13e015f6b90460f1407bd3"
"sha256:8f441c527b0b8a26cd96c965565ff0513d1e4d9952b704bf449409e5015c77b7",
"sha256:ac400d672b8954b0306bca890b088bb8ba2a757dc8133cca0b878f34b33b2740"
],
"version": "==1.52"
},
"geopy": {
"hashes": [
"sha256:58b7edf526b8c32e33126570b5f4fcdfaa29d4416506064777ae8d84cd103fdd",
"sha256:8f1f949082b964385de61fcc3a667a6a9a6e242beb1ae8972449f164b2ba0e89"
],
"index": "pypi",
"version": "==2.2.0"
},
"google-api-core": {
"hashes": [
"sha256:b8ad41f72a70dd709dd13a08478936172aecf5f2d34a18ab378efa6d2d6ab575",
"sha256:d6760f21b3a064a8397916b33be7383fc169d1a3c3d7fae7b47eb92bba7892b8"
],
"markers": "python_version >= '3.6'",
"version": "==2.0.1"
},
"google-api-python-client": {
"hashes": [
"sha256:1f2657d1be246bc3d4bd1f0642f498653ce5fba507e6a05bbc405ed65bf10377",
"sha256:c89b345615188fbd525f52d59013156ad3bfd1023af27041f2dec3d7877ba112"
],
"index": "pypi",
"version": "==2.19.1"
},
"google-auth": {
"hashes": [
"sha256:104475dc4d57bbae49017aea16fffbb763204fa2d6a70f1f3cc79962c1a383a4",
"sha256:cde472372e030e1e0bc64dac00fb53e6c095d7ab641f4281e2c995e85e205d8b"
],
"markers": "python_version >= '3.6'",
"version": "==2.0.2"
},
"google-auth-httplib2": {
"hashes": [
"sha256:31e49c36c6b5643b57e82617cb3e021e3e1d2df9da63af67252c02fa9c1f4a10",
@ -72,11 +95,11 @@
},
"google-auth-oauthlib": {
"hashes": [
"sha256:4ab58e6c3dc6ccf112f921fcced40e5426fba266768986ea502228488276eaba",
"sha256:b5a1ce7c617d247ccb2dfbba9d4bfc734b41096803d854a2c52592ae80150a67"
"sha256:3f2a6e802eebbb6fb736a370fbf3b055edcb6b52878bf2f26330b5e041316c73",
"sha256:a90a072f6993f2c327067bf65270046384cda5a8ecb20b94ea9a687f1f233a7a"
],
"index": "pypi",
"version": "==0.4.5"
"version": "==0.4.6"
},
"googleapis-common-protos": {
"hashes": [
@ -101,6 +124,42 @@
"markers": "python_version >= '3'",
"version": "==3.2"
},
"numpy": {
"hashes": [
"sha256:09858463db6dd9f78b2a1a05c93f3b33d4f65975771e90d2cf7aadb7c2f66edf",
"sha256:209666ce9d4a817e8a4597cd475b71b4878a85fa4b8db41d79fdb4fdee01dde2",
"sha256:298156f4d3d46815eaf0fcf0a03f9625fc7631692bd1ad851517ab93c3168fc6",
"sha256:30fc68307c0155d2a75ad19844224be0f2c6f06572d958db4e2053f816b859ad",
"sha256:423216d8afc5923b15df86037c6053bf030d15cc9e3224206ef868c2d63dd6dc",
"sha256:426a00b68b0d21f2deb2ace3c6d677e611ad5a612d2c76494e24a562a930c254",
"sha256:466e682264b14982012887e90346d33435c984b7fead7b85e634903795c8fdb0",
"sha256:51a7b9db0a2941434cd930dacaafe0fc9da8f3d6157f9d12f761bbde93f46218",
"sha256:52a664323273c08f3b473548bf87c8145b7513afd63e4ebba8496ecd3853df13",
"sha256:550564024dc5ceee9421a86fc0fb378aa9d222d4d0f858f6669eff7410c89bef",
"sha256:5de64950137f3a50b76ce93556db392e8f1f954c2d8207f78a92d1f79aa9f737",
"sha256:640c1ccfd56724f2955c237b6ccce2e5b8607c3bc1cc51d3933b8c48d1da3723",
"sha256:7fdc7689daf3b845934d67cb221ba8d250fdca20ac0334fea32f7091b93f00d3",
"sha256:805459ad8baaf815883d0d6f86e45b3b0b67d823a8f3fa39b1ed9c45eaf5edf1",
"sha256:92a0ab128b07799dd5b9077a9af075a63467d03ebac6f8a93e6440abfea4120d",
"sha256:9f2dc79c093f6c5113718d3d90c283f11463d77daa4e83aeeac088ec6a0bda52",
"sha256:a5109345f5ce7ddb3840f5970de71c34a0ff7fceb133c9441283bb8250f532a3",
"sha256:a55e4d81c4260386f71d22294795c87609164e22b28ba0d435850fbdf82fc0c5",
"sha256:a9da45b748caad72ea4a4ed57e9cd382089f33c5ec330a804eb420a496fa760f",
"sha256:b160b9a99ecc6559d9e6d461b95c8eec21461b332f80267ad2c10394b9503496",
"sha256:b342064e647d099ca765f19672696ad50c953cac95b566af1492fd142283580f",
"sha256:b5e8590b9245803c849e09bae070a8e1ff444f45e3f0bed558dd722119eea724",
"sha256:bf75d5825ef47aa51d669b03ce635ecb84d69311e05eccea083f31c7570c9931",
"sha256:c01b59b33c7c3ba90744f2c695be571a3bd40ab2ba7f3d169ffa6db3cfba614f",
"sha256:d96a6a7d74af56feb11e9a443150216578ea07b7450f7c05df40eec90af7f4a7",
"sha256:dd0e3651d210068d13e18503d75aaa45656eef51ef0b261f891788589db2cc38",
"sha256:e167b9805de54367dcb2043519382be541117503ce99e3291cc9b41ca0a83557",
"sha256:e42029e184008a5fd3d819323345e25e2337b0ac7f5c135b7623308530209d57",
"sha256:f545c082eeb09ae678dd451a1b1dbf17babd8a0d7adea02897a76e639afca310",
"sha256:fde50062d67d805bc96f1a9ecc0d37bfc2a8f02b937d2c50824d186aa91f2419"
],
"index": "pypi",
"version": "==1.21.2"
},
"oauthlib": {
"hashes": [
"sha256:42bf6354c2ed8c6acb54d971fce6f88193d97297e18602a3a886603f9d7730cc",
@ -109,6 +168,44 @@
"markers": "python_version >= '3.6'",
"version": "==3.1.1"
},
"opencv-python": {
"hashes": [
"sha256:05c5139d620e8d02f7ce0921796d55736fa19fa15e2ec00a388db2eb1ae1e9a1",
"sha256:085232718f28bddd265da480874c37db5c7354cb08f23f4a68a8639b16276a89",
"sha256:18a4a14015eee30d9cd514db8cdefbf594b1d5c234762d27abe512d62a333bc3",
"sha256:205a73adb29c37e42475645519e612e843a985475da993d10b4d5daa6afec36a",
"sha256:3c001d3feec7f3140f1fb78dfc52ca28122db8240826882d175a208a89d2731b",
"sha256:437f30e300725e1d1b3744dbfbc66a523a4744792b58f3dbe1e9140c8f4dfba5",
"sha256:5366fcd6eae4243add3c8c92142045850f1db8e464bcf0b75313e1596b2e3671",
"sha256:54c64e86a087841869901fd34462bb6bec01cd4652800fdf5d92fe7b0596c82f",
"sha256:6763729fcfee2a08e069aa1982c9a8c1abf55b9cdf2fb9640eda1d85bdece19a",
"sha256:68813b720b88e4951e84399b9a8a7b532d45a07a96ea8f539636242f862e32e0",
"sha256:7f41b97d84ac66bdf13cb4d9f4dad3e159525ba1e3f421e670c787ce536eb70a",
"sha256:831b92fe63ce18dd628f71104da7e60596658b75e2fa16b83aefa3eb10c115e2",
"sha256:881f3d85269500e0c7d72b140a6ebb5c14a089f8140fb9da7ce01f12a245858e",
"sha256:8852be06c0749fef0d9c58f532bbcb0570968c59e41cf56b90f5c92593c6e108",
"sha256:8b5bc61be7fc8565140b746288b370a4bfdb4edb9d680b66bb914e7690485db1",
"sha256:8d3282138f3a8646941089aae142684910ebe40776266448eab5f4bb609fc63f",
"sha256:9a78558b5ae848386edbb843c761e5fed5a8480be9af16274a5a78838529edeb",
"sha256:b42bbba9f5421865377c7960bd4f3dd881003b322a6bf46ed2302b89224d102b",
"sha256:c360cb76ad1ddbd5d2d3e730b42f2ff6e4be08ea6f4a6eefacca175d27467e8f",
"sha256:cdc3363c2911d7cfc6c9f55308c51c2841a7aecbf0bf5e791499d220ce89d880",
"sha256:e1f54736272830a1e895cedf7a4ee67737e31e966d380c82a81ef22515d043a3",
"sha256:e42c644a70d5c54f53a4b114dbd88b4eb83f42a9ca998f07bd5682f3f404efcc",
"sha256:f1bda4d144f5204e077ca4571453ebb2015e5748d5e0043386c92c2bbf7f52eb",
"sha256:f3ac2355217114a683f3f72a9c40a5890914a59c4a2df62e4083c66ff65c9cf9"
],
"index": "pypi",
"version": "==4.5.3.56"
},
"plum-py": {
"hashes": [
"sha256:6d0bdf7aefa10e519d2902827dee2f96a554e55f04064c978197714b26ca1ab9",
"sha256:b02077cda2403d9dac65db38dba072a9db751129cc7b6d569837bf7a2276fd39"
],
"markers": "python_version >= '3.6'",
"version": "==0.7.0"
},
"protobuf": {
"hashes": [
"sha256:13ee7be3c2d9a5d2b42a1030976f760f28755fcf5863c55b1460fd205e6cd637",

169
edit.py Executable file
View file

@ -0,0 +1,169 @@
#!/usr/bin/env python3
import cv2
import sys
import numpy as np
import exif
from geopy.geocoders import Nominatim
from datetime import datetime
def wait_with_check_closing(win_name):
"""
https://stackoverflow.com/questions/35003476/"
"opencv-python-how-to-detect-if-a-window-is-closed/37881722
"""
while True:
keyCode = cv2.waitKey(50)
if keyCode != -1:
break
win_prop = cv2.getWindowProperty(win_name, cv2.WND_PROP_VISIBLE)
if win_prop <= 0:
break
def dms_coordinates_to_dd_coordinates(coordinates, coordinates_ref):
decimal_degrees = coordinates[0] + \
coordinates[1] / 60 + \
coordinates[2] / 3600
if coordinates_ref == "S" or coordinates_ref == "W":
decimal_degrees = -decimal_degrees
return decimal_degrees
class Image:
def __init__(self, path, w=1920, h=1080):
self.w = w
self.h = h
self.path = path
self.image = cv2.imread(self.path)
self.geolocator = Nominatim(user_agent="jeena-synology-pictures")
def get_exif(self):
with open(self.path, 'rb') as img:
e = exif.Image(img)
if e and e.has_exif:
return e
else:
return None
def get_place_name(self, e):
lat = str(dms_coordinates_to_dd_coordinates(e['gps_latitude'], e['gps_latitude_ref']))
lng = str(dms_coordinates_to_dd_coordinates(e['gps_longitude'], e['gps_longitude_ref']))
if lat and lng:
location = self.geolocator.reverse(lat + ", " + lng, language="en")
city = location.raw.get('address', {}).get('city', "")
country = location.raw.get('address', {}).get('country', "")
name = ", ".join((city, country))
return name
else:
return ""
def crop(self):
oh, ow, z = self.image.shape
w = self.w
h = self.h
vertical = False
if oh/ow < 1:
# horizontal
if ow <= self.w:
w = ow
h = oh
else:
# need resizing to smaller
if self.w/ow < self.h/oh:
# hight priority
w = self.h/oh * ow
h = self.h
else:
w = self.w
h = self.w/ow * oh
else:
# vertical
vertical = True
if oh <= self.h:
w = ow
h = oh
else:
# need resizing to smaller
w = self.h/oh * ow
h = self.h
if vertical:
self.image = cv2.resize(self.image, (int(w), int(h)))
self.image = self.image[0:self.h, 0:self.w]
bg_image = cv2.resize(self.image.copy(), (self.w, self.h))
# make darker
bg_image = cv2.add(bg_image, np.array([-25.0]))
bg_image = cv2.blur(bg_image, (200, 200))
x_offset = int(self.w / 2 - self.image.shape[1] / 2)
y_offset = 0
bg_image[y_offset:y_offset+self.image.shape[0], x_offset:x_offset+self.image.shape[1]] = self.image
self.image = bg_image
else:
self.image = cv2.resize(self.image, (int(w), int(h)))
# center zoom
x = self.image.shape[1]/2 - self.w/2
y = self.image.shape[0]/2 - self.h/2
if x > 0 and y > 0:
self.image = self.image[int(y):int(y+self.h), int(x):int(x+self.w)]
def add_metadata(self):
e = self.get_exif()
if e:
d = datetime.strptime(e['datetime_original'], "%Y:%m:%d %H:%M:%S")
date = d.strftime("%Y-%m-%d %H:%M")
place = self.get_place_name(e)
self.writeText(date, 2)
self.writeText(place, 1)
def writeText(self, text, line_from_bottom):
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
font = cv2.FONT_HERSHEY_SIMPLEX
font_size = 1
font_color = BLACK
font_thickness = 4
line_height = 1.2
padding = 20
textsize, baseline = cv2.getTextSize(text, font, font_size, font_thickness)
x = self.image.shape[1] - textsize[0] - padding
y = int(self.image.shape[0] - textsize[1] * line_from_bottom * line_height + baseline - padding)
print(baseline)
# outline
self.image = cv2.putText(self.image,
text,
(x,y),
font,
font_size,
font_color,
font_thickness,
cv2.LINE_AA)
# text
font_color = WHITE
font_thickness = 2
self.image = cv2.putText(self.image,
text,
(x,y),
font,
font_size,
font_color,
font_thickness,
cv2.LINE_AA)
def safe(self, new_path):
cv2.imwrite(new_path, self.image)
def show(self):
title = "Image"
cv2.imshow(title, self.image)
wait_with_check_closing(title)
cv2.destroyAllWindows()
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: edit.py path/to/picture.jpeg")
else:
img_path = sys.argv[1]
img = Image(img_path)
img.crop()
img.add_metadata()
img.show()
img.safe("/home/jeena/Downloads/test.jpg")

20
ha.py
View file

@ -1,20 +1,30 @@
#!/usr/bin/env python3
import synology
# from wand.image import Image
import os
import shutil
import edit
def prepare_photo(i, path):
# sudo apt-get install ufraw-batch
# convert 13895032967_642e23af42_o.jpg -adaptive-resize 1920x1080\> -size 1920x1080 xc:black +swap -gravity center -composite 01.jpg
# filename, file_extension = os.path.splitext(path)
new_path = os.path.join(os.path.dirname(path), str(i) + '.jpg')
cmd = 'convert "' + path + '" -adaptive-resize 1920x1080\> -size 1920x1080 xc:black +swap -gravity center -composite ' + new_path
n_path = new_path(i, path)
cmd = 'convert "' + path + '" -adaptive-resize 1920x1080\> -size 1920x1080 xc:black +swap -gravity center -composite ' + n_path
print(cmd)
os.system(cmd)
# os.rename(path, new_path)
return new_path
return n_path
def new_path(i, path):
return os.path.join(os.path.dirname(path), str(i) + '.jpg')
def resize_and_crop(i, path):
n_path = new_path(i, path)
img = edit.Image(path)
img.crop()
img.safe(n_path)
return n_path
def upload_photo(remotepath, path):
cmd = 'scp ' + path + ' ' + remotepath
@ -32,6 +42,6 @@ if __name__ == "__main__":
with os.scandir(dirpath) as dirs:
for i, entry in enumerate(dirs):
upload_photo(ha_path, prepare_photo(i, os.path.join(dirpath, entry.name)))
upload_photo(ha_path, resize_and_crop(i, os.path.join(dirpath, entry.name)))
shutil.rmtree(dirpath)