#!/usr/bin/python """Blurs a land-sea-mask blur_lsm infile.png outfile.png blur_radius Applies a square uniform filter to a land-sea-mask. The resulting mask will be: - 0 if the full filter was land - 255 if the full filter was sea - 1 <= x <= 254 if part of the filter was land, part sea This is particularly useful if applying the land-sea-mask for sensors with large footprints, such as AMSU-A; then the literally gray areas can be excluded from either land-based or sea-based pixels.""" # $Id$ from __future__ import division import sys import PIL.Image ##import PIL.ImageFilter import numpy import datetime now = datetime.datetime.now import scipy.ndimage.io import scipy.ndimage.filters def main(): try: f_in, f_out, size = sys.argv[1:4] except ValueError: print __doc__ sys.exit(1) size = int(size) im = PIL.Image.open(sys.argv[1]) # convert to uint8 or numpy.array will crash im = im.convert('L') # apply uniform (averaging) filter imd = numpy.array(im, dtype='float32') imd = scipy.ndimage.filters.uniform_filter(imd, size) # correct for rounding errors verysmall = imd<1e-4 verylarge = imd>(255-1e-4) imd[verysmall] = 0 imd[verylarge] = 255 # make sure small and large don't get clipped to none and all small = imd<1 imd[small] = numpy.ceil(imd[small]) large = imd>254 imd[large] = numpy.floor(imd[large]) # convert back to uint8 imd = numpy.round(imd) imd = numpy.uint8(imd) im.fromstring(imd.tostring()) im.save(f_out) if __name__ == "__main__": main()