Create a new image using the supplied shape as a mask from which to cut out pixels from the
supplied image. Pixels inside the shape will be added to the final image, pixels outside
the shape will be clear.
961810
public static BufferedImage composeMaskedImage (
        ImageCreator isrc, Shape mask, BufferedImage base)
    {
        int wid = base.getWidth();
        int hei = base.getHeight();

        // alternate method for composition:
        // 1. create WriteableRaster with base data
        // 2. test each pixel with mask.contains() and set the alpha channel to fully-alpha if false
        // 3. create buffered image from raster
        // (I didn't use this method because it depends on the colormodel of the source image, and
        // was booching when the souce image was a cut-up from a tileset, and it seems like it
        // would take longer than the method we are using. But it's something to consider)

        // composite them by rendering them with an alpha rule
        BufferedImage target = isrc.createImage(wid, hei, Transparency.TRANSLUCENT);
        Graphics2D g2 = target.createGraphics();
        try {
            g2.setColor(Color.BLACK); // whatever, really
            g2.fill(mask);
            g2.setComposite(AlphaComposite.SrcIn);
            g2.drawImage(base, 0, 0, null);
        } finally {
            g2.dispose();
        }
        return target;
    }
102704
public static GridCoverage2D cut( GridCoverage2D raster, GridCoverage2D mask ) throws Exception {
        OmsCutOut cutDrain = new OmsCutOut();
        cutDrain.inRaster = raster;
        cutDrain.inMask = mask;
        cutDrain.process();
        return cutDrain.outRaster;
    }
1024436
private void frameShape(final Shape pShape) {
        if (isPenVisible()) {
            setupForPaint();

            Stroke stroke = getStroke(penSize);
            Shape shape = stroke.createStrokedShape(pShape);
            graphics.draw(shape);
        }
    }
478060
def mask( m )
      check_shape m
      left = MultiArray.new typecode, *( shape.first( dimension - m.dimension ) +
                                         [ m.size ] )
      index = Hornetseye::Pointer( INT ).new
      index.store INT.new( 0 )
      block = Mask.new left, self, m, index
      if block.compilable?
        GCCFunction.run block
      else
        block.demand
      end
      left[0 ... index[]].roll
    end
1294604
def apply_palette(img, palette, options):

    '''

    '''

    if not options.quiet:
        print('  applying palette...')

    bg_color = palette[0]

    fg_mask = get_fg_mask(bg_color, img, options)

    orig_shape = img.shape

    pixels = img.reshape((-1, 3))
    fg_mask = fg_mask.flatten()

    num_pixels = pixels.shape[0]

    labels = np.zeros(num_pixels, dtype=np.uint8)

    labels[fg_mask], _ = vq(pixels[fg_mask], palette)

    return labels.reshape(orig_shape[:-1])
88126
def cutout_source(x_pos, y_pos, image, kernelsize, shift=True):
    
    if kernelsize % 2 == 0:
        raise ValueError("even pixel number kernel size not supported!")
    x_int = int(round(x_pos))
    y_int = int(round(y_pos))
    n = len(image)
    d = (kernelsize - 1)/2
    x_max = int(np.minimum(x_int + d + 1, n))
    x_min = int(np.maximum(x_int - d, 0))
    y_max = int(np.minimum(y_int + d + 1, n))
    y_min = int(np.maximum(y_int - d, 0))
    image_cut = copy.deepcopy(image[y_min:y_max, x_min:x_max])
    shift_x = x_int - x_pos
    shift_y = y_int - y_pos
    if shift is True:
        kernel_shift = de_shift_kernel(image_cut, shift_x, shift_y, iterations=50)
    else:
        kernel_shift = image_cut
    kernel_final = np.zeros((kernelsize, kernelsize))

    k_l2_x = int((kernelsize - 1) / 2)
    k_l2_y = int((kernelsize - 1) / 2)

    xk_min = np.maximum(0, -x_int + k_l2_x)
    yk_min = np.maximum(0, -y_int + k_l2_y)
    xk_max = np.minimum(kernelsize, -x_int + k_l2_x + n)
    yk_max = np.minimum(kernelsize, -y_int + k_l2_y + n)

    kernel_final[yk_min:yk_max, xk_min:xk_max] = kernel_shift
    return kernel_final
799144
def cut_edges(image, numPix):
    
    nx, ny = image.shape
    if nx < numPix or ny < numPix:
        print('WARNING: image can not be resized, in routine cut_edges.')
        return image
    if nx % 2 == 0 or ny % 2 == 0 or numPix % 2 == 0:
        #pass
        print("WARNING: image or cutout side are even number. This routine only works for odd numbers %s %s %s"
                         % (nx, ny, numPix))
    cx = int((nx-1)/2)
    cy = int((ny-1)/2)
    d = int((numPix-1)/2)
    if nx % 2 == 0:
        cx += 1
    if ny % 2 == 0:
        cy += 1
    resized = image[cx-d:cx+d+1, cy-d:cy+d+1]
    return copy.deepcopy(resized)
1095268
function(shape){
        var remove=shape;
        if(remove.parent!=this)return;
        var prev=remove.prev;
        var next=remove.next;
        if(prev!=null)prev.next=next;
        if(next!=null)next.prev=prev;
        if(this.shapes==remove)this.shapes=next;
        remove.prev=null;
        remove.next=null;
        remove.parent=null;
        if(this.parent)this.parent.removeShape(remove);
        this.numShapes--;
    }
