View Code? Open in Web Editor
NEW
Repo for my blogpost on cropping tiles satellite imagery to a GeoJSON boundary
Home Page: https://bjnortier.com/blog/cropping-tiled-satellite-imagery
License: MIT License
crop-satellite-tiles-to-geojson's People
Stargazers
Watchers
crop-satellite-tiles-to-geojson's Issues
|
# How to do RGB in one step? |
Use .polygon(fill=255)
to draw the alpha mask with a 255
value
and later just merge the single sliced layer in masked_raw[...,3] = mask_raw
def mask_pixels_outside_boundary(cropped_to_boundary_image,
boundary_coordinates,
cropped_bbox):
"""
Mask the image to only include pixels inside the GeoJSON boundary.
"""
polygon_pixels = create_boundary_pixels(
boundary_coordinates,
cropped_bbox,
cropped_to_boundary_image.size)
mask_image = Image.new('L', cropped_to_boundary_image.size, 0)
ImageDraw.Draw(mask_image).polygon(polygon_pixels, outline=0, fill=255)
mask_raw = np.array(mask_image)
cropped_to_bbox_raw = np.asarray(cropped_to_boundary_image)
masked_raw = np.empty(cropped_to_bbox_raw.shape, dtype='uint8')
masked_raw[...,3] = mask_raw
masked_image = Image.fromarray(masked_raw, 'RGBA')
return masked_image