Skip to content
Snippets Groups Projects
Commit 71fdd306 authored by Sharon Yates's avatar Sharon Yates
Browse files

Function to create test data

Create white images of specified size with square black objects of specified pixel size and pixel location.
parent 27accd35
No related branches found
No related tags found
No related merge requests found
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
def generate_test_image(width, height, file_path): def generate_test_image(width, height, file_path):
# Create an empty white image # Create an empty white image
image = Image.new(mode = "RGB", size = (width, height), color=(255,255,255)) image = Image.new(mode = "RGB", size = (width, height), color=(255,255,255))
...@@ -15,29 +16,34 @@ file_path = 'testimage.png' # Specify the file path for saving the image ...@@ -15,29 +16,34 @@ file_path = 'testimage.png' # Specify the file path for saving the image
# Generate and save the black and white image # Generate and save the black and white image
generate_test_image(width, height, file_path) generate_test_image(width, height, file_path)
def generate_white_image_with_squares(width, height, square_size, square_locations):
# Create a white image with objects of specified size and x, y location
image = Image.new('RGB', (width, height), color=(255, 255, 255))
# Create a draw object """This is used to generate the test data"""
draw = ImageDraw.Draw(image)
def generate_image_with_squares(width, height, square_diameter, square_locations, num_images):
# Create a white image with objects of specified size at specified x, y locations
for i in range(1, num_images + 1):
image = Image.new('RGB', (width, height), color=(255, 255, 255))
# Draw black squares # Create a draw object
for location in square_locations: draw = ImageDraw.Draw(image)
x, y = location
square = (x, y, x + square_size, y + square_size)
draw.rectangle(square, fill=(0, 0, 0))
return image # Draw black squares
for location in square_locations:
x, y = location
square = (x, y, x + (square_size - 1), y + (square_size - 1))
# square defines the bounding box
draw.rectangle(square, fill=(0, 0, 0))
file_name = f"../test_data/PyTest/test_s00{i}.png"
image.save(file_name, "PNG")
# Example usage # Example usage
width = 1000 # Specify the width of the image in pixels width = 1500 # Specify the width of the image in pixels
height = 1000 # Specify the height of the image in pixels height = 1000 # Specify the height of the image in pixels
square_size = 50 # Specify the size of the black squares in pixels square_diameter = 10 # Specify the size of the black squares in pixels
square_locations = [(500, 500), (150, 150), (250, 250), (750, 750)] # Specify the x, y locations of the black squares square_locations = [(500, 500), (500, 600), (500, 700), (1000, 500), (1000,600),(1000,700)] # Specify the x, y locations of the black squares
num_images = 5
# Generate the white image with black squares # Generate the white image with black squares
image = generate_white_image_with_squares(width, height, square_size, square_locations) image = generate_image_with_squares(width, height, square_diameter, square_locations,num_images)
# Save the image
image.save('white_image_with_squares.png')
\ No newline at end of file
test_data/PyTest/test_s001.png

6.61 KiB

test_data/PyTest/test_s002.png

6.61 KiB

test_data/PyTest/test_s003.png

6.61 KiB

test_data/PyTest/test_s004.png

6.61 KiB

test_data/PyTest/test_s005.png

6.61 KiB

test_data/testimage.png

5.09 KiB

test_data/white_image_with_squares.png

5.16 KiB | W: | H:

test_data/white_image_with_squares.png

5.17 KiB | W: | H:

test_data/white_image_with_squares.png
test_data/white_image_with_squares.png
test_data/white_image_with_squares.png
test_data/white_image_with_squares.png
  • 2-up
  • Swipe
  • Onion skin
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment