Spaces:
Running
Running
File size: 4,031 Bytes
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import os
import shutil
import cv2
import gradio as gr
import numpy as np
from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence
import uuid
import edimg
def outpaint_image(image_path, output_path):
# Load the image
image = cv2.imread(image_path)
height, width, channels = image.shape
# Define the amount of pixels to add on each side
top = 42
bottom = 42
left = 27
right = 28
# Use cv2.copyMakeBorder to extend the image
outpainted_image = cv2.copyMakeBorder(
image,
top,
bottom,
left,
right,
cv2.BORDER_REPLICATE
)
# Save the outpainted image
cv2.imwrite(output_path, outpainted_image)
print(f"Image saved to {output_path}")
def outpaint_rgba_image(image_path, output_path):
# Load the image with alpha channel
image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
height, width, channels = image.shape
if channels != 4:
raise ValueError("Image does not have an alpha channel")
# Define the amount of pixels to add on each side
top = 42
bottom = 42
left = 27
right = 28
# Create the outpainted image with a transparent background
outpainted_image = np.zeros((height + top + bottom, width + left + right, 4), dtype=image.dtype)
# Copy the original image to the center of the outpainted image
outpainted_image[top:top+height, left:left+width] = image
# Fill the transparent areas with the edge colors
for i in range(4): # RGBA channels
outpainted_image[:top, left:left+width, i] = image[0, :, i] # Top
outpainted_image[top+height:, left:left+width, i] = image[-1, :, i] # Bottom
outpainted_image[:, :left, i] = outpainted_image[:, left:left+1, i] # Left
outpainted_image[:, left+width:, i] = outpainted_image[:, left+width-1:left+width, i] # Right
# Fill the corners
outpainted_image[:top, :left] = outpainted_image[top, left] # Top-left corner
outpainted_image[:top, left+width:] = outpainted_image[top, left+width-1] # Top-right corner
outpainted_image[top+height:, :left] = outpainted_image[top+height-1, left] # Bottom-left corner
outpainted_image[top+height:, left+width:] = outpainted_image[top+height-1, left+width-1] # Bottom-right corner
# Save the outpainted image
cv2.imwrite(output_path, outpainted_image)
print(f"Image saved to {output_path}")
def select(a, b, login):
if login == 'Login to Discord':
gr.Warning('먼저 로그인해주세요')
return None, None, None, None
if a == None or b == None:
gr.Warning('앞 뒷면 이미지를 먼저 만들어주세요')
return None, None, None, None
a.save('printing_a.png')
r, g, bs = b.getpixel((33, 33))
b = edimg.rounded_color(b, r, g, bs)
b.save('printing_b.png')
outpaint_image('printing_a.png', 'printing_a2.png')
outpaint_image('printing_b.png', 'printing_b2.png')
return 'printing_a2.png', 'printing_b2.png', a, b
def submit(file_path_a, file_path_b, login):
if login == 'Login to Discord':
gr.Warning('먼저 로그인해주세요')
return None
if file_path_a == None or file_path_b == 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)
# Copy the files to the target directory
shutil.copy(file_path_a, os.path.join(target_directory, os.path.basename(file_path_a)))
shutil.copy(file_path_b, os.path.join(target_directory, os.path.basename(file_path_b)))
#print(f'Files have been copied to {target_directory}')
return unique_id
# Example usage
#outpaint_image('path_to_your_image.jpg', 'output_image.jpg')
|