Spaces:
Runtime error
Runtime error
from PIL import Image, ImageOps | |
import numpy as np | |
from create_print_layover import create_hard_light_layover, create_soft_light_layover | |
import cv2 | |
def create_layover(background_image, layer_image, opacity): | |
background_img_raw = Image.open(background_image) | |
background_img_raw = background_img_raw.convert("RGBA") | |
background_img = np.array(background_img_raw) | |
background_img_float = background_img.astype(float) | |
foreground_img_raw = Image.open(layer_image) | |
foreground_img_raw = foreground_img_raw.convert("RGBA") | |
foreground_img = np.array(foreground_img_raw) | |
foreground_img_float = foreground_img.astype(float) | |
blended_img_float = create_soft_light_layover(background_img_float, foreground_img_float, opacity) | |
blended_img = np.uint8(blended_img_float) | |
blended_img_raw = Image.fromarray(blended_img) | |
output_path = "lay_over_image.png" | |
blended_img_raw.save(output_path) | |
return output_path | |
def create_image_tile(input_patch, x_dim, y_dim): | |
input_image = Image.open(input_patch) | |
input_image = input_image.convert("RGB") | |
width, height = input_image.size | |
output_image = Image.new("RGB", (x_dim, y_dim)) | |
for y in range(0, y_dim, height): | |
for x in range(0, x_dim, width): | |
region_height = min(height, y_dim - y) | |
region_width = min(width, x_dim - x) | |
region = input_image.crop((0, 0, region_width, region_height)) | |
output_image.paste(region, (x, y)) | |
output_image.save('tiled_image.png') | |
def create_image_cutout(texture_transfer_image, actual_mask): | |
image = Image.open(texture_transfer_image).convert('RGB') | |
mask = Image.open(actual_mask).convert('L') | |
if mask.size != image.size: | |
image = image.resize(mask.size, Image.Resampling.NEAREST) | |
image_np = np.array(image) | |
mask_np = np.array(mask) | |
binary_mask = (mask_np > 127).astype(np.uint8) | |
masked_image_np = image_np * np.expand_dims(binary_mask, axis=-1) | |
masked_image = Image.fromarray(masked_image_np.astype(np.uint8)) | |
masked_image.save('cut_out_image.png') | |
def paste_image(base_image_path, cutout_image_path, mask_path): | |
background = Image.open(base_image_path).convert("RGB") | |
cutout = Image.open(cutout_image_path) | |
mask = Image.open(mask_path).convert("L") | |
if cutout.mode == 'RGBA': | |
cutout_rgb = cutout.convert("RGB") | |
cutout_alpha = cutout.split()[-1] | |
else: | |
cutout_rgb = cutout.convert("RGB") | |
cutout_alpha = mask | |
cutout_rgb = cutout_rgb.resize(background.size, Image.Resampling.NEAREST) | |
cutout_alpha = cutout_alpha.resize(background.size, Image.Resampling.NEAREST) | |
cutout_rgb_np = np.array(cutout_rgb) | |
background_np = np.array(background) | |
cutout_alpha_np = np.array(cutout_alpha) | |
cutout_alpha_np = cutout_alpha_np / 255.0 | |
cutout_masked = (cutout_rgb_np * cutout_alpha_np[..., np.newaxis]).astype(np.uint8) | |
background_masked = (background_np * (1 - cutout_alpha_np[..., np.newaxis])).astype(np.uint8) | |
result_np = cutout_masked + background_masked | |
result = Image.fromarray(result_np) | |
result.save('result.png') | |