|
import os |
|
import zipfile |
|
import streamlit as st |
|
from PIL import Image, ImageOps |
|
import io |
|
import tempfile |
|
|
|
|
|
patterns = { |
|
1: "Barcode Pattern.jpg", |
|
2: "Diagonal Stripes.jpg", |
|
3: "horizontal stripe pattern.jpg", |
|
4: "Vertical Concentrated.jpg" |
|
} |
|
|
|
|
|
def apply_pattern(image, pattern_id): |
|
"""Apply selected stripe pattern to the given image.""" |
|
original = image.convert("RGBA") |
|
pattern = Image.open(patterns[pattern_id]).convert("L") |
|
|
|
|
|
transparent_pattern = Image.new("RGBA", pattern.size) |
|
for x in range(pattern.width): |
|
for y in range(pattern.height): |
|
pixel = pattern.getpixel((x, y)) |
|
|
|
transparent_pattern.putpixel((x, y), (0, 0, 0, 255) if pixel < 128 else (0, 0, 0, 0)) |
|
|
|
|
|
transparent_pattern = ImageOps.fit(transparent_pattern, original.size, method=0, bleed=0.0, centering=(0.5, 0.5)) |
|
|
|
|
|
combined = Image.alpha_composite(original, transparent_pattern) |
|
return combined |
|
|
|
|
|
def process_image(image, pattern_id, image_name): |
|
"""Apply the selected pattern to the uploaded image and save it to the output directory.""" |
|
combined_image = apply_pattern(image, pattern_id) |
|
|
|
|
|
combined_image = combined_image.convert("RGB") |
|
|
|
|
|
img_byte_arr = io.BytesIO() |
|
combined_image.save(img_byte_arr, format='JPEG') |
|
img_byte_arr.seek(0) |
|
return img_byte_arr |
|
|
|
|
|
def create_zip_file(image_buffers, zip_filename): |
|
"""Create a ZIP file from the processed images.""" |
|
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: |
|
for idx, image_buffer in enumerate(image_buffers): |
|
|
|
zipf.writestr(f"patterned_image_{idx + 1}.jpg", image_buffer.read()) |
|
return zip_filename |
|
|
|
|
|
st.title("Stripe Pattern Applier") |
|
st.write("Upload one or multiple images and apply a stripe pattern.") |
|
|
|
|
|
st.sidebar.title("Instructions") |
|
st.sidebar.write(""" |
|
1. **Upload an Image**: Choose either a single image or multiple images by using the file uploader. |
|
2. **Choose a Pattern**: You can either apply a **single pattern** or apply **all available patterns** to your uploaded images. |
|
3. **Download**: Once the images are processed, you can download the individual processed image(s) or download all processed images in a ZIP file. |
|
4. **Pattern Options**: The available patterns include: |
|
- **Vertical Stripes** |
|
- **Diagonal Stripes** |
|
- **Horizontal Stripes** |
|
- **Vertical Concentrated Stripes** |
|
""") |
|
|
|
|
|
with st.expander("Learn about Face Filtering", expanded=False): |
|
st.write( |
|
"Face filtering is a creative technique that enhances images by applying various effects to highlight different features of the subject. The filters available in this app include Detail, which sharpens textures and makes features stand out; Edge Enhance and Edge Enhance More, which accentuate edges for a defined look; Smooth and Smooth More, which soften the image and reduce imperfections for a polished appearance; and Sharpen, which increases contrast and clarity to reveal finer details. Together, these filters allow users to transform their photos, emphasizing beauty and enabling unique artistic interpretations, making face filtering an engaging way to manipulate images and express creativity." |
|
) |
|
|
|
|
|
|
|
st.image("Barcode Pattern.jpg.jpg", caption="Verticle Stripe Pattern", use_column_width=True) |
|
st.image("Diagonal Stripes.jpg", caption="Diagonal Stripe Pattern", use_column_width=True) |
|
st.image("horizontal stripe pattern.jpg", caption="Horizontal Stripe Pattern", use_column_width=True) |
|
st.image("Vertical Concentrated.jpg", caption="Vertical Concentrated Stripe Pattern", use_column_width=True) |
|
|
|
|
|
upload_option = st.radio("Choose Upload Option", ["Upload Single Image", "Upload Multiple Images"]) |
|
|
|
|
|
if upload_option == "Upload Single Image": |
|
uploaded_files = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg", "bmp", "gif"], key="single_image") |
|
else: |
|
uploaded_files = st.file_uploader("Choose image files", type=["png", "jpg", "jpeg", "bmp", "gif"], accept_multiple_files=True, key="multiple_images") |
|
|
|
|
|
pattern_option = st.radio( |
|
"Select Pattern Option", |
|
options=["Apply One Pattern", "Apply All Patterns"] |
|
) |
|
|
|
|
|
if pattern_option == "Apply One Pattern": |
|
pattern_id = st.radio( |
|
"Select a stripe pattern", |
|
options=[1, 2, 3, 4], |
|
format_func=lambda x: { |
|
1: "Vertical Stripes", |
|
2: "Diagonal Stripes", |
|
3: "Horizontal Stripes", |
|
4: "Vertical Concentrated Stripes" |
|
}[x] |
|
) |
|
|
|
|
|
if uploaded_files: |
|
if upload_option == "Upload Single Image": |
|
|
|
image = Image.open(uploaded_files) |
|
st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
if pattern_option == "Apply One Pattern": |
|
processed_image_buffer = process_image(image, pattern_id, uploaded_files.name) |
|
|
|
st.download_button( |
|
label="Download Processed Image", |
|
data=processed_image_buffer, |
|
file_name=f"patterned_{uploaded_files.name}", |
|
mime="image/jpeg" |
|
) |
|
else: |
|
|
|
processed_image_buffers = [] |
|
for pattern_id in patterns: |
|
processed_image_buffer = process_image(image, pattern_id, uploaded_files.name) |
|
processed_image_buffers.append(processed_image_buffer) |
|
|
|
|
|
zip_filename = "processed_images.zip" |
|
zip_file = create_zip_file(processed_image_buffers, zip_filename) |
|
|
|
|
|
with open(zip_filename, "rb") as f: |
|
st.download_button( |
|
label="Download All Processed Images as ZIP", |
|
data=f, |
|
file_name="processed_images.zip", |
|
mime="application/zip" |
|
) |
|
|
|
else: |
|
|
|
processed_image_buffers = [] |
|
|
|
|
|
for uploaded_file in uploaded_files: |
|
image = Image.open(uploaded_file) |
|
if pattern_option == "Apply One Pattern": |
|
processed_image_buffer = process_image(image, pattern_id, uploaded_file.name) |
|
processed_image_buffers.append(processed_image_buffer) |
|
else: |
|
|
|
for pattern_id in patterns: |
|
processed_image_buffer = process_image(image, pattern_id, uploaded_file.name) |
|
processed_image_buffers.append(processed_image_buffer) |
|
|
|
|
|
zip_filename = "processed_images.zip" |
|
zip_file = create_zip_file(processed_image_buffers, zip_filename) |
|
|
|
|
|
with open(zip_filename, "rb") as f: |
|
st.download_button( |
|
label="Download All Processed Images as ZIP", |
|
data=f, |
|
file_name="processed_images.zip", |
|
mime="application/zip" |
|
) |
|
|
|
else: |
|
st.write("Please upload one or more images to start.") |
|
|