This Python script extracts polygons from a PNG image based on a target color and converts them into vector polygons in QGIS. The polygons are added to a temporary vector layer with a specified CRS. Ideal for converting image data into spatial data for analysis.
Features
- Target Color Extraction: Extracts polygons from a PNG image by detecting pixels of a specified color (e.g., purple).
- Polygon Creation: Generates polygons from the detected pixels using the skimage library.
- Spatial Adjustment: Translates and scales the extracted polygons to fit into a specific geographic area (e.g., Glenorchy, Tasmania).
- Vector Layer Creation: Adds the extracted polygons to a temporary vector layer in QGIS with a specified CRS (GDA94 / MGA Zone 55, EPSG:28355).
- Filtering by Size: Filters polygons based on a minimum size to ensure only meaningful geometries are included.
Requirements
- QGIS with Python support
- Libraries: numpy, PIL (Pillow), skimage
You can install the required Python libraries using pip pip install numpy pillow scikit-image
Installation
- Download or clone this repository.
- Save the script as Extract_Polygon_from_PNG_Image.py.
- Open the script in the QGIS Python console.
Usage
- Modify the image_path variable in the script to point to your PNG image file.
- Adjust the purple_colour variable to target the RGB color you want to extract from the image.
- Set the translation and scaling factors to adjust the spatial placement of the extracted polygons as needed.
- Run the script in the QGIS Python console.
- The extracted polygons will be added as a temporary vector layer in QGIS, with CRS set to GDA94 / MGA Zone 55 (EPSG:28355).
- Word file with simple example uploaded
Example image_path = r"C:\path\to\your\imagename.png" purple_colour = (160, 32, 240) # RGB value for purple
Cheers