Skip to content

Instantly share code, notes, and snippets.

@sgillies
Created February 4, 2015 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgillies/5368316f88bf67288bec to your computer and use it in GitHub Desktop.
Save sgillies/5368316f88bf67288bec to your computer and use it in GitHub Desktop.
Extracting characters from a PNG
$ rio insp ~/Desktop/logo.png Rasterio 0.17.1 Interactive Inspector (Python 2.7.9)
Type "src.meta", "src.read_band(1)", or "help(src)" for more information.
>>> red = src.read(1)
>>> red
array([[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
...,
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
>>> selection = red < 250
>>> img = np.ones_like(red) * 255
>>> img[selection] = 0
>>> from rasterio.features import shapes
>>> features = list(s[0] for s in shapes(img) if s[1] == 0)
>>> len(features)
4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment