From aababfa10094321c5f06b4cfdd28b6ef3a252846 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 26 Jul 2023 12:20:17 -0700 Subject: test exif --- bot/Dockerfile | 3 ++- bot/bot.py | 57 +++++++++++++++++++++++++++++++++++++-------------------- test.py | 22 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 test.py diff --git a/bot/Dockerfile b/bot/Dockerfile index 6e97ed6..33c14fb 100644 --- a/bot/Dockerfile +++ b/bot/Dockerfile @@ -3,7 +3,8 @@ FROM python:3.10 WORKDIR /usr/src/app COPY requirements.txt ./ -RUN pip install --no-cache-dir -r requirements.txt + +RUN apt-get update && apt-get install exiftool && pip install --no-cache-dir -r requirements.txt COPY . . diff --git a/bot/bot.py b/bot/bot.py index 307f18f..859edae 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -13,6 +13,7 @@ from datetime import datetime import json import sys import httpx +import exiftool if os.getenv("DB_DRIVER") == "psql": @@ -145,18 +146,34 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await file.download_to_memory(img) try: - im = Image.open(img) - exif = im.getexif() - - camera_model = "" - shot_datetime = None - if exif: - for key, val in exif.items(): - if key in ExifTags.TAGS: - if ExifTags.TAGS[key] == "Model": - camera_model = val - elif ExifTags.TAGS[key] == "DateTime": - shot_datetime = datetime.strptime(val, "%Y:%m:%d %H:%M:%S") + # im = Image.open(img) + # exif = im.getexif() + # + # camera_model = "" + # shot_datetime = None + # if exif: + # for key, val in exif.items(): + # if key in ExifTags.TAGS: + # if ExifTags.TAGS[key] == "Model": + # camera_model = val + # elif ExifTags.TAGS[key] == "DateTime": + # shot_datetime = datetime.strptime(val, "%Y:%m:%d %H:%M:%S") + + with exiftool.ExifTool() as et: + metadata = et.get_metadata([bytes(img)]) + for d in metadata: + iso = d["EXIF:ISO"] + f = d["EXIF:FNumber"] + exp = d["EXIF:ExposureTime"] + print(f"{iso} {f} {exp}") + + # with exiftool.ExifToolHelper() as et: + # metadata = et.get_metadata([bytes(img)]) + # for d in metadata: + # iso = d["EXIF:ISO"] + # f = d["EXIF:FNumber"] + # exp = d["EXIF:ExposureTime"] + # print(f"{iso} {f} {exp}") output = io.BytesIO() im.save(output, format="webp", lossless=False, quality=80) @@ -179,14 +196,14 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: uploadedAt=datetime.now()) output.seek(0) - upload_to_s3(photo.fileId, output.read()) - - write_json() - response = httpx.post(cloudflare_pages_deploy_hook_url) - if response.status_code == 200: - await update.message.reply_markdown_v2(text="trigger deploy succeed") - else: - await update.message.reply_markdown_v2("trigger deploy failed, status: {}".format(response.status_code)) + # upload_to_s3(photo.fileId, output.read()) + + # write_json() + # response = httpx.post(cloudflare_pages_deploy_hook_url) + # if response.status_code == 200: + # await update.message.reply_markdown_v2(text="trigger deploy succeed") + # else: + # await update.message.reply_markdown_v2("trigger deploy failed, status: {}".format(response.status_code)) except Exception: await update.message.reply_markdown_v2(text="Error:\n```{}```".format(traceback.format_exc())) diff --git a/test.py b/test.py new file mode 100644 index 0000000..ce1a747 --- /dev/null +++ b/test.py @@ -0,0 +1,22 @@ +from PIL import Image, ExifTags +from PIL.ExifTags import TAGS +from iptcinfo3 import IPTCInfo +import exiftool +import io + +im = Image.open("2023-03-10 17-49-50-00-IMG_8077.jpg") + +img_byte_arr = io.BytesIO() +im.save(img_byte_arr, format='webp') +img_byte_arr.seek(0) +# img_byte_arr = img_byte_arr.getvalue() + +with exiftool.ExifToolHelper() as et: + metadata = et.get_metadata([img_byte_arr.getvalue()], raw_bytes=True) + for d in metadata: + # print("{:20.20} {:20.20}".format(d["SourceFile"], + # d["EXIF:DateTimeOriginal"])) + iso = d["EXIF:ISO"] + f = d["EXIF:FNumber"] + exp = d["EXIF:ExposureTime"] + print(f"{iso} {f} {exp}") \ No newline at end of file -- cgit v1.2.3