def clip_power(pow,thresh=3):
        m = arraystats.mean(pow)
        s = arraystats.std(pow)
        minval = m-thresh*s*0.5
        maxval = m+thresh*s
        pow = numpy.clip(pow, minval, maxval)

        return pow
Ejemplo n.º 2
0
def clip_power(pow, thresh=3):
    m = arraystats.mean(pow)
    s = arraystats.std(pow)
    minval = m - thresh * s * 0.5
    maxval = m + thresh * s
    pow = numpy.clip(pow, minval, maxval)

    return pow
Ejemplo n.º 3
0
def write(a, filename, min=None, max=None, quality=80, newsize=None, stdval=5):
    '''
Write a 2-D numpy array to a JPEG file.
Usage:
	write(array, filename)

Optional arguments 'min' and 'max' will determine what range of the
input array will be scaled to 0-255 in the jpg file.  The default
is to use the following calculation:
   min = mean - 3 * stdev
   max = mean + 3 * stdev

Optional argument 'quality' is used for jpeg quality, a number between
1 and 100.   The default is 80.

Optional argument 'newsize' is used for scaling the image.
	'''

    ## auto determination of range for scaling to 8 bit.
    if min is None:
        mean = arraystats.mean(a)
        std = arraystats.std(a)
        min = mean - stdval * std
    if max is None:
        mean = arraystats.mean(a)
        std = arraystats.std(a)
        max = mean + stdval * std

    ## scale to 8 bit
    a = numpy.clip(a, min, max)
    scale = 255.0 / (max - min)
    a = scale * (a - min)
    a = a.astype(numpy.uint8)

    ## use PIL to write JPEG
    imsize = a.shape[1], a.shape[0]
    nstr = a.tostring()
    image = Image.fromstring('L', imsize, nstr, 'raw', 'L', 0, 1)
    image.convert('L').save(filename, "JPEG", quality=quality)
    if newsize is None:
        image.convert('L').save(filename, "JPEG", quality=quality)
    else:
        image.convert('L').resize(newsize).save(filename,
                                                "JPEG",
                                                quality=quality)
Ejemplo n.º 4
0
def write(a, filename, min=None, max=None, quality=80, newsize=None):
	'''
Write a 2-D numpy array to a JPEG file.
Usage:
	write(array, filename)

Optional arguments 'min' and 'max' will determine what range of the
input array will be scaled to 0-255 in the jpg file.  The default
is to use the following calculation:
   min = mean - 3 * stdev
   max = mean + 3 * stdev

Optional argument 'quality' is used for jpeg quality, a number between
1 and 100.   The default is 80.

Optional argument 'newsize' is used for scaling the image.
	'''
	
	## auto determination of range for scaling to 8 bit.
	if min is None:
		mean = arraystats.mean(a)
		std = arraystats.std(a)
		min = mean - 5 * std
	if max is None:
		mean = arraystats.mean(a)
		std = arraystats.std(a)
		max = mean + 5 * std

	## scale to 8 bit
	a = numpy.clip(a, min, max)
	scale = 255.0 / (max - min)
	a = scale * (a - min)
	a = a.astype(numpy.uint8)

	## use PIL to write JPEG
	imsize = a.shape[1], a.shape[0]
	nstr = a.tostring()
	image = Image.fromstring('L', imsize, nstr, 'raw', 'L', 0, 1)
	image.convert('L').save(filename, "JPEG", quality=quality)
	if newsize is None:
		image.convert('L').save(filename, "JPEG", quality=quality)
	else:
		image.convert('L').resize(newsize).save(filename, "JPEG", quality=quality)
def write(a, imfile=None, format=None, limits=None, writefloat=False):
    '''
        Convert array to 8 bit gray scale and save to filename.
        Format is determined from filename extension by PIL.
        '''
    if limits is None:
        mean = arraystats.mean(a)
        std = arraystats.std(a)
        limits = (mean - 3 * std, mean + 3 * std)

    size = a.shape[1], a.shape[0]

    if imfile is None:
        imfile = sys.stdout

    ## try saving float data
    if writefloat and a.dtype.type in (numpy.int64, numpy.float32):
        a = numpy.asarray(a, numpy.float32)
        im = Image.frombuffer('F', size, a, 'raw', 'F', 0, 1)
        try:
            im.save(imfile, format=format)
            return
        except:
            ## assume any exception here means that float32 not supported
            pass

    ## save scaled 8 bit data
    a = imagefun.linearscale(a, limits, (0, 255))
    a = a.clip(0, 255)
    a = numpy.asarray(a, numpy.uint8)
    im = Image.frombuffer('L', size, a, 'raw', 'L', 0, 1)

    try:
        im.save(imfile, format=format)
    except KeyError:
        ## bad file format
        sys.stderr.write('Bad PIL image format.  Try one of these: %s\n' %
                         (pilformats, ))
def write(a, imfile=None, format=None, limits=None, writefloat=False):
        '''
        Convert array to 8 bit gray scale and save to filename.
        Format is determined from filename extension by PIL.
        '''
        if limits is None:
                mean = arraystats.mean(a)
                std = arraystats.std(a)
                limits = (mean-3*std, mean+3*std)

        size = a.shape[1], a.shape[0]

        if imfile is None:
                imfile = sys.stdout

        ## try saving float data
        if writefloat and a.dtype.type in (numpy.int64, numpy.float32):
                a = numpy.asarray(a, numpy.float32)
                im = Image.frombuffer('F', size, a, 'raw', 'F', 0, 1)
                try:
                        im.save(imfile, format=format)
                        return
                except:
                        ## assume any exception here means that float32 not supported
                        pass

        ## save scaled 8 bit data
        a = imagefun.linearscale(a, limits, (0,255))
        a = a.clip(0,255)
        a = numpy.asarray(a, numpy.uint8)
        im = Image.frombuffer('L', size, a, 'raw', 'L', 0, 1)

        try:
                im.save(imfile, format=format)
        except KeyError:
                ## bad file format
                sys.stderr.write('Bad PIL image format.  Try one of these: %s\n' % (pilformats,))