File size: 2,061 Bytes
1485575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image, ImageDraw

def create_square_mask(
    width: int = 1536, 
    height: int = 1024, 
    square_size: int = 512, 
    center_x: int = None, 
    center_y: int = None
) -> Image:
    """
    Create a square mask for an image of given size.
    
    Args:
        width (int): Width of the image (default: 1536)
        height (int): Height of the image (default: 1024)
        square_size (int): Size of the square mask (default: 512)
        center_x (int): X-coordinate of the square's center (default: image center)
        center_y (int): Y-coordinate of the square's center (default: image center)
    
    Returns:
        Image: A PIL Image with a white square mask on a transparent background
    """
    # Create a new image with RGBA mode (transparent background)
    mask_image = Image.new("RGBA", (width, height), (0, 0, 0, 0))
    
    # Create a drawing context
    draw = ImageDraw.Draw(mask_image)
    
    # Determine the center of the square
    if center_x is None:
        center_x = width // 2
    if center_y is None:
        center_y = height // 2
    
    # Calculate the bounding box for the square
    half_size = square_size // 2
    left = center_x - half_size
    top = center_y - half_size
    right = center_x + half_size
    bottom = center_y + half_size
    
    # Ensure the square stays within image bounds
    left = max(0, left)
    top = max(0, top)
    right = min(width, right)
    bottom = min(height, bottom)
    
    # Draw a white square (fully opaque) on the transparent background
    draw.rectangle([left, top, right, bottom], fill=(255, 255, 255, 255))
    
    return mask_image

# Generate and save the mask
if __name__ == "__main__":
    # Create a square mask centered in the image
    mask = create_square_mask(
        width=1536,
        height=1024,
        square_size=512,  # A 512x512 square
        center_x=768,     # Center of 1536
        center_y=512      # Center of 1024
    )
    mask.save("square_mask.png", "PNG")
    print("Square mask image saved as 'square_mask.png'")