Reputation: 131
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
Upvotes: 1
Views: 447
Reputation: 131
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
Upvotes: 1