Spaces:
Sleeping
Sleeping
File size: 4,130 Bytes
e05b0c4 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 d38e53e 2e01db0 2f98247 2e01db0 2f98247 04cb461 2f98247 04cb461 2f98247 |
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 |
import streamlit as st
import cv2
import numpy as np
from io import BytesIO
from PIL import Image
import base64
from fpdf import FPDF
import os
# Define augmentation functions
def rotate_image(image, angle):
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h))
return rotated_image
def add_noise(image):
s_vs_p = 0.5
amount = 0.02
noisy_image = image.copy()
num_salt = np.ceil(amount * image.size * s_vs_p)
num_pepper = np.ceil(amount * image.size * (1.0 - s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape[:2]]
noisy_image[coords[0], coords[1], :] = 255
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape[:2]]
noisy_image[coords[0], coords[1], :] = 0
return noisy_image
def blur_image(image, kernel_size):
blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
return blurred_image
def translate_image(image, tx, ty):
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
height, width = image.shape[:2]
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
return translated_image
# Download functions
def download_image(image, file_format):
if file_format.lower() == "jpg":
file_format = "jpeg"
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
buffer = BytesIO()
pil_image.save(buffer, format=file_format.upper())
buffer.seek(0)
b64 = base64.b64encode(buffer.read()).decode()
href = f'<a href="data:file/{file_format};base64,{b64}" download="augmented_image.{file_format.lower()}">Download {file_format.upper()} File</a>'
return href
def download_pdf(image):
pdf = FPDF()
pdf.add_page()
temp_image_path = "temp_image.jpg"
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
pil_image.save(temp_image_path, format="JPEG")
pdf.image(temp_image_path, x=10, y=10, w=190)
os.remove(temp_image_path)
pdf_output = BytesIO()
pdf.output(pdf_output, "F")
pdf_output.seek(0)
b64 = base64.b64encode(pdf_output.read()).decode()
href = f'<a href="data:application/octet-stream;base64,{b64}" download="augmented_image.pdf">Download PDF</a>'
return href
# Streamlit app
st.title("Image Augmentation Tool")
# File upload
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
if uploaded_file:
image = Image.open(uploaded_file)
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
st.image(image, caption="Uploaded Image", use_column_width=True)
st.write("### Select Transformations to Apply:")
rotate = st.checkbox("Rotate (45 degrees)")
noise = st.checkbox("Add Noise")
blur = st.checkbox("Blur (5x5 Kernel)")
translate = st.checkbox("Translate (50 pixels right and down)")
transformed_image = None # Initialize transformed_image
if st.button("Apply Transformations"):
transformed_image = image.copy()
if rotate:
transformed_image = rotate_image(transformed_image, 45)
if noise:
transformed_image = add_noise(transformed_image)
if blur:
transformed_image = blur_image(transformed_image, 5)
if translate:
transformed_image = translate_image(transformed_image, 50, 50)
st.image(cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB), caption="Transformed Image", use_column_width=True)
# Provide download options after transformations
if transformed_image is not None:
st.markdown("### Download Transformed Image:")
download_format = st.radio("Select Download Format:", ["JPG", "PNG", "PDF"], key="download_format")
if download_format == "JPG" or download_format == "PNG":
st.markdown(download_image(transformed_image, download_format), unsafe_allow_html=True)
elif download_format == "PDF":
st.markdown(download_pdf(transformed_image), unsafe_allow_html=True)
|