Esempio n. 1
0
def exportImageWindow(w,
                      basepath,
                      roi=None,
                      scale=None,
                      bgFilename=None,
                      overlayHandler=addStandardOverlay):
    """Exports the currently displayed contents of the ImageWindow `w`
    as XFig file <basepath>.fig.

    `roi` may be a specific ROI to save, or `True` to save the visible
    image region.  The default (None) saves the whole image.

    If `bgFilename` is not given, the displayed background image is
    saved as <basepath>_bg.png and used as background.  The background
    can be disabled entirely by setting `bgFilename` to `False`.

    Overlays are saved using the given overlayHandler.  The default
    handler (addStandardOverlay) can handle the standard vigrapyqt4
    point, edge, and circle overlays."""

    figFilename = basepath + ".fig"
    pngFilename = basepath + "_bg.png"

    # determine ROI to be saved
    if roi == None:
        roi = Rect2D(w.image.size())
    elif roi == True:
        roi = visibleROI(w)
    elif type(roi) == tuple:
        roi = Rect2D(*roi)
    elif type(roi) == str:
        roi = Rect2D(*fig.parseGeometry(roi))

    if bgFilename == None:
        # create .png background
        image, normalize = w.getDisplay()
        image.subImage(roi).write(pngFilename, normalize)
        _, bgFilename = os.path.split(pngFilename)

    # create .fig file
    if scale == None:
        scale = 20 * 450 / roi.width()  # default: 20cm width
        print "auto-adjusted scale to %s." % (scale, )
    fe = FigExporter(scale, roi)
    if bgFilename != False:
        fe.addBackgroundWithFrame(bgFilename, depth=100, lineWidth=0)
    else:
        fe.addROIRect(depth=100, lineWidth=0)

    _exportOverlays(fe, w.viewer.overlays, overlayHandler)
    fe.save(figFilename)

    return fe
Esempio n. 2
0
    def addPixelRaster(self,
                       rect=None,
                       labels=None,
                       fill=None,
                       container=True,
                       labelType=int,
                       **attr):
        """The default label size looks good with 1cm^2 pixels
        (scale = 450)."""

        assert rect or labels or fill
        if rect is None:
            rect = Rect2D((labels or fill).size())
        elif not hasattr(rect, "upperLeft"):
            w, h = rect
            rect = Rect2D((w, h))

        if container == True:
            container = self.f
        result = fig.Compound(container)

        for x, y in meshIter(rect):
            boxX1 = (x - rect.left()) * self.scale
            boxY1 = (y - rect.top()) * self.scale
            boxX2 = boxX1 + self.scale
            boxY2 = boxY1 + self.scale

            pixelRect = fig.PolyBox(boxX1, boxY1, boxX2, boxY2)
            for a in attr:
                setattr(pixelRect, a, attr[a])

            if labels:
                textX = (boxX1 + boxX2) / 2
                textY = (boxY1 + boxY2) / 2

                label = fig.Text(fig.Vector(textX, textY),
                                 str(labelType(labels[x, y])),
                                 alignment=fig.Alignment.Centered)
                label.pos += (0, label.height / 2)

                label.depth = pixelRect.depth - 10
                label.font = fig.Font.Helvetica
                result.append(label)

            if fill:
                pixelRect.fillStyle = fig.FillStyle.Solid
                pixelRect.fillColor = self.f.getColor(int(fill[x, y]))

            result.append(pixelRect)
        return result
Esempio n. 3
0
    def addBackgroundWithFrame(self,
                               bgImageFilename,
                               container=True,
                               **params):
        """fe.addBackgroundWithFrame(bgImageFilename, depth = 85, ...)

        Adds a picture object to the fig.File, framed by an additional
        rectangle.  See addROIRect() and addImage().  If no roi is
        given (via a keyword parameter), the image file is opened
        (using readImage) and its size is used to initialize a
        BoundingBox positioned at the origin.

        Returns the pair (bgImage, bgRect) of both added fig objects."""

        if not params.has_key("roi") and not self.roi:
            size = readImage(bgImageFilename).size()
            params["roi"] = Rect2D(size)
        if container == True:
            container = self.f

        if not params.has_key("depth"):
            params["depth"] = 1

        bgRect = self.addROIRect(**params)

        bgImage = fig.PictureBBox(0, 0, 1, 1, bgImageFilename)
        bgImage.points = list(bgRect.points)
        bgImage.depth = 999
        container.append(bgImage)

        return bgImage, bgRect
