Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from PIL import Image, ImageDraw
|
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
# Function to hash image data
|
| 10 |
def hash_image(image):
|
|
@@ -60,24 +61,41 @@ def check_authenticity(image):
|
|
| 60 |
return "Image is new or modified."
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
| 63 |
def process_image(image, description):
|
|
|
|
|
|
|
|
|
|
| 64 |
hash_code1 = hash_image(image)
|
| 65 |
qr_img = generate_qr_code(hash_code1)
|
| 66 |
qr_img = qr_img.resize((100, 100)) # Resize QR code as needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
image_with_qr = embed_qr_code(image, qr_img)
|
| 68 |
save_hash(hash_code1, description)
|
| 69 |
hash_code2 = hash_image(image_with_qr)
|
| 70 |
save_hash(hash_code2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return image_with_qr, "Image processed and hashes stored."
|
| 72 |
|
| 73 |
-
#
|
| 74 |
with gr.Blocks() as app:
|
| 75 |
with gr.Tab("Upload and Process Image"):
|
| 76 |
with gr.Row():
|
| 77 |
-
image_input = gr.Image(label="Upload Image")
|
| 78 |
description_input = gr.Textbox(label="Description")
|
| 79 |
submit_button = gr.Button("Process Image")
|
| 80 |
-
image_output = gr.Image(label="Processed Image")
|
| 81 |
message_output = gr.Textbox(label="Status Message")
|
| 82 |
submit_button.click(
|
| 83 |
process_image,
|
|
@@ -87,10 +105,11 @@ with gr.Blocks() as app:
|
|
| 87 |
|
| 88 |
with gr.Tab("Check Image Authenticity"):
|
| 89 |
with gr.Row():
|
| 90 |
-
image_check_input = gr.Image(label="Upload Image to Verify")
|
| 91 |
check_button = gr.Button("Check Authenticity")
|
| 92 |
authenticity_output = gr.Textbox(label="Result")
|
| 93 |
check_button.click(check_authenticity, inputs=[image_check_input], outputs=authenticity_output)
|
| 94 |
|
| 95 |
# Launch the application
|
| 96 |
app.launch()
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
+
import io
|
| 9 |
|
| 10 |
# Function to hash image data
|
| 11 |
def hash_image(image):
|
|
|
|
| 61 |
return "Image is new or modified."
|
| 62 |
|
| 63 |
|
| 64 |
+
|
| 65 |
+
|
| 66 |
def process_image(image, description):
|
| 67 |
+
# Determine the image format
|
| 68 |
+
image_format = image.format if image.format else 'PNG' # Default to PNG if format cannot be detected
|
| 69 |
+
|
| 70 |
hash_code1 = hash_image(image)
|
| 71 |
qr_img = generate_qr_code(hash_code1)
|
| 72 |
qr_img = qr_img.resize((100, 100)) # Resize QR code as needed
|
| 73 |
+
|
| 74 |
+
# Convert image to PIL Image if it's not one already (necessary if the image input comes as a numpy array)
|
| 75 |
+
if not isinstance(image, Image.Image):
|
| 76 |
+
image = Image.open(io.BytesIO(image))
|
| 77 |
+
|
| 78 |
image_with_qr = embed_qr_code(image, qr_img)
|
| 79 |
save_hash(hash_code1, description)
|
| 80 |
hash_code2 = hash_image(image_with_qr)
|
| 81 |
save_hash(hash_code2)
|
| 82 |
+
|
| 83 |
+
# Save the processed image in the original format
|
| 84 |
+
buffer = io.BytesIO()
|
| 85 |
+
image_with_qr.save(buffer, format=image_format)
|
| 86 |
+
buffer.seek(0)
|
| 87 |
+
image_with_qr = Image.open(buffer) # Reopen the image from buffer to return it properly through Gradio
|
| 88 |
+
|
| 89 |
return image_with_qr, "Image processed and hashes stored."
|
| 90 |
|
| 91 |
+
# Adjust the Gradio block as needed
|
| 92 |
with gr.Blocks() as app:
|
| 93 |
with gr.Tab("Upload and Process Image"):
|
| 94 |
with gr.Row():
|
| 95 |
+
image_input = gr.Image(label="Upload Image", type="pil")
|
| 96 |
description_input = gr.Textbox(label="Description")
|
| 97 |
submit_button = gr.Button("Process Image")
|
| 98 |
+
image_output = gr.Image(label="Processed Image", type="pil")
|
| 99 |
message_output = gr.Textbox(label="Status Message")
|
| 100 |
submit_button.click(
|
| 101 |
process_image,
|
|
|
|
| 105 |
|
| 106 |
with gr.Tab("Check Image Authenticity"):
|
| 107 |
with gr.Row():
|
| 108 |
+
image_check_input = gr.Image(label="Upload Image to Verify", type="pil")
|
| 109 |
check_button = gr.Button("Check Authenticity")
|
| 110 |
authenticity_output = gr.Textbox(label="Result")
|
| 111 |
check_button.click(check_authenticity, inputs=[image_check_input], outputs=authenticity_output)
|
| 112 |
|
| 113 |
# Launch the application
|
| 114 |
app.launch()
|
| 115 |
+
|