From 98c850ac6250d68c11ee6569c9023dc63204c2e8 Mon Sep 17 00:00:00 2001 From: Stefal Date: Thu, 26 Oct 2023 21:50:26 +0200 Subject: [PATCH] Get and write artist, camera make and model --- mapillary_download.py | 10 ++++++++-- writer.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/mapillary_download.py b/mapillary_download.py index 98f1358..c87cc4c 100644 --- a/mapillary_download.py +++ b/mapillary_download.py @@ -42,10 +42,10 @@ def download(url, filepath, metadata=None): def get_single_image_data(image_id, mly_header): - req_url = 'https://graph.mapillary.com/{}?fields=thumb_original_url,altitude,camera_type,captured_at,compass_angle,geometry,exif_orientation,sequence'.format(image_id) + req_url = 'https://graph.mapillary.com/{}?fields=creator,thumb_original_url,altitude,make,model,camera_type,captured_at,compass_angle,geometry,exif_orientation,sequence'.format(image_id) r = session.get(req_url, headers=mly_header) data = r.json() - #print(data) + print(data) return data @@ -99,6 +99,9 @@ def write_exif(picture, img_metadata): # 'captured_at': 1603459736644, 'geometry': {'type': 'Point', 'coordinates': [2.5174596904057, 48.777089857534]}, 'id': '485924785946693'} with writer.Writer(picture) as image: + image.add_artist(img_metadata) + image.add_camera_make(img_metadata) + image.add_camera_model(img_metadata) image.add_datetimeoriginal(img_metadata) image.add_lat_lon(img_metadata) image.add_altitude(img_metadata) @@ -151,6 +154,9 @@ if __name__ == '__main__': path = os.path.join(path_destination, date_time_image_filename) img_metadata = writer.PictureMetadata( capture_time = datetime.utcfromtimestamp(int(image_data['captured_at'])/1000), + artist = image_data['creator']['username'], + camera_make = image_data['make'], + camera_model = image_data['model'], longitude = image_data['geometry']['coordinates'][0], latitude = image_data['geometry']['coordinates'][1], picture_type = PictureType("equirectangular") if image_data['camera_type'] == 'spherical' or image_data['camera_type'] == 'equirectangular' else PictureType("flat"), diff --git a/writer.py b/writer.py index 99a8d27..7c19acc 100644 --- a/writer.py +++ b/writer.py @@ -20,6 +20,9 @@ tz_finder = timezonefinder.TimezoneFinder() @dataclass class PictureMetadata: + artist: Optional[str] = None + camera_make: Optional[str] = None + camera_model: Optional[str] = None capture_time: Optional[datetime] = None longitude: Optional[float] = None latitude: Optional[float] = None @@ -146,6 +149,32 @@ class Writer(): self.updated_xmp["Xmp.GPano.ProjectionType"] = metadata.picture_type.value self.updated_xmp["Xmp.GPano.UsePanoramaViewer"] = True + def add_artist(self, metadata: PictureMetadata) -> None: + """ + Add image author in Exif Artist tag + """ + + if metadata.artist is not None: + self.updated_exif["Exif.Image.Artist"] = ascii(metadata.artist).strip("'") + + + def add_camera_make(self, metadata: PictureMetadata) -> None: + """ + Add camera manufacture in Exif Make tag + """ + + if metadata.camera_make is not None: + self.updated_exif["Exif.Image.Make"] = ascii(metadata.camera_make).strip("'") + + + def add_camera_model(self, metadata: PictureMetadata) -> None: + """ + Add camera model in Exif Model tag + """ + + if metadata.camera_model is not None: + self.updated_exif["Exif.Image.Model"] = ascii(metadata.camera_model).strip("'") + def format_offset(self, offset: timedelta) -> str: """Format offset for OffsetTimeOriginal. Format is like "+02:00" for paris offset >>> format_offset(timedelta(hours=5, minutes=45)) @@ -156,7 +185,6 @@ class Writer(): offset_hour, remainer = divmod(offset.total_seconds(), 3600) return f"{'+' if offset_hour >= 0 else '-'}{int(abs(offset_hour)):02}:{int(remainer/60):02}" - def localize(self, naive_dt: datetime, metadata: PictureMetadata) -> datetime: """ Localize a datetime in the timezone of the picture @@ -188,7 +216,6 @@ class Writer(): return tz.localize(naive_dt) - def _from_dms(self, val: str) -> float: """Convert exif lat/lon represented as degre/minute/second into decimal >>> _from_dms("1/1 55/1 123020/13567") @@ -206,7 +233,6 @@ class Writer(): return float(deg) + float(min) / 60 + float(sec) / 3600 - def _to_dms(self, value: float) -> Tuple[int, int, float]: """Return degree/minute/seconds for a decimal >>> _to_dms(38.889469) @@ -223,7 +249,6 @@ class Writer(): return deg, int(min), round(sec, 8) - def _to_exif_dms(self, value: float) -> str: """Return degree/minute/seconds string formated for the exif metadata for a decimal >>> _to_exif_dms(38.889469)