Spaces:
Sleeping
Sleeping
Update Image Augmentation.py
Browse files- Image Augmentation.py +20 -15
Image Augmentation.py
CHANGED
|
@@ -5,8 +5,9 @@ from io import BytesIO
|
|
| 5 |
from PIL import Image
|
| 6 |
import base64
|
| 7 |
from fpdf import FPDF
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
def rotate_image(image, angle):
|
| 11 |
(h, w) = image.shape[:2]
|
| 12 |
center = (w // 2, h // 2)
|
|
@@ -14,7 +15,6 @@ def rotate_image(image, angle):
|
|
| 14 |
rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h))
|
| 15 |
return rotated_image
|
| 16 |
|
| 17 |
-
|
| 18 |
def add_noise(image):
|
| 19 |
s_vs_p = 0.5
|
| 20 |
amount = 0.02
|
|
@@ -27,45 +27,48 @@ def add_noise(image):
|
|
| 27 |
noisy_image[coords[0], coords[1], :] = 0
|
| 28 |
return noisy_image
|
| 29 |
|
| 30 |
-
|
| 31 |
def blur_image(image, kernel_size):
|
| 32 |
blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
|
| 33 |
return blurred_image
|
| 34 |
|
| 35 |
-
|
| 36 |
def translate_image(image, tx, ty):
|
| 37 |
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 38 |
height, width = image.shape[:2]
|
| 39 |
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
|
| 40 |
return translated_image
|
| 41 |
|
| 42 |
-
|
| 43 |
def download_image(image, file_format):
|
|
|
|
|
|
|
| 44 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 45 |
buffer = BytesIO()
|
| 46 |
pil_image.save(buffer, format=file_format.upper())
|
| 47 |
buffer.seek(0)
|
| 48 |
b64 = base64.b64encode(buffer.read()).decode()
|
| 49 |
-
href = f'<a href="data:file/{file_format};base64,{b64}" download="augmented_image.{file_format}">Download {file_format.upper()} File</a>'
|
| 50 |
return href
|
| 51 |
|
| 52 |
-
|
| 53 |
def download_pdf(image):
|
| 54 |
pdf = FPDF()
|
| 55 |
pdf.add_page()
|
|
|
|
|
|
|
| 56 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
pdf_output = BytesIO()
|
| 62 |
pdf.output(pdf_output, "F")
|
| 63 |
pdf_output.seek(0)
|
|
|
|
| 64 |
b64 = base64.b64encode(pdf_output.read()).decode()
|
| 65 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="augmented_image.pdf">Download PDF</a>'
|
| 66 |
return href
|
| 67 |
|
| 68 |
-
|
| 69 |
st.title("Image Augmentation Tool")
|
| 70 |
|
| 71 |
# File upload
|
|
@@ -96,7 +99,9 @@ if uploaded_file:
|
|
| 96 |
st.image(cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB), caption="Transformed Image", use_column_width=True)
|
| 97 |
|
| 98 |
st.markdown("### Download Transformed Image:")
|
| 99 |
-
st.
|
| 100 |
-
st.markdown(download_image(transformed_image, "png"), unsafe_allow_html=True)
|
| 101 |
-
st.markdown(download_pdf(transformed_image), unsafe_allow_html=True)
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import base64
|
| 7 |
from fpdf import FPDF
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
# Define augmentation functions
|
| 11 |
def rotate_image(image, angle):
|
| 12 |
(h, w) = image.shape[:2]
|
| 13 |
center = (w // 2, h // 2)
|
|
|
|
| 15 |
rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h))
|
| 16 |
return rotated_image
|
| 17 |
|
|
|
|
| 18 |
def add_noise(image):
|
| 19 |
s_vs_p = 0.5
|
| 20 |
amount = 0.02
|
|
|
|
| 27 |
noisy_image[coords[0], coords[1], :] = 0
|
| 28 |
return noisy_image
|
| 29 |
|
|
|
|
| 30 |
def blur_image(image, kernel_size):
|
| 31 |
blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
|
| 32 |
return blurred_image
|
| 33 |
|
|
|
|
| 34 |
def translate_image(image, tx, ty):
|
| 35 |
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 36 |
height, width = image.shape[:2]
|
| 37 |
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
|
| 38 |
return translated_image
|
| 39 |
|
| 40 |
+
# Download functions
|
| 41 |
def download_image(image, file_format):
|
| 42 |
+
if file_format.lower() == "jpg":
|
| 43 |
+
file_format = "jpeg"
|
| 44 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 45 |
buffer = BytesIO()
|
| 46 |
pil_image.save(buffer, format=file_format.upper())
|
| 47 |
buffer.seek(0)
|
| 48 |
b64 = base64.b64encode(buffer.read()).decode()
|
| 49 |
+
href = f'<a href="data:file/{file_format};base64,{b64}" download="augmented_image.{file_format.lower()}">Download {file_format.upper()} File</a>'
|
| 50 |
return href
|
| 51 |
|
|
|
|
| 52 |
def download_pdf(image):
|
| 53 |
pdf = FPDF()
|
| 54 |
pdf.add_page()
|
| 55 |
+
|
| 56 |
+
temp_image_path = "temp_image.jpg"
|
| 57 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 58 |
+
pil_image.save(temp_image_path, format="JPEG")
|
| 59 |
+
|
| 60 |
+
pdf.image(temp_image_path, x=10, y=10, w=190)
|
| 61 |
+
os.remove(temp_image_path)
|
| 62 |
+
|
| 63 |
pdf_output = BytesIO()
|
| 64 |
pdf.output(pdf_output, "F")
|
| 65 |
pdf_output.seek(0)
|
| 66 |
+
|
| 67 |
b64 = base64.b64encode(pdf_output.read()).decode()
|
| 68 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="augmented_image.pdf">Download PDF</a>'
|
| 69 |
return href
|
| 70 |
|
| 71 |
+
# Streamlit app
|
| 72 |
st.title("Image Augmentation Tool")
|
| 73 |
|
| 74 |
# File upload
|
|
|
|
| 99 |
st.image(cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB), caption="Transformed Image", use_column_width=True)
|
| 100 |
|
| 101 |
st.markdown("### Download Transformed Image:")
|
| 102 |
+
download_format = st.radio("Select Download Format:", ["JPG", "PNG", "PDF"])
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
if download_format == "JPG" or download_format == "PNG":
|
| 105 |
+
st.markdown(download_image(transformed_image, download_format), unsafe_allow_html=True)
|
| 106 |
+
elif download_format == "PDF":
|
| 107 |
+
st.markdown(download_pdf(transformed_image), unsafe_allow_html=True)
|