Spaces:
Running
Running
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') | |