Spaces:
Running
Running
Upload 2 files
Browse files- app.py +86 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
|
7 |
+
def crop_image(image, left_mm, right_mm):
|
8 |
+
"""
|
9 |
+
Crop the image from both the left and right sides.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
image (PIL.Image.Image): The input image.
|
13 |
+
left_mm (float): The amount to crop from the left side in millimeters.
|
14 |
+
right_mm (float): The amount to crop from the right side in millimeters.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
PIL.Image.Image: The cropped image.
|
18 |
+
"""
|
19 |
+
# Calculate the cropping width in pixels based on image DPI
|
20 |
+
dpi = image.info.get("dpi", 72)
|
21 |
+
width_mm = image.width * 25.4 / dpi
|
22 |
+
cropping_width = int((left_mm + right_mm) * image.width / width_mm)
|
23 |
+
|
24 |
+
# Calculate the left and right cropping boundaries
|
25 |
+
left_crop = int(left_mm * image.width / width_mm)
|
26 |
+
right_crop = image.width - int(right_mm * image.width / width_mm)
|
27 |
+
|
28 |
+
# Crop the image
|
29 |
+
cropped_image = image.crop((left_crop, 0, right_crop, image.height))
|
30 |
+
|
31 |
+
return cropped_image
|
32 |
+
|
33 |
+
|
34 |
+
def main():
|
35 |
+
st.title("Batch Image Cropper")
|
36 |
+
|
37 |
+
# Upload multiple image files
|
38 |
+
uploaded_files = st.file_uploader("Upload image files", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
39 |
+
|
40 |
+
if uploaded_files is not None:
|
41 |
+
# Get user input for cropping dimensions
|
42 |
+
left_mm = st.number_input("Crop from left (mm)", min_value=0.0, max_value=1000.0, value=0.0, step=0.1)
|
43 |
+
right_mm = st.number_input("Crop from right (mm)", min_value=0.0, max_value=1000.0, value=0.0, step=0.1)
|
44 |
+
|
45 |
+
# Create a container to hold the cropped images
|
46 |
+
images_container = st.empty()
|
47 |
+
|
48 |
+
# Process each uploaded image file
|
49 |
+
cropped_images = []
|
50 |
+
for uploaded_file in uploaded_files:
|
51 |
+
# Open the image file
|
52 |
+
image = Image.open(uploaded_file)
|
53 |
+
|
54 |
+
# Crop the image
|
55 |
+
cropped_image = crop_image(image, left_mm, right_mm)
|
56 |
+
cropped_images.append(cropped_image)
|
57 |
+
|
58 |
+
# Display the original and cropped images side by side
|
59 |
+
col_width = 300
|
60 |
+
num_images = len(uploaded_files)
|
61 |
+
num_columns = 2
|
62 |
+
num_rows = (num_images + 1) // num_columns
|
63 |
+
for row in range(num_rows):
|
64 |
+
cols = st.columns(num_columns)
|
65 |
+
for col in range(num_columns):
|
66 |
+
index = row * num_columns + col
|
67 |
+
if index < num_images:
|
68 |
+
cols[col].subheader(f"Image {index+1}")
|
69 |
+
cols[col].image(uploaded_files[index], caption="Original Image", width=col_width)
|
70 |
+
cols[col].image(cropped_images[index], caption="Cropped Image", width=col_width)
|
71 |
+
|
72 |
+
# Save all cropped images
|
73 |
+
if st.button("Save All"):
|
74 |
+
zip_filename = "cropped_images.zip"
|
75 |
+
with zipfile.ZipFile(zip_filename, "w") as zipf:
|
76 |
+
for i, cropped_image in enumerate(cropped_images):
|
77 |
+
filename, ext = os.path.splitext(uploaded_files[i].name)
|
78 |
+
cropped_filename = f"cropped_{filename}.png"
|
79 |
+
cropped_image.save(cropped_filename)
|
80 |
+
zipf.write(cropped_filename)
|
81 |
+
os.remove(cropped_filename)
|
82 |
+
st.success(f"All cropped images saved successfully as {zip_filename}")
|
83 |
+
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Pillow
|
2 |
+
streamlit
|