1370896
def cropping_mask
      return if crop_from.blank? || crop_size.blank?
      crop_from = point_from_string(read_attribute(:crop_from))
      crop_size = sizes_from_string(read_attribute(:crop_size))

      point_and_mask_to_points(crop_from, crop_size)
    end
344346
def _set_mask(self, mask):
        
        self.mask = mask.copy()
        if self.h5 is not None:
            if 'mask' in self.h5:
                self.h5.pop('mask')
            self.h5.create_dataset('mask', mask.shape, dtype=self.mask.dtype, data=mask, compression="gzip", shuffle=True)
819473
def set_shape(self, id, new_shape):
        """"""
        old_shape = self.id_to_shape[id]
        old_buffer = self.get_buffer(old_shape)
        model, color = old_buffer.get(id)
        new_data = self._create_turtle(id, new_shape, model, color)
        old_buffer.remove(id)
        self.id_to_shape[id] = new_shape
        return new_data
84641
def _remove_hidden_parts(projected_surface):
    
    surface = np.copy(projected_surface)
    surface[~_make_occlusion_mask(projected_surface)] = np.nan
    return surface
537421
def get_mask_from_raster(rasterfile, outmaskfile, keep_nodata=False):
        
        raster_r = RasterUtilClass.read_raster(rasterfile)
        xsize = raster_r.nCols
        ysize = raster_r.nRows
        nodata_value = raster_r.noDataValue
        srs = raster_r.srs
        x_min = raster_r.xMin
        y_max = raster_r.yMax
        dx = raster_r.dx
        data = raster_r.data

        if not keep_nodata:
            i_min = ysize - 1
            i_max = 0
            j_min = xsize - 1
            j_max = 0
            for i in range(ysize):
                for j in range(xsize):
                    if abs(data[i][j] - nodata_value) > DELTA:
                        i_min = min(i, i_min)
                        i_max = max(i, i_max)
                        j_min = min(j, j_min)
                        j_max = max(j, j_max)

            # print(i_min, i_max, j_min, j_max)
            y_size_mask = i_max - i_min + 1
            x_size_mask = j_max - j_min + 1
            x_min_mask = x_min + j_min * dx
            y_max_mask = y_max - i_min * dx
        else:
            y_size_mask = ysize
            x_size_mask = xsize
            x_min_mask = x_min
            y_max_mask = y_max
            i_min = 0
            j_min = 0
        print('%dx%d -> %dx%d' % (xsize, ysize, x_size_mask, y_size_mask))

        mask = numpy.zeros((y_size_mask, x_size_mask))

        for i in range(y_size_mask):
            for j in range(x_size_mask):
                if abs(data[i + i_min][j + j_min] - nodata_value) > DELTA:
                    mask[i][j] = 1
                else:
                    mask[i][j] = DEFAULT_NODATA

        mask_geotrans = [x_min_mask, dx, 0, y_max_mask, 0, -dx]
        RasterUtilClass.write_gtiff_file(outmaskfile, y_size_mask, x_size_mask, mask,
                                         mask_geotrans, srs, DEFAULT_NODATA, GDT_Int32)
        return Raster(y_size_mask, x_size_mask, mask, DEFAULT_NODATA, mask_geotrans, srs)
848513
def mask_xdata_with_shape(self, shape: DataAndMetadata.ShapeType) -> DataAndMetadata.DataAndMetadata:
        
        mask = self._graphic.get_mask(shape)
        return DataAndMetadata.DataAndMetadata.from_data(mask)
1073152
function(pixel, drawing) {
        if(!this.line) {
            this.createFeature(pixel);
        }
        var lonlat = this.layer.getLonLatFromViewPortPx(pixel); 
        this.point.geometry.x = lonlat.lon;
        this.point.geometry.y = lonlat.lat;
        this.callback("modify", [this.point.geometry, this.getSketch(), drawing]);
        this.point.geometry.clearBounds();
        this.drawFeature();
    }
273137
def removeSinglePixels(img):
    '''
    
    '''

    gx = img.shape[0]
    gy = img.shape[1]

    for i in range(gx):
        for j in range(gy):

            if img[i, j]:

                found_neighbour = False
                for ii in range(max(0, i - 1), min(gx, i + 2)):
                    for jj in range(max(0, j - 1), min(gy, j + 2)):

                        if ii == i and jj == j:
                            continue

                        if img[ii, jj]:
                            found_neighbour = True
                            break
                    if found_neighbour:
                        break

                if not found_neighbour:
                    img[i, j] = 0
263201
def _update_mask(self, mask: np.ndarray, polygons: Iterable, separate_instances: bool = False) -> None:
        
        for i, p in enumerate(polygons):
            if isinstance(p, geometry.MultiPolygon):
                self._update_mask(mask=mask, polygons=p.geoms, separate_instances=True)
                continue
            elif not isinstance(p, geometry.Polygon):
                continue
            poly_area = Image.fromarray(np.zeros(mask.shape, dtype=np.uint8))
            holes = Image.fromarray(np.zeros(mask.shape, dtype=np.uint8))
            outline_color = 0 if separate_instances else 255
            ImageDraw.Draw(poly_area).polygon(p.exterior.coords, fill=255, outline=outline_color)
            for h in p.interiors:
                ImageDraw.Draw(holes).polygon(h.coords, fill=255, outline=255)
            mask[np.nonzero(np.array(poly_area, dtype=np.uint8))] = 255
            mask[np.nonzero(np.array(holes, dtype=np.uint8))] = 0