lyimo commited on
Commit
c09c63f
·
verified ·
1 Parent(s): 4ef473f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import hashlib
3
+ import qrcode
4
+ from PIL import Image, ImageDraw
5
+ import numpy as np
6
+ import io
7
+
8
+ # Function to hash image data
9
+ def hash_image(image):
10
+ image_data = image.tobytes()
11
+ image_hash = hashlib.sha256(image_data).hexdigest()
12
+ return image_hash
13
+
14
+ # Function to generate QR code
15
+ def generate_qr_code(data):
16
+ qr = qrcode.QRCode(
17
+ version=1,
18
+ error_correction=qrcode.constants.ERROR_CORRECT_L,
19
+ box_size=10,
20
+ border=4,
21
+ )
22
+ qr.add_data(data)
23
+ qr.make(fit=True)
24
+ qr_img = qr.make_image(fill='black', back_color='white')
25
+ return qr_img
26
+
27
+ def embed_qr_code(image, qr_img):
28
+ if isinstance(image, np.ndarray):
29
+ image = Image.fromarray(image.astype('uint8')) # Convert NumPy array to PIL Image
30
+ image.paste(qr_img, (10, 10)) # Adjust position as needed
31
+ return image
32
+
33
+ # Function to save hash to file
34
+ def save_hash(hash_code, description=""):
35
+ with open("hash.txt", "a") as file:
36
+ file.write(f"{hash_code}: {description}\n")
37
+
38
+ # Function to check image authenticity
39
+ def check_authenticity(image):
40
+ hash_code = hash_image(image)
41
+ try:
42
+ with open("hash.txt", "r") as file:
43
+ hashes = file.readlines()
44
+ for line in hashes:
45
+ parts = line.strip().split(': ', 1)
46
+ if len(parts) == 2:
47
+ saved_hash, description = parts
48
+ if saved_hash == hash_code:
49
+ return f"Image is authentic. Description: {description}"
50
+ else:
51
+ saved_hash = parts[0]
52
+ if saved_hash == hash_code:
53
+ return "Image is authentic, but no description provided."
54
+ except FileNotFoundError:
55
+ return "Hash file not found. Please process an image first."
56
+ return "Image is new or modified."
57
+
58
+ def process_image(image, description):
59
+ image_format = image.format if hasattr(image, 'format') else 'PNG' # Default to PNG if format cannot be detected
60
+
61
+ hash_code1 = hash_image(image)
62
+ qr_img = generate_qr_code(hash_code1)
63
+ qr_img = qr_img.resize((100, 100)) # Resize QR code as needed
64
+ image_with_qr = embed_qr_code(image, qr_img)
65
+ save_hash(hash_code1, description)
66
+ hash_code2 = hash_image(image_with_qr)
67
+ save_hash(hash_code2)
68
+
69
+ buffer = io.BytesIO()
70
+ image_with_qr.save(buffer, format=image_format)
71
+ buffer.seek(0)
72
+ return buffer.getvalue(), "Image processed and hashes stored."
73
+
74
+ st.title('Image Authenticity and QR Code Embedding')
75
+
76
+ uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
77
+ description = st.text_input("Enter a description for the image:")
78
+
79
+ if uploaded_file is not None:
80
+ image = Image.open(uploaded_file)
81
+ if st.button('Process Image'):
82
+ processed_image_data, message = process_image(image, description)
83
+ st.image(processed_image_data, caption='Processed Image')
84
+ st.success(message)
85
+
86
+ if st.button('Check Authenticity'):
87
+ if uploaded_file is not None:
88
+ result = check_authenticity(image)
89
+ st.info(result)
90
+ else:
91
+ st.error("Please upload an image first.")