encrypt / app.py
lyimo's picture
Update app.py
19c1c45 verified
import streamlit as st
import hashlib
from PIL import Image
import io
# Function to hash image data
def hash_image(image):
image_data = image.tobytes()
image_hash = hashlib.sha256(image_data).hexdigest()
return image_hash
# Function to save hash to file
def save_hash(hash_code, description=""):
with open("hash.txt", "a") as file:
file.write(f"{hash_code}: {description}\n")
# Function to check image authenticity
def check_authenticity(image):
hash_code = hash_image(image)
try:
with open("hash.txt", "r") as file:
hashes = file.readlines()
for line in hashes:
parts = line.strip().split(': ', 1)
if len(parts) == 2:
saved_hash, description = parts
if saved_hash == hash_code:
return f"Image is authentic. Description: {description}"
else:
saved_hash = parts[0]
if saved_hash == hash_code:
return "Image is authentic, but no description provided."
except FileNotFoundError:
return "Hash file not found. Please process an image first."
return "Image is new or modified."
def process_image(image, description):
image_format = image.format if hasattr(image, 'format') else 'PNG' # Default to PNG if format cannot be detected
hash_code = hash_image(image)
save_hash(hash_code, description)
return "Image hash computed and stored."
st.title('Image Authenticity Checker')
uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
description = st.text_input("Enter a description for the image:")
if uploaded_file is not None:
image = Image.open(uploaded_file)
if st.button('Process Image'):
message = process_image(image, description)
st.success(message)
if st.button('Check Authenticity'):
if uploaded_file is not None:
result = check_authenticity(image)
st.info(result)
else:
st.error("Please upload an image first.")