Hi, I want to crop some images downloaded previously, using the map feature’s polygon coordinates obtained previously, to have an image containing only the road sign cut out from the image, I tried to normalize the coordinates but the result is always an empty image , am I making a mistake in normalizing the coordinates?..
This is my testing code:
def cropImages(self):
image_id = "381021976479284"
image_url = f'https://graph.mapillary.com/{image_id}?access_token={codes.API_KEY}&fields=height,width'
response = requests.get(image_url)
image_data = response.json()
height = image_data['height']
width = image_data['width']
# read image as RGB and add alpha (transparency)
im = Image.open('D:/Dataset Custom Mapillary/images/771212506906717.png').convert('RGBA')
polygon = [[1216, 2655], [1346, 2655], [1346, 2594], [1216, 2594], [1216, 2655]]
pixel_coords = [(int(x / 4096 * width), int(y / 4096 * height)) for x, y in tuple(polygon)]
# convert to numpy (for convenience)
imArray = np.asarray(im)
# create mask
maskIm = Image.new('L', (imArray.shape[1], imArray.shape[0]), 0)
ImageDraw.Draw(maskIm).polygon(pixel_coords, outline=1, fill=1)
mask = np.array(maskIm)
# assemble new image (uint8: 0-255)
newImArray = np.empty(imArray.shape, dtype='uint8')
# colors (three first columns, RGB)
newImArray[:, :, :3] = imArray[:, :, :3]
# transparency (4th column)
newImArray[:, :, 3] = mask * 255
# back to Image from numpy
newIm = Image.fromarray(newImArray, "RGBA")
newIm.save("D:/Dataset Custom Mapillary/images_crop/out.png")