Esempio n. 4
0
    def __init__(self,
                 parent=None,
                 imageSize=None,
                 roi=None,
                 viewer=None,
                 color=QtCore.Qt.yellow,
                 width=0,
                 alwaysVisible=True):
        QImageViewerTool.__init__(self, parent)
        self._painting = False
        self._alwaysVisible = False
        self.roi = roi

        self.color = color
        self.width = width

        if viewer:
            self._viewer = viewer
        else:
            self._viewer = parent.viewer
            if imageSize == None and hasattr(parent, 'image'):
                imageSize = parent.image.size()

        self._validRect = imageSize and Rect2D(imageSize)

        self.setVisible(alwaysVisible)

        self.connectViewer(self._viewer)
Esempio n. 5
0
 def _redisplayROIImage(self, roi):
     roi &= Rect2D(self.map.imageSize())
     roiImage = self._labelImage().subImage(roi)
     if self._backgroundMode > 3:
         roiImage = self._faceMeans.regionImage(roiImage)
     # FIXME: use global normalization here
     self.viewer.replaceROI(roiImage.toPNM(vigra.BYTE),
                            QtCore.QPoint(*roi.upperLeft()))
Esempio n. 6
0
 def mouseMoved(self, x, y):
     if not self._painting: return
     # TODO: update overlay
     x1, y1 = self.startPos
     x, y = intPos((x, y))
     self.setROI(
         Rect2D(min(x1, x), min(y1, y),
                max(x1, x) + 1,
                max(y1, y) + 1))
Esempio n. 7
0
def cellImage2display(cellImage,
                      background=None,
                      nodeColor=(0, 0, 255),
                      edgeColor=(255, 0, 0)):
    assert False, "cellImage2display needs to be ported to vigranumpy"
    if hasattr(cellImage, "cellImage"):
        cellImage = cellImage.cellImage
    if hasattr(cellImage, "serialize"):
        cellImage = cellImage.serialize()
    if background is None:
        background = cellImage / 4
    if background.bands() < 3:
        background = vigra.RGBImage(background)
    if background.size() != cellImage.size():
        nbg = vigra.RGBImage(cellImage.size())
        shift = Point2D((cellImage.width() - background.width()) / 2,
                        (cellImage.height() - background.height()) / 2)
        nbg.subImage(Rect2D(shift, background.size())).copyValues(background)
        background = nbg
    return transformImage(
        cellImage, background,
        "\l ci, b: ci %% 4 == 1 ? %r : (ci %% 4 == 2 ? %r : b)" %
        (edgeColor, nodeColor))
Esempio n. 8
0
def visibleROI(imageWindow):
    viewer = hasattr(imageWindow, "viewer") \
             and imageWindow.viewer or imageWindow
    return Rect2D(
        intPos(viewer.toImageCoordinates(0, 0)),
        intPos(viewer.toImageCoordinates(viewer.width(), viewer.height())))
Esempio n. 9
0
    if scale == None:
        scale = 20 * 450 / roi.width()  # default: 20cm width
        print "auto-adjusted scale to %s." % (scale, )
    fe = FigExporter(scale, roi)
    if bgFilename != False:
        fe.addBackgroundWithFrame(bgFilename, depth=100, lineWidth=0)
    else:
        fe.addROIRect(depth=100, lineWidth=0)

    _exportOverlays(fe, w.viewer.overlays, overlayHandler)
    fe.save(figFilename)

    return fe


# --------------------------------------------------------------------
#                               USAGE
# --------------------------------------------------------------------

if False:
    # default scale, no ROI:
    fe = FigExporter()
    # use given scale and ROI:
    fe = FigExporter(scale=15, roi=BoundingBox(Rect2D(dm.imageSize())))
    # add background image:
    fe.addBackgroundWithFrame("background.png")
    fe.addMapEdges(someMap)  # give optional properties like lineWidth = 4
    someRadius = 0.1  # pixels
    fe.addMapNodes(someMap, someRadius)
    fe.saveEPS("example_basename")