Make compatible with MacOS ARM
This commit is contained in:
+9
-6
@@ -5,7 +5,7 @@ import re
|
||||
import json
|
||||
import time
|
||||
import numpy as np
|
||||
import pyexiv2
|
||||
import piexif
|
||||
from PIL.PngImagePlugin import PngInfo
|
||||
from PIL import Image, PngImagePlugin
|
||||
from pathlib import Path
|
||||
@@ -207,11 +207,14 @@ class PrimereMetaSave:
|
||||
else:
|
||||
img.save(output_file, quality=quality, optimize=True)
|
||||
if image_embed_exif == True:
|
||||
metadata = pyexiv2.Image(output_file)
|
||||
# if exif_metadata_A11 is not None:
|
||||
# metadata.modify_exif({'Exif.Photo.UserComment': 'charset=Unicode ' + exif_metadata_A11})
|
||||
metadata.modify_exif({'Exif.Image.ImageDescription': json.dumps(exif_metadata_json)})
|
||||
print(f"Image file saved with exif: {output_file}")
|
||||
try:
|
||||
exif_dict = piexif.load(output_file)
|
||||
exif_dict["Exif"][piexif.ExifIFD.UserComment] = piexif.helper.UserComment.dump(json.dumps(exif_metadata_json), encoding="unicode")
|
||||
exif_bytes = piexif.dump(exif_dict)
|
||||
piexif.insert(exif_bytes, output_file)
|
||||
print(f"Image file saved with exif: {output_file}")
|
||||
except Exception as e:
|
||||
print(f"Error saving EXIF data: {e}")
|
||||
else:
|
||||
if extension == 'webp':
|
||||
img.save(output_file, quality=quality, exif=metadata)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
|
||||
import piexif
|
||||
import pyexiv2
|
||||
#import pyexiv2
|
||||
import piexif.helper
|
||||
from PIL import Image
|
||||
|
||||
@@ -29,43 +29,61 @@ class ImageExifReader:
|
||||
return False
|
||||
return True
|
||||
|
||||
with Image.open(file) as f:
|
||||
p2metadata = pyexiv2.Image(file)
|
||||
is_primere = p2metadata.read_exif()
|
||||
if 'Exif.Image.ImageDescription' in is_primere:
|
||||
primere_exif_string = is_primere.get('Exif.Image.ImageDescription').strip()
|
||||
self._original = primere_exif_string
|
||||
if is_json(primere_exif_string) == True:
|
||||
json_object = json.loads(primere_exif_string)
|
||||
# keysList = {'positive', 'negative', 'positive_l', 'negative_l', 'positive_r', 'negative_r', 'seed', 'model_hash', 'model_name', 'sampler_name'}
|
||||
# if not (keysList - json_object.keys()):
|
||||
self._tool = "Primere"
|
||||
self._parser = Primere(info=json_object)
|
||||
else:
|
||||
if f.format == "PNG":
|
||||
self._original = f.info
|
||||
if "parameters" in f.info:
|
||||
self._tool = "Automatic1111"
|
||||
self._parser = Automatic1111(info=f.info)
|
||||
elif "prompt" in f.info:
|
||||
self._tool = "ComfyUI"
|
||||
try:
|
||||
self._parser = ComfyUI(info=f.info)
|
||||
except Exception:
|
||||
self._parser = {}
|
||||
def read_data(self, file):
|
||||
def is_json(jsoninput):
|
||||
try:
|
||||
json.loads(jsoninput)
|
||||
except ValueError as e:
|
||||
return False
|
||||
return True
|
||||
|
||||
elif f.format == "JPEG" or f.format == "WEBP":
|
||||
with Image.open(file) as f:
|
||||
# Remove pyexiv2 usage
|
||||
# p2metadata = pyexiv2.Image(file)
|
||||
# is_primere = p2metadata.read_exif()
|
||||
# if 'Exif.Image.ImageDescription' in is_primere:
|
||||
# primere_exif_string = is_primere.get('Exif.Image.ImageDescription').strip()
|
||||
# self._original = primere_exif_string
|
||||
# if is_json(primere_exif_string) == True:
|
||||
# json_object = json.loads(primere_exif_string)
|
||||
# self._tool = "Primere"
|
||||
# self._parser = Primere(info=json_object)
|
||||
|
||||
# Use piexif to check for Primere metadata
|
||||
exif_dict = piexif.load(f.info.get("exif", b""))
|
||||
user_comment = piexif.helper.UserComment.load(exif_dict.get("Exif", {}).get(piexif.ExifIFD.UserComment, b""))
|
||||
if user_comment:
|
||||
self._original = user_comment.decode()
|
||||
if is_json(self._original):
|
||||
json_object = json.loads(self._original)
|
||||
self._tool = "Primere"
|
||||
self._parser = Primere(info=json_object)
|
||||
|
||||
else:
|
||||
if f.format == "PNG":
|
||||
self._original = f.info
|
||||
if "parameters" in f.info:
|
||||
self._tool = "Automatic1111"
|
||||
self._parser = Automatic1111(info=f.info)
|
||||
elif "prompt" in f.info:
|
||||
self._tool = "ComfyUI"
|
||||
try:
|
||||
exif = piexif.load(f.info.get("exif")) or {}
|
||||
self._raw = piexif.helper.UserComment.load(
|
||||
exif.get("Exif").get(piexif.ExifIFD.UserComment)
|
||||
)
|
||||
self._original = self._raw
|
||||
if is_json(self._raw) != True:
|
||||
self._tool = "Automatic1111"
|
||||
self._parser = Automatic1111(raw=self._raw)
|
||||
self._parser = ComfyUI(info=f.info)
|
||||
except Exception:
|
||||
print('Exif data cannot read from selected image.')
|
||||
self._parser = {}
|
||||
|
||||
elif f.format == "JPEG" or f.format == "WEBP":
|
||||
try:
|
||||
exif = piexif.load(f.info.get("exif")) or {}
|
||||
self._raw = piexif.helper.UserComment.load(
|
||||
exif.get("Exif").get(piexif.ExifIFD.UserComment)
|
||||
)
|
||||
self._original = self._raw
|
||||
if is_json(self._raw) != True:
|
||||
self._tool = "Automatic1111"
|
||||
self._parser = Automatic1111(raw=self._raw)
|
||||
except Exception:
|
||||
print('Exif data cannot read from selected image.')
|
||||
|
||||
@property
|
||||
def parser(self):
|
||||
|
||||
Reference in New Issue
Block a user