Spaces:
Runtime error
Runtime error
File size: 8,338 Bytes
b3cc385 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
import streamlit as st
import numpy as np
from PIL import Image
import io
import tempfile
import os
def main():
st.title("Digital Image Processing System")
st.header("User Information")
name = st.text_input("Enter your name")
reg_no = st.text_input("Enter your registration number (Format: 2000-AG-1000)")
is_valid_reg = False
if reg_no:
import re
pattern = r'^\d{4}-[aA][gG]-\d{4}$'
is_valid_reg = bool(re.match(pattern, reg_no))
if not is_valid_reg:
st.error("Please enter a valid registration number in the format 2000-AG-1000")
st.header("Upload Images")
col1, col2 = st.columns(2)
with col1:
uploaded_file1 = st.file_uploader("Choose first image...", type=["jpg", "jpeg", "png"])
with col2:
uploaded_file2 = st.file_uploader("Choose second image...", type=["jpg", "jpeg", "png"])
if uploaded_file1 is not None and uploaded_file2 is not None and name and is_valid_reg:
image1 = Image.open(uploaded_file1).convert('RGB')
image2 = Image.open(uploaded_file2).convert('RGB')
image2 = image2.resize(image1.size)
img_array1 = np.array(image1)
img_array2 = np.array(image2)
st.header("Original Images")
col1, col2 = st.columns(2)
with col1:
st.image(image1, caption="Image One", use_column_width=True)
with col2:
st.image(image2, caption="Image Two", use_column_width=True)
st.header("Image Operations")
select_all = st.checkbox("Select All Operations")
addition = st.checkbox("Addition", value=select_all)
subtraction = st.checkbox("Subtraction", value=select_all)
multiplication = st.checkbox("Multiplication", value=select_all)
division = st.checkbox("Division", value=select_all)
weight = st.slider("Weight factor for Image One (0 to 1)", 0.0, 1.0, 0.5, step=0.1)
if st.button("Process Images"):
if not (addition or subtraction or multiplication or division):
st.error("Please select at least one operation")
else:
processed_images = {}
if addition:
processed_images["Addition"] = apply_operation_two_images(img_array1, img_array2, "Addition", weight)
if subtraction:
processed_images["Subtraction"] = apply_operation_two_images(img_array1, img_array2, "Subtraction", weight)
if multiplication:
processed_images["Multiplication"] = apply_operation_two_images(img_array1, img_array2, "Multiplication", weight)
if division:
processed_images["Division"] = apply_operation_two_images(img_array1, img_array2, "Division", weight)
for operation, result_img in processed_images.items():
st.subheader(f"{operation} Result")
col1, col2, col3 = st.columns(3)
with col1:
st.image(image1, caption="Image One", use_column_width=True)
with col2:
st.image(image2, caption="Image Two", use_column_width=True)
with col3:
st.image(result_img, caption="Result", use_column_width=True)
try:
with st.spinner("Generating PDF..."):
pdf_path = create_two_image_pdf(name, reg_no, img_array1, img_array2, processed_images)
with open(pdf_path, "rb") as f:
pdf_bytes = f.read()
os.remove(pdf_path)
st.success("Processing complete! Download your PDF to view results.")
st.download_button(
label="Download as PDF",
data=pdf_bytes,
file_name=f"{reg_no}_image_processing.pdf",
mime="application/pdf"
)
except Exception as e:
st.error(f"Error generating PDF: {str(e)}")
def apply_operation_two_images(img_array1, img_array2, operation, weight=0.5):
img1_float = img_array1.astype(np.float32)
img2_float = img_array2.astype(np.float32)
if operation == "Addition":
result = np.clip(weight * img1_float + (1 - weight) * img2_float, 0, 255)
elif operation == "Subtraction":
result = np.clip(weight * img1_float - (1 - weight) * img2_float, 0, 255)
elif operation == "Multiplication":
result = np.clip((img1_float * img2_float) / 255.0, 0, 255)
elif operation == "Division":
epsilon = 1e-10
result = np.clip(img1_float / (img2_float + epsilon) * 127.5, 0, 255)
return result.astype(np.uint8)
def create_two_image_pdf(name, reg_no, img_array1, img_array2, processed_images):
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.pdf')
temp_file.close()
pdf_path = temp_file.name
with tempfile.TemporaryDirectory() as temp_dir:
img1_path = os.path.join(temp_dir, "image1.jpg")
img2_path = os.path.join(temp_dir, "image2.jpg")
Image.fromarray(img_array1).save(img1_path, format="JPEG")
Image.fromarray(img_array2).save(img2_path, format="JPEG")
processed_paths = {}
for op_name, img_array in processed_images.items():
img_path = os.path.join(temp_dir, f"{op_name.lower()}.jpg")
Image.fromarray(img_array).save(img_path, format="JPEG")
processed_paths[op_name] = img_path
c = canvas.Canvas(pdf_path, pagesize=letter)
width, height = letter
c.setFont("Helvetica-Bold", 16)
c.drawString(50, height - 50, "Digital Image Processing Report")
c.setFont("Helvetica", 12)
c.drawString(50, height - 80, f"Name: {name}")
c.drawString(50, height - 100, f"Registration Number: {reg_no}")
c.drawString(50, height - 130, "Original Images:")
c.drawImage(img1_path, 50, height - 330, width=(width-100)/2, height=180, preserveAspectRatio=True)
c.drawString(50, height - 350, "Image One")
c.drawImage(img2_path, width/2, height - 330, width=(width-100)/2, height=180, preserveAspectRatio=True)
c.drawString(width/2, height - 350, "Image Two")
for operation, img_path in processed_paths.items():
c.showPage()
c.setFont("Helvetica-Bold", 16)
c.drawString(50, height - 50, "Digital Image Processing Report")
c.setFont("Helvetica", 14)
c.drawString(50, height - 80, f"{operation} Operation:")
img_width = (width-120)/3
img_height = 180
c.drawImage(img1_path, 50, height - 280, width=img_width, height=img_height, preserveAspectRatio=True)
c.setFont("Helvetica", 10)
c.drawString(50, height - 300, "Image One")
c.drawImage(img2_path, 60 + img_width, height - 280, width=img_width, height=img_height, preserveAspectRatio=True)
c.drawString(60 + img_width, height - 300, "Image Two")
c.drawImage(img_path, 70 + img_width*2, height - 280, width=img_width, height=img_height, preserveAspectRatio=True)
c.drawString(70 + img_width*2, height - 300, "Result")
c.save()
return pdf_path
if __name__ == "__main__":
main() |