Get the a range of image intensities
    that most pixels are in with
500237
def _getMinMax(img):
    '''
    
    '''
    av = np.mean(img)
    std = np.std(img)
    # define range for segmentation:
    mn = av - 3 * std
    mx = av + 3 * std

    return max(img.min(), mn, 0), min(img.max(), mx)
795945
def _extract_mask_distance(image, mask = slice(None), voxelspacing = None):
    
    if isinstance(mask, slice):
        mask = numpy.ones(image.shape, numpy.bool)
    
    distance_map = distance_transform_edt(mask, sampling=voxelspacing)
    
    return _extract_intensities(distance_map, mask)
157569
def _get_aeff_corrections(intensity_ratio, mask):
        
        nebins = len(intensity_ratio.data)
        aeff_corrections = np.zeros((nebins))
        for i in range(nebins):
            bright_pixels_intensity = intensity_ratio.data[i][mask.data[i]]
            mean_bright_pixel = bright_pixels_intensity.mean()
            aeff_corrections[i] = 1. / mean_bright_pixel

        print("Aeff correction: ", aeff_corrections)
        return aeff_corrections
1350594
def rescale_pixel_values(images, order="C"):
    
    images = np.asarray(images, order=order).astype("float32")
    images -= images.min()
    images /= images.max()
    return images
692898
def pixel(self, coord_x, coord_y):
        # type: (int, int) -> Pixel
        

        try:
            return self.pixels[coord_y][coord_x]  # type: ignore
        except IndexError:
            raise ScreenShotError(
                "Pixel location ({}, {}) is out of range.".format(coord_x, coord_y)
            )
415724
def _as_rescale(self, get, targetbitdepth):
        """"""

        width,height,pixels,meta = get()
        maxval = 2**meta['bitdepth'] - 1
        targetmaxval = 2**targetbitdepth - 1
        factor = float(targetmaxval) / float(maxval)
        meta['bitdepth'] = targetbitdepth
        def iterscale():
            for row in pixels:
                yield map(lambda x: int(round(x*factor)), row)
        if maxval == targetmaxval:
            return width, height, pixels, meta
        else:
            return width, height, iterscale(), meta
1136825
def _pixelsize(self, p):
        """"""
        xpixelsize = 1./float(p.xdensity)
        ypixelsize = 1./float(p.ydensity)
        return max([xpixelsize,ypixelsize])
223973
def _fill_masked_intensity_resid(intensity_resid, bright_pixel_mask):
        
        filled_intensity = np.zeros((intensity_resid.data.shape))
        nebins = len(intensity_resid.data)
        for i in range(nebins):
            masked = bright_pixel_mask.data[i]
            unmasked = np.invert(masked)
            mean_intensity = intensity_resid.data[i][unmasked].mean()
            filled_intensity[i] = np.where(masked, mean_intensity, intensity_resid.data[i])
        return HpxMap(filled_intensity, intensity_resid.hpx)
522364
def getPixelAttrMost(self, x, y):
        ''
        r = self.pixels[y][x]
        c = sorted((len(rows), attr, rows) for attr, rows in list(r.items()) if attr and attr not in self.hiddenAttrs)
        if not c:
            return 0
        _, attr, rows = c[-1]
        if isinstance(self.source, BaseSheet) and anySelected(self.source, rows):
            attr = CursesAttr(attr, 8).update_attr(colors.color_graph_selected, 10).attr
        return attr
689829
def getPixels(self):
        
        array = self.toArray()
        (width, height, depth) = array.size
        for x in range(width):
            for y in range(height):
                yield Pixel(array, x, y)
1295898
def constant_image_value(image, crs='EPSG:32613', scale=1):
    """"""
    return getinfo(ee.Image(image).reduceRegion(
        reducer=ee.Reducer.first(), scale=scale,
        geometry=ee.Geometry.Rectangle([0, 0, 10, 10], crs, False)))
1249360
def intensity(image):
    '''
    '''
    if len(image.shape) > 2:
        # Convert to grayscale
        image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) / 255
    elif issubclass(image.dtype.type, np.integer):
        image /= 255
    return float(np.sum(image) / np.prod(image.shape))
1312524
function analyzePixels() {

    var len = pixels.length;
    var nPix = len / 3;
    indexedPixels = [];
    var nq = new NeuQuant(pixels, len, sample);

    // initialize quantizer
    colorTab = nq.process(); // create reduced palette

    // map image pixels to new palette
    var k = 0;
    for (var j = 0; j < nPix; j++) {
      var index = nq.map(pixels[k++] & 0xff, pixels[k++] & 0xff, pixels[k++] & 0xff);
      usedEntry[index] = true;
      indexedPixels[j] = index;
    }

    pixels = null;
    colorDepth = 8;
    palSize = 7;

    // get closest match to transparent color if specified
    if (transparent !== null) {
      transIndex = findClosest(transparent);
    }
  }
1357249
def get(img, light=False):
    """"""
    colors = gen_colors(img)
    return adjust(colors, light)
1231002
def most_free_pixel(self):
        
        dist_tf = self.to_distance_im()
        max_px = np.where(dist_tf == np.max(dist_tf))
        free_pixel = np.array([max_px[0][0], max_px[1][0]])
        return free_pixel
840290
def bbox(self):
        

        # (stop - 1) to return the max pixel location, not the slice index
        return (self._slice[0].start, self._slice[1].start,
                self._slice[0].stop - 1, self._slice[1].stop - 1) * u.pix
953450
def numberOfTilesAtZoom(self, zoom):
        ""
        [minRow, minCol, maxRow, maxCol] = self.getExtentAddress(zoom)
        return (maxCol - minCol + 1) * (maxRow - minRow + 1)