Contents
How to create a 2D polygon mask in Python?
I need to create a numpy 2D array which represents a binary mask of a polygon, using standard Python packages. (Larger context: I want to get the distance transform of this polygon using scipy.ndimage.morphology.distance_transform_edt.)
How to draw a polygon on top of an image?
To draw a semi-transparent polygon on top of img, make a copy of the image. One the copy, draw the polygon in full color without alpha. Then use Image.blend to combine the original image with the copy with a set level of alpha.
How do you draw a shape in Python?
The line () method will draw a straight line, connecting each point. The polygon () will draw a polygon where each point is connected. Finally, the point () will draw a point of 1-pixel at each point. The parameter, fill, is used to set the color that will fill the shape.
What can I use to rasterize polygons in Python?
As a slightly more direct alternative to @Anil’s answer, matplotlib has matplotlib.nxutils.points_inside_poly that can be used to quickly rasterize an arbitrary polygon. E.g.
How to check if a string is IPv4 subnet mask?
I’ve written a small Python function to check whether a string is a valid IPv4 subnet mask. It works but maybe it’s too convoluted and terse? Or maybe it could be improved / optimised / rewritten in a cleverer way than just to check 1’s in a string?..
How to find the coordinates of a binary mask in Python?
You can use np.column_stack () + np.where (). The idea is to determine the white pixels in the binary image then order then in corresponding (x, y) order Another way is to find the coordinates of the bounding rectangle using OpenCV’s cv2.boundingRect (). This will give you the width, height, and top-left (x,y) coordinates.
Which is the correct way to do isipv4mask?
Your IsIPv4Mask should probably be done in a very different manner – rather than string-ifying the octets to binary text, it should convert the words to a single 32-bit integer (as is done everywhere else), for efficiency. At that point, write a loop that Thanks for contributing an answer to Code Review Stack Exchange!