|
import gradio as gr |
|
import hashlib |
|
import qrcode |
|
from PIL import Image, ImageDraw |
|
import os |
|
|
|
|
|
def hash_image(image): |
|
image_data = image.tobytes() |
|
image_hash = hashlib.sha256(image_data).hexdigest() |
|
return image_hash |
|
|
|
|
|
def generate_qr_code(data): |
|
qr = qrcode.QRCode( |
|
version=1, |
|
error_correction=qrcode.constants.ERROR_CORRECT_L, |
|
box_size=10, |
|
border=4, |
|
) |
|
qr.add_data(data) |
|
qr.make(fit=True) |
|
qr_img = qr.make_image(fill='black', back_color='white') |
|
return qr_img |
|
|
|
|
|
def embed_qr_code(image, qr_img): |
|
image.paste(qr_img, (10, 10)) |
|
return image |
|
|
|
|
|
def save_hash(hash_code, description=""): |
|
with open("hash.txt", "a") as file: |
|
file.write(f"{hash_code}: {description}\n") |
|
|
|
|
|
def check_authenticity(image): |
|
hash_code = hash_image(image) |
|
with open("hash.txt", "r") as file: |
|
hashes = file.readlines() |
|
for line in hashes: |
|
saved_hash, description = line.strip().split(': ', 1) |
|
if saved_hash == hash_code: |
|
return f"Image is authentic. Description: {description}" |
|
return "Image is new or modified." |
|
|
|
|
|
def process_image(image, description): |
|
hash_code1 = hash_image(image) |
|
qr_img = generate_qr_code(hash_code1) |
|
qr_img = qr_img.resize((100, 100)) |
|
image_with_qr = embed_qr_code(image, qr_img) |
|
save_hash(hash_code1, description) |
|
hash_code2 = hash_image(image_with_qr) |
|
save_hash(hash_code2) |
|
return image_with_qr, "Image processed and hashes stored." |
|
|
|
|
|
with gr.Blocks() as app: |
|
with gr.Tab("Upload and Process Image"): |
|
with gr.Row(): |
|
image_input = gr.Image(label="Upload Image") |
|
description_input = gr.Textbox(label="Description") |
|
submit_button = gr.Button("Process Image") |
|
image_output = gr.Image(label="Processed Image") |
|
submit_button.click(process_image, inputs=[image_input, description_input], outputs=image_output) |
|
|
|
with gr.Tab("Check Image Authenticity"): |
|
with gr.Row(): |
|
image_check_input = gr.Image(label="Upload Image to Verify") |
|
check_button = gr.Button("Check Authenticity") |
|
authenticity_output = gr.Textbox(label="Result") |
|
check_button.click(check_authenticity, inputs=[image_check_input], outputs=authenticity_output) |
|
|
|
|
|
app.launch() |