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