Created
July 2, 2025 08:56
-
-
Save tomcurran/b8bf1bf362b7476a81bc528f23db5746 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import base64 | |
| import json | |
| import sys | |
| from PIL import Image | |
| from io import BytesIO | |
| def get_exif_date_taken(image_data): | |
| try: | |
| image = Image.open(BytesIO(image_data)) | |
| exif_data = image._getexif() | |
| if exif_data: | |
| print(f"EXIF data available") | |
| for tag, value in exif_data.items(): | |
| if tag in Image.ExifTags.TAGS: | |
| print(Image.ExifTags.TAGS[tag]) | |
| if Image.ExifTags.TAGS[tag] == 'DateTimeOriginal': | |
| return value | |
| return None | |
| except Exception as e: | |
| print(f"An error occurred while extracting EXIF data: {e}") | |
| return None | |
| def convert_json_to_image(json_file_path): | |
| try: | |
| with open(json_file_path, 'r') as file: | |
| json_data = json.load(file) | |
| image_name = json_data["name"] | |
| image_data = base64.b64decode(json_data["data"]) | |
| exif_date_taken = get_exif_date_taken(image_data) | |
| if exif_date_taken: | |
| print(f"EXIF Date Taken: {exif_date_taken}") | |
| else: | |
| print("No EXIF Date Taken found.") | |
| with open(image_name, 'wb') as image_file: | |
| image_file.write(image_data) | |
| print(f"Image file '{image_name}' has been created successfully.") | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| if __name__ == "__main__": | |
| if len(sys.argv) != 2: | |
| print("Usage: python convert_to_image.py <json_file_path>") | |
| else: | |
| json_file_path = sys.argv[1] | |
| convert_json_to_image(json_file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment