Spaces:
Running
Running
import os | |
import shutil | |
import gradio as gr | |
from PIL import Image | |
import uuid | |
import edimg | |
def replicate_edge(image, border_size): | |
# Replicate the edges | |
left = image.crop((0, 0, 1, image.height)).resize((border_size, image.height)) | |
right = image.crop((image.width - 1, 0, image.width, image.height)).resize((border_size, image.height)) | |
top = image.crop((0, 0, image.width, 1)).resize((image.width, border_size)) | |
bottom = image.crop((0, image.height - 1, image.width, image.height)).resize((image.width, border_size)) | |
# Create a new image with the replicated borders | |
new_image = Image.new('RGB', (image.width + 2 * border_size, image.height + 2 * border_size)) | |
new_image.paste(image, (border_size, border_size)) | |
new_image.paste(left, (0, border_size)) | |
new_image.paste(right, (image.width + border_size, border_size)) | |
new_image.paste(top, (border_size, 0)) | |
new_image.paste(bottom, (border_size, image.height + border_size)) | |
# Replicate corners | |
new_image.paste(image.getpixel((0, 0)), (0, 0, border_size, border_size)) # Top-left corner | |
new_image.paste(image.getpixel((image.width - 1, 0)), | |
(image.width + border_size, 0, image.width + 2 * border_size, border_size)) # Top-right corner | |
new_image.paste(image.getpixel((0, image.height - 1)), | |
(0, image.height + border_size, border_size, image.height + 2 * border_size)) # Bottom-left corner | |
new_image.paste(image.getpixel((image.width - 1, image.height - 1)), ( | |
image.width + border_size, image.height + border_size, image.width + 2 * border_size, | |
image.height + 2 * border_size)) # Bottom-right corner | |
return new_image | |
def outpaint_image_with_pillow(image): | |
# Define the amount of pixels to add on each side | |
border_size = 42 | |
# Extend the image with replicated edges | |
outpainted_image = replicate_edge(image, border_size) | |
# Save the outpainted image | |
return outpainted_image | |
def select(a, b, login): | |
if a == None or b == None: | |
gr.Warning('앞 뒷면 이미지를 먼저 만들어주세요') | |
return None, None, None, None | |
r, g, bs = b.getpixel((33, 33)) | |
b = edimg.rounded_color(b, r, g, bs) | |
ap = outpaint_image_with_pillow(a) | |
bp = outpaint_image_with_pillow(b) | |
return ap, bp | |
def submit(ap, bp, login): | |
if ap == None or bp == None: | |
gr.Warning('앞 뒷면 이미지를 먼저 만들어주세요') | |
return None | |
# Generate a unique identifier | |
unique_id = str(uuid.uuid4())[:18] | |
# Define the target directory | |
target_directory = f'/data/printing/{unique_id}' | |
# Create the target directory if it doesn't exist | |
if not os.path.exists(target_directory): | |
os.makedirs(target_directory) | |
ap.save(f"{target_directory}/front.png", dpi=(2400, 2400)) | |
bp.save(f"{target_directory}/back.png", dpi=(2400, 2400)) | |
#print(f'Files have been copied to {target_directory}') | |
return unique_id | |
# Example usage | |
#outpaint_image('path_to_your_image.jpg', 'output_image.jpg') | |