|
import random |
|
from PIL import Image, ImageDraw |
|
|
|
|
|
def mask_image_with_random_boxes(image_path, output_path, num_boxes, box_width, box_height): |
|
|
|
image = Image.open(image_path) |
|
width, height = image.size |
|
|
|
|
|
draw = ImageDraw.Draw(image) |
|
|
|
|
|
for _ in range(num_boxes): |
|
x1 = random.randint(0, width - box_width) |
|
y1 = random.randint(0, height - box_height) |
|
x2 = x1 + box_width |
|
y2 = y1 + box_height |
|
draw.rectangle([x1, y1, x2, y2], fill="black") |
|
|
|
|
|
image.save(output_path) |
|
|
|
|
|
|
|
input_path = "tmp/data/Beautiful-horse-running-in-a-grassy-field.jpg" |
|
output_path = "tmp/data/random_mask-Beautiful-horse-running-in-a-grassy-field.jpg" |
|
mask_image_with_random_boxes(input_path, output_path, 64, 140, 140) |
|
|