-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kolacik33
committed
Apr 9, 2024
1 parent
75b3361
commit d927cbe
Showing
5 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ reportlab | |
beautifulsoup4 | ||
toml | ||
pyyaml | ||
pygments==2.16.1 | ||
pygments==2.16.1 | ||
piexif |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from PIL import Image, ExifTags | ||
import piexif | ||
import json | ||
from enum import Enum | ||
|
||
class Metadata(Enum): | ||
DIR_SETUP = 'DirSetup' | ||
DIR_DATA = 'DirData' | ||
GIT_COMMIT = 'GitCommit' | ||
GIT_BRANCH = 'GitBranch' | ||
DATETIME = 'DateTime' | ||
SETTINGS = 'Settings' | ||
|
||
class ImageMetadata: | ||
|
||
modified:bool = False | ||
file_name:str | ||
metadata:dict | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, exc_traceback): | ||
if self.modified: | ||
self.save() | ||
|
||
def __init__(self, file_name:str): | ||
self.file_name = file_name | ||
with Image.open(file_name) as img: | ||
self.metadata = {} | ||
if 'exif' in img.info: | ||
metadata = piexif.load(img.info['exif']) | ||
if piexif.ExifIFD.UserComment in metadata['Exif']: | ||
comments = metadata['Exif'][piexif.ExifIFD.UserComment].decode() | ||
self.metadata = json.loads(comments) | ||
|
||
def __setitem__(self, key:str, value:str): | ||
self.set(key,value) | ||
|
||
def __getitem__(self, key:str): | ||
return self.get(key) | ||
|
||
def set(self, key:Metadata, value:str): | ||
self.metadata[key.value] = value | ||
self.modified = True | ||
|
||
def get(self, key:Metadata): | ||
return self.metadata[key.value] | ||
|
||
def save(self): | ||
img = Image.open(self.file_name) | ||
if 'exif' in img.info: | ||
metadata = piexif.load(img.info['exif']) | ||
else: | ||
metadata = { | ||
"0th": {}, | ||
"Exif": {}, | ||
"1st": {}, | ||
"thumbnail": None, | ||
"GPS": {} | ||
} | ||
metadata['Exif'][piexif.ExifIFD.UserComment] = json.dumps(self.metadata).encode() | ||
img.save(self.file_name, img.format, exif=piexif.dump(metadata)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import numpy as np | ||
import os | ||
import pytest | ||
from PIL import Image | ||
import piexif | ||
import sys | ||
sys.path.insert(0, 'src') | ||
|
||
from scinumtools import ImageMetadata, Metadata | ||
|
||
@pytest.fixture() | ||
def temp_file(): | ||
dir_temp = "tmp" | ||
name_temp = "thumbnail.png" | ||
file_name = f"{dir_temp}/{name_temp}" | ||
if not os.path.isdir(dir_temp): | ||
os.mkdir(dir_temp) | ||
data = np.full((10,10),1) | ||
with Image.fromarray(data.astype('uint8')) as im: | ||
im.save(file_name) | ||
return file_name | ||
|
||
def test_read_exif(temp_file): | ||
assert os.path.isfile(temp_file) | ||
|
||
with ImageMetadata(temp_file) as im: | ||
im.set(Metadata.DATETIME,'World') | ||
|
||
with ImageMetadata(temp_file) as im: | ||
assert im.get(Metadata.DATETIME) == 'World' |