To draw a square on the image, you can use Rectangle
from matplotlib.
See matplotlib: how to draw a rectangle on image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
imageArray = plt.imread('earth.jpg')
print('type of imageArray is ', type(imageArray))
print('shape of imageArray is ', imageArray.shape)
fig, ax = plt.subplots(1)
plt.imshow(imageArray)
square = patches.Rectangle((100,100), 20,20, color='RED')
ax.add_patch(square)
plt.show()
If you actually want to change each individual pixel, you could iterate over the rows/columns and set each pixel to [255, 0, 0]. Here's an example (if you go this direction, you'll want to include exception handling for IndexError):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def drawRedSquare(image, location, size):
x,y = location
w,h = size
for row in range(h):
for col in range(w):
image[row+y][col+x] = [255, 0, 0]
return image
imageArray = np.array(plt.imread('earth.jpg'), dtype=np.uint8)
print('type of imageArray is ', type(imageArray))
print('shape of imageArray is ', imageArray.shape)
imArray = drawRedSquare(imageArray, (100,100), (20,20))
fig = plt.figure()
plt.imshow(imageArray)
Result
Edit:
A more efficient solution for changing pixel values would be to use an array slice.
def drawRedSquare(image, location, size):
x,y = location
w,h = size
image[y:y+h,x:x+w] = np.ones((w,h,3)) * [255,0,0]
return image