Skip to content

Commit

Permalink
fix: allow reading of metadata from jpeg, jpg and webp again (lllyasv…
Browse files Browse the repository at this point in the history
…iel#3301)

also massively improves metadata read speed by switching from filepath (tempfile) to pil, which allows direct processing
  • Loading branch information
mashb1t authored Jul 17, 2024
1 parent f97adaf commit f597bf1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions modules/meta_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,8 @@ def get_metadata_parser(metadata_scheme: MetadataScheme) -> MetadataParser:
raise NotImplementedError


def read_info_from_image(filepath) -> tuple[str | None, MetadataScheme | None]:
with Image.open(filepath) as image:
items = (image.info or {}).copy()
def read_info_from_image(file) -> tuple[str | None, MetadataScheme | None]:
items = (file.info or {}).copy()

parameters = items.pop('parameters', None)
metadata_scheme = items.pop('fooocus_scheme', None)
Expand All @@ -615,7 +614,7 @@ def read_info_from_image(filepath) -> tuple[str | None, MetadataScheme | None]:
if parameters is not None and is_json(parameters):
parameters = json.loads(parameters)
elif exif is not None:
exif = image.getexif()
exif = file.getexif()
# 0x9286 = UserComment
parameters = exif.get(0x9286, None)
# 0x927C = MakerNote
Expand Down
10 changes: 5 additions & 5 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ def trigger_show_image_properties(image):

with gr.TabItem(label='Metadata') as metadata_tab:
with gr.Column():
metadata_input_image = grh.Image(label='For images created by Fooocus', source='upload', type='filepath')
metadata_input_image = grh.Image(label='For images created by Fooocus', source='upload', type='pil')
metadata_json = gr.JSON(label='Metadata')
metadata_import_button = gr.Button(value='Apply Metadata')

def trigger_metadata_preview(filepath):
parameters, metadata_scheme = modules.meta_parser.read_info_from_image(filepath)
def trigger_metadata_preview(file):
parameters, metadata_scheme = modules.meta_parser.read_info_from_image(file)

results = {}
if parameters is not None:
Expand Down Expand Up @@ -995,8 +995,8 @@ def parse_meta(raw_prompt_txt, is_generating):

load_parameter_button.click(modules.meta_parser.load_parameter_button_click, inputs=[prompt, state_is_generating, inpaint_mode], outputs=load_data_outputs, queue=False, show_progress=False)

def trigger_metadata_import(filepath, state_is_generating):
parameters, metadata_scheme = modules.meta_parser.read_info_from_image(filepath)
def trigger_metadata_import(file, state_is_generating):
parameters, metadata_scheme = modules.meta_parser.read_info_from_image(file)
if parameters is None:
print('Could not find metadata in the image!')
parsed_parameters = {}
Expand Down

0 comments on commit f597bf1

Please sign in to comment.