Qurn commited on
Commit
a2ef5f1
1 Parent(s): a7726da

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import piexif
3
+ import piexif.helper
4
+ import json
5
+ from PIL import Image
6
+
7
+ IGNORED_INFO_KEYS = {
8
+ 'jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
9
+ 'loop', 'background', 'timestamp', 'duration', 'progressive', 'progression',
10
+ 'icc_profile', 'chromaticity', 'photoshop',
11
+ }
12
+
13
+ def read_info_from_image(image: Image.Image) -> tuple[str |None, dict]:
14
+ if image is None:
15
+ return "Please upload an image.", {} # Return an empty dict instead of None
16
+
17
+ items = (image.info or {}).copy()
18
+ geninfo = items.pop('parameters', None)
19
+
20
+ if "exif" in items:
21
+ exif_data = items["exif"]
22
+ try:
23
+ exif = piexif.load(exif_data)
24
+ except OSError:
25
+ exif = None
26
+ exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
27
+ try:
28
+ exif_comment = piexif.helper.UserComment.load(exif_comment)
29
+ except ValueError:
30
+ exif_comment = exif_comment.decode('utf8', errors="ignore")
31
+
32
+ if exif_comment:
33
+ items['exif comment'] = exif_comment
34
+ geninfo = exif_comment
35
+ elif "comment" in items:
36
+ geninfo = items["comment"].decode('utf8', errors="ignore")
37
+
38
+ for field in IGNORED_INFO_KEYS:
39
+ items.pop(field, None)
40
+
41
+ if items.get("Software", None) == "NovelAI":
42
+ try:
43
+ json_info = json.loads(items["Comment"])
44
+ sampler = "Euler a" # Removed sd_samplers import
45
+
46
+ geninfo = f"""{items["Description"]}
47
+ Negative prompt: {json_info["Negative Prompt"]}
48
+ Steps: {json_info["steps"]}, Sampler: {sampler}, CFG scale: {json_info["scale"]}, Seed: {json_info["seed"]}, Size: {image.width}x{image.height}, Clip skip: 2, ENSD: 31337"""
49
+ except Exception as e:
50
+ print(f"Error parsing NovelAI image generation parameters:")
51
+
52
+ return geninfo, items
53
+
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown(
56
+ """
57
+ # Image Exif Parser
58
+ [ref webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)\n
59
+ support png jpeg webp image format from images generated by AI tools.
60
+ """
61
+ )
62
+
63
+ with gr.Row():
64
+ with gr.Column():
65
+ input_image = gr.Image(sources=["upload", "clipboard"], label="Input Image", type="pil", height=680)
66
+
67
+ with gr.Column():
68
+ # output_metadata = gr.JSON(label="format metadata")
69
+ output_metadata = gr.Textbox(label="format metadata")
70
+ with gr.Accordion(open=True):
71
+ # output_exif = gr.JSON(label="exif comments")
72
+ output_exif = gr.Textbox(label="exif comments")
73
+
74
+ input_image.change(
75
+ fn=read_info_from_image,
76
+ inputs=input_image,
77
+ outputs=[output_metadata, output_exif],
78
+ )
79
+
80
+ gr.Examples(
81
+ examples=[
82
+ ["ex/0.png"],
83
+ ["ex/5.jpeg"],
84
+ ["ex/7.webp"],
85
+ ["ex/s.png"],
86
+ ],
87
+ inputs=input_image,
88
+ outputs=[output_metadata, output_exif],
89
+ fn=read_info_from_image,
90
+ cache_examples=False,
91
+ label="Exmaple format: png, jpeg, webp"
92
+ )
93
+
94
+ demo.launch()