1

Hey guys I am struggling with making a grid over an image of a basketball court that starts from the middle - meaning there is a middle line both at width and height of the image from where the grid then evenly spans out over the whole picture (to the left&right and bottom&top). I am using this as an example: draw grid lines over an image in matplotlib. I would greatly appreciate any help! I am very new to matplotlib and am asking the experts out there!

Thank you

This is the code I am currently working on which is not really working (in case that helps)

import matplotlib.pyplot as plt
from PIL import Image
import pylab as pl

img = Image.open('PATH_TO_IMAGE')

im = pl.imread('PATH_TO_IMAGE')


width, height = img.size


newh = height/2
neww = width/2

print(newh)
print(neww)


#dividing the new numbers by the court dimensions 60x110 both divided by two since height and width are divided by two. We want rectangles exactly 1 foot big in width and height

dx, dy = int(newh/30), int(neww/55)

print(dx)
print(dy)

grid_color = [0,0,0]

 
im[:,::dy,:] = grid_color
im[::dx,:,:] = grid_color

plt.figure(figsize=(6,3.2))

# Show the result
plt.imshow(im)
plt.show()

This works exactly for one image but no other ones. Here is the error message:

line 37, in im[:,::dy,:] = grid_color ValueError: could not broadcast input array from shape (3) into shape (1322,119,4)

I can't paste the image of the court because of copyright problems but you can just use any image of a 2D basketball court image from google like this: https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.istockphoto.com%2Fillustrations%2Fbasketball-court-overhead&psig=AOvVaw3ORchlrt0TuWGaDMHoe5zn&ust=1629496920541000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCPCIk5yLvvICFQAAAAAdAAAAABAG

Felix
  • 113
  • 2
  • 10
  • Does this question help? https://stackoverflow.com/questions/20368413/draw-grid-lines-over-an-image-in-matplotlib – Milan Aug 19 '21 at 00:57
  • The code you provided is not reproducible: what is `img`? Please, provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Zephyr Aug 19 '21 at 07:13
  • Moreover, what do you mean by _starts from the middle - meaning there is a middle line both at width and height of the image from where the grid then evenly spans_? There are a vertical and a horizontal lines which divide the image in 4 quadrants and you want only one of them to be covered by the grid? – Zephyr Aug 19 '21 at 07:16
  • @Zephyr sorry - I updated it now. Let me know if you can find the error. Would greatly appreciate it! – Felix Aug 19 '21 at 22:06
  • using instead of grid colors just the number 1 worked but I want the grid colors in black. With 1 they are in grey: im[:,::dy,:] = 1 im[::dx,:,:] = 1 – Felix Aug 19 '21 at 22:29

1 Answers1

1

Ok so I found a solution for grid

since it is a RGBA value it needs 4 values. Red, green, blue and alpha. For the example of orange it would be:

grid_color = (255, 165, 0, 255)
im[:, ::dy, :] = grid_color
im[::dx, :, :] = grid_color
Felix
  • 113
  • 2
  • 10