Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import hashlib
|
3 |
-
import
|
4 |
-
from PIL import Image, ImageDraw
|
5 |
-
import numpy as np
|
6 |
import io
|
7 |
|
8 |
# Function to hash image data
|
@@ -11,25 +9,6 @@ def hash_image(image):
|
|
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:
|
@@ -57,21 +36,11 @@ def check_authenticity(image):
|
|
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 |
-
|
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:")
|
@@ -79,8 +48,7 @@ description = st.text_input("Enter a description for the image:")
|
|
79 |
if uploaded_file is not None:
|
80 |
image = Image.open(uploaded_file)
|
81 |
if st.button('Process Image'):
|
82 |
-
|
83 |
-
st.image(processed_image_data, caption='Processed Image')
|
84 |
st.success(message)
|
85 |
|
86 |
if st.button('Check Authenticity'):
|
|
|
1 |
import streamlit as st
|
2 |
import hashlib
|
3 |
+
from PIL import Image
|
|
|
|
|
4 |
import io
|
5 |
|
6 |
# Function to hash image data
|
|
|
9 |
image_hash = hashlib.sha256(image_data).hexdigest()
|
10 |
return image_hash
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Function to save hash to file
|
13 |
def save_hash(hash_code, description=""):
|
14 |
with open("hash.txt", "a") as file:
|
|
|
36 |
|
37 |
def process_image(image, description):
|
38 |
image_format = image.format if hasattr(image, 'format') else 'PNG' # Default to PNG if format cannot be detected
|
39 |
+
hash_code = hash_image(image)
|
40 |
+
save_hash(hash_code, description)
|
41 |
+
return "Image hash computed and stored."
|
42 |
|
43 |
+
st.title('Image Authenticity Checker')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
|
46 |
description = st.text_input("Enter a description for the image:")
|
|
|
48 |
if uploaded_file is not None:
|
49 |
image = Image.open(uploaded_file)
|
50 |
if st.button('Process Image'):
|
51 |
+
message = process_image(image, description)
|
|
|
52 |
st.success(message)
|
53 |
|
54 |
if st.button('Check Authenticity'):
|