Spaces:
Running
Running
File size: 3,067 Bytes
997dacc 3e8ff37 997dacc 3e8ff37 997dacc 3e8ff37 997dacc 3e8ff37 997dacc 3e8ff37 997dacc 3e8ff37 997dacc cd04538 3e8ff37 997dacc 3e8ff37 997dacc ec08e21 997dacc 3e8ff37 997dacc 3e8ff37 997dacc 3e8ff37 997dacc 3e8ff37 997dacc dfb7f08 997dacc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
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')
|