From 4fcb62f89340ea49a86d198b163ffac3cfe56925 Mon Sep 17 00:00:00 2001 From: Stefal Date: Sun, 17 Sep 2023 20:07:14 +0200 Subject: [PATCH] use a with statement to update image metadata --- download.py | 22 +++++++--------------- writer.py | 3 +++ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/download.py b/download.py index ca44c73..98b13c2 100644 --- a/download.py +++ b/download.py @@ -99,21 +99,13 @@ def write_exif(picture, img_metadata): #{'thumb_original_url': 'https://scontent-cdg4-2.xx.fbcdn.net/m1/v/t6/An9Zy2SrH9vXJIF01QkBODyUbg7XSKfwL48UwHyvihSwvECGjVbG0vSw9uhxe2-Dq-k2eUcigb83buO6zo-7eVbykfp5aQIe1kgd-MJr66nU_H-o_mwBLZXgVbj5I_5WX-C9c6FxJruHkV962F228O0?ccb=10-5&oh=00_AfDOKD869DxL-4ZNCbVo8Rn29vsc0JyjMAU2ctx4aAFVMQ&oe=65256C25&_nc_sid=201bca', # 'captured_at': 1603459736644, 'geometry': {'type': 'Point', 'coordinates': [2.5174596904057, 48.777089857534]}, 'id': '485924785946693'} - #picture = writer.writePictureMetadata(picture, img_metadata) - #picture = writer.add_altitude(picture, img_metadata) - #picture = writer.add_direction(picture, img_metadata) - image = writer.Writer(picture) - image.writePictureMetadata(img_metadata) - image.add_altitude(img_metadata) - image.add_direction(img_metadata) - image.apply() - import copy - #bb = copy.copy(updated_image.get_Bytes()) - #updated_image.close() - #TODO voir si j'ai encore besoin d'une copie et d'un .close() si j'ajoute la methode __exit__ à la classe Writer - #return updated_image.get_Bytes() - updated_image = image.get_Bytes() - image.close() + with writer.Writer(picture) as image: + image.writePictureMetadata(img_metadata) + image.add_altitude(img_metadata) + image.add_direction(img_metadata) + image.apply() + updated_image = image.get_Bytes() + return updated_image if __name__ == '__main__': diff --git a/writer.py b/writer.py index 436ac89..e494c14 100644 --- a/writer.py +++ b/writer.py @@ -37,6 +37,9 @@ class Writer(): self.updated_exif = {} self.updated_xmp = {} + def __enter__(self): + return self + def __exit__(self, *args): self.image.close()