Add meta data as text overlay
This commit is contained in:
parent
43a3c34867
commit
23a6c12fbe
3 changed files with 135 additions and 21 deletions
82
edit.py
82
edit.py
|
@ -2,6 +2,10 @@
|
|||
|
||||
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):
|
||||
"""
|
||||
|
@ -16,12 +20,41 @@ def wait_with_check_closing(win_name):
|
|||
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
|
||||
|
@ -56,8 +89,10 @@ class Image:
|
|||
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.image.shape[0] / 2)
|
||||
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
|
||||
|
@ -69,6 +104,50 @@ class Image:
|
|||
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)
|
||||
|
||||
|
@ -85,5 +164,6 @@ if __name__ == "__main__":
|
|||
img_path = sys.argv[1]
|
||||
img = Image(img_path)
|
||||
img.crop()
|
||||
img.add_metadata()
|
||||
img.show()
|
||||
img.safe("/home/jeena/Downloads/test.jpg")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue