File size: 3,104 Bytes
f25db56
 
 
 
 
 
 
65c821e
 
f25db56
 
308e62d
23ef698
f25db56
 
 
 
 
 
 
 
 
 
 
577dce8
3953b0e
f25db56
 
 
 
 
 
 
 
 
 
 
 
135150b
f25db56
 
 
 
 
135150b
 
 
f25db56
 
 
 
 
 
 
 
 
 
 
135150b
f25db56
 
 
 
 
135150b
 
 
 
f25db56
135150b
2de513c
 
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
from PIL import Image, ImageOps
import numpy as np
from create_print_layover import create_hard_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_hard_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')