File size: 2,079 Bytes
19c1c45
915a80d
f5bf9cd
0be566a
664f2dc
caf264c
 
 
 
 
915a80d
caf264c
 
 
 
4acc6af
caf264c
 
 
3015beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caf264c
4acc6af
caf264c
19c1c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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.")