|
|
|
|
|
import streamlit as st |
|
from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw, ImageChops |
|
import random |
|
import os |
|
import io |
|
import time |
|
import numpy as np |
|
|
|
|
|
st.title("Unique Generative Photo Editor") |
|
|
|
|
|
start_time = time.time() |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
input_image = Image.open(uploaded_file).convert("RGB") |
|
st.image(input_image, caption='Uploaded Image', use_column_width=True) |
|
|
|
|
|
original_width, original_height = input_image.size |
|
|
|
|
|
st.sidebar.title("Parameter Adjustments") |
|
|
|
|
|
scale_factor = st.sidebar.slider("Image Scale (Size)", 0.1, 1.0, 1.0, 0.01) |
|
new_width = int(original_width * scale_factor) |
|
new_height = int(original_height * scale_factor) |
|
input_image = input_image.resize((new_width, new_height), resample=Image.LANCZOS) |
|
st.write(f"Resized Image: {input_image.size}") |
|
|
|
|
|
contrast_factor = st.sidebar.slider("Contrast Strength", 0.5, 3.0, 1.5, 0.1) |
|
|
|
|
|
brightness_factor = st.sidebar.slider("Brightness", 0.5, 3.0, 1.0, 0.1) |
|
|
|
|
|
sharpness_factor = st.sidebar.slider("Sharpness", 0.0, 5.0, 1.0, 0.1) |
|
|
|
|
|
sepia_depth = st.sidebar.slider("Sepia Depth", 0, 100, 30, 1) |
|
|
|
|
|
vignette_strength = st.sidebar.slider("Vignette Strength", 0.0, 1.0, 0.5, 0.01) |
|
|
|
|
|
noise_level = st.sidebar.slider("Noise Level", 0, 100, 30, 1) |
|
|
|
|
|
seed = random.randint(0, 2**32 - 1) |
|
st.write(f"Unique Seed Value: {seed}") |
|
|
|
|
|
seed_file = "used_seeds.txt" |
|
|
|
|
|
if os.path.exists(seed_file): |
|
with open(seed_file, 'r') as f: |
|
used_seeds = set(int(line.strip()) for line in f) |
|
else: |
|
used_seeds = set() |
|
|
|
if seed in used_seeds: |
|
st.error("This seed value has already been used. Please try again.") |
|
else: |
|
|
|
with open(seed_file, 'a') as f: |
|
f.write(f"{seed}\n") |
|
|
|
|
|
with st.spinner('Processing image...'): |
|
try: |
|
def apply_unique_effect(image, seed, contrast_factor, brightness_factor, sharpness_factor, |
|
sepia_depth, vignette_strength, noise_level): |
|
|
|
np.random.seed(seed) |
|
random.seed(seed) |
|
|
|
|
|
image = ImageOps.grayscale(image) |
|
|
|
|
|
enhancer = ImageEnhance.Contrast(image) |
|
image = enhancer.enhance(contrast_factor) |
|
|
|
|
|
enhancer = ImageEnhance.Brightness(image) |
|
image = enhancer.enhance(brightness_factor) |
|
|
|
|
|
enhancer = ImageEnhance.Sharpness(image) |
|
image = enhancer.enhance(sharpness_factor) |
|
|
|
|
|
sepia_image = np.array(image).astype(np.float64) |
|
sepia_image = sepia_image / 255.0 |
|
|
|
sepia_filter = np.array([[1.0, 0.95, 0.82]]) |
|
sepia_image = sepia_image[..., np.newaxis] * sepia_filter |
|
|
|
sepia_image = np.clip(sepia_image * (1 + sepia_depth / 100), 0, 1) |
|
sepia_image = (sepia_image * 255).astype(np.uint8) |
|
|
|
image = Image.fromarray(sepia_image, mode='RGB') |
|
|
|
|
|
width, height = image.size |
|
x = np.linspace(-1, 1, width) |
|
y = np.linspace(-1, 1, height) |
|
xx, yy = np.meshgrid(x, y) |
|
gradient = np.sqrt(xx**2 + yy**2) |
|
mask = (1 - gradient / gradient.max()) |
|
mask = np.clip(mask, 0, 1) |
|
mask = mask ** (vignette_strength * 10) |
|
|
|
alpha = (mask * 255).astype(np.uint8) |
|
vignette = Image.fromarray(alpha, mode='L') |
|
image.putalpha(vignette) |
|
|
|
|
|
noise_array = np.random.randint(0, noise_level, (height, width), dtype='uint8') |
|
noise_image = Image.fromarray(noise_array, mode='L') |
|
noise_image = noise_image.convert('RGBA') |
|
|
|
|
|
r, g, b, a = image.split() |
|
noise_r, noise_g, noise_b, noise_a = noise_image.split() |
|
|
|
r = ImageChops.add(r, noise_r) |
|
g = ImageChops.add(g, noise_g) |
|
b = ImageChops.add(b, noise_b) |
|
|
|
image = Image.merge('RGBA', (r, g, b, a)) |
|
|
|
|
|
image = image.convert("RGB") |
|
|
|
|
|
processing_time = time.time() - start_time |
|
if processing_time > 30: |
|
raise TimeoutError("Processing timed out. Please try again with a smaller image size.") |
|
|
|
return image |
|
|
|
|
|
output_image = apply_unique_effect( |
|
input_image, |
|
seed, |
|
contrast_factor, |
|
brightness_factor, |
|
sharpness_factor, |
|
sepia_depth, |
|
vignette_strength, |
|
noise_level |
|
) |
|
|
|
|
|
total_time = time.time() - start_time |
|
st.write(f"Processing Time: {total_time:.2f} seconds") |
|
|
|
st.image(output_image, caption='Transformed Image', use_column_width=True) |
|
|
|
|
|
buffered = io.BytesIO() |
|
output_image.save(buffered, format="PNG") |
|
img_data = buffered.getvalue() |
|
|
|
st.download_button( |
|
label="Download Image", |
|
data=img_data, |
|
file_name="transformed_image.png", |
|
mime="image/png" |
|
) |
|
except TimeoutError as e: |
|
st.error(str(e)) |
|
|
|
|
|
|
|
markdown_text = """ |
|
This application allows you to apply unique, artistic effects to your images, emulating a vintage style. Each image transformation is guaranteed to be unique due to the use of a random seed, ensuring that the same effect cannot be reproduced. |
|
|
|
## Features |
|
|
|
- **Uniqueness Guaranteed:** Uses a random seed for each transformation, so every image is one-of-a-kind. |
|
- **User Control:** Adjust various parameters like image scale, contrast, brightness, sharpness, sepia depth, vignette strength, and noise level to customize the effect. |
|
- **Vintage Effects:** Emulates the ambiance of classic photography techniques through digital image processing. |
|
|
|
## How to Use |
|
|
|
1. **Upload an Image:** Select the image you want to transform. |
|
2. **Adjust Parameters:** Use the sliders to fine-tune the effects to your liking. |
|
3. **Unique Seed Generation:** A unique seed value is generated for each transformation to ensure uniqueness. |
|
4. **Image Processing:** The app applies the effects based on your settings and the unique seed. |
|
5. **View and Download:** Preview the transformed image and download it if you're satisfied. |
|
|
|
## Notes |
|
|
|
- The uniqueness of each image is based on the random seed and your chosen parameters. |
|
- Images are processed locally and are not saved on the server. |
|
- Experiment with different settings to create your own unique piece of art. |
|
|
|
## Reference |
|
|
|
[1] Chinatsu Ozawa, Tatsuya Minagawa, and Yoichi Ochiai. 2024. Can AI Generated Ambrotype Chain the Aura of Alternative Process? In *SIGGRAPH Asia 2024 Art Papers (SA Art Papers '24)*, December 03–06, 2024, Tokyo, Japan. ACM, New York, NY, USA, 13 Pages. [https://doi.org/10.1145/3680530.3695434](https://doi.org/10.1145/3680530.3695434) |
|
""" |
|
|
|
|
|
st.markdown(markdown_text) |