fork download
  1. import numpy as np
  2.  
  3. def divide_image(image, num_vertical_divides, num_horizontal_divides):
  4. """
  5. Divides a white-black image into subimages based on the number of vertical and horizontal divides.
  6.  
  7. Args:
  8. image: A numpy array representing the white-black image.
  9. num_vertical_divides: The number of vertical divides.
  10. num_horizontal_divides: The number of horizontal divides.
  11.  
  12. Returns:
  13. A list of tuples, where each tuple represents a cutting line with its start and end points.
  14. """
  15.  
  16. height, width = image.shape
  17.  
  18. vertical_lines = []
  19. horizontal_lines = []
  20.  
  21. # Calculate vertical cutting lines
  22. for i in range(1, num_vertical_divides + 1):
  23. x = int(width * (i / (num_vertical_divides + 1)))
  24. vertical_lines.append(((x, 0), (x, height - 1)))
  25.  
  26. # Calculate horizontal cutting lines
  27. for i in range(1, num_horizontal_divides + 1):
  28. y = int(height * (i / (num_horizontal_divides + 1)))
  29. horizontal_lines.append(((0, y), (width - 1, y)))
  30.  
  31. return vertical_lines + horizontal_lines
  32.  
  33. # Example usage:
  34. # Assuming you have a 256x256 image and want to divide it into 4x4 grid (3 vertical and 3 horizontal divides)
  35. image = np.zeros((256, 256)) # Example black image
  36. cutting_lines = divide_image(image, 3, 3)
  37.  
  38. # Print the start and end points of each cutting line
  39. for line in cutting_lines:
  40. print(f"Start: {line[0]}, End: {line[1]}")
Success #stdin #stdout 0.12s 28952KB
stdin
Standard input is empty
stdout
Start: (64, 0), End: (64, 255)
Start: (128, 0), End: (128, 255)
Start: (192, 0), End: (192, 255)
Start: (0, 64), End: (255, 64)
Start: (0, 128), End: (255, 128)
Start: (0, 192), End: (255, 192)