Пример #1
0
def renderLineSampleImage(line, width, dpi = 100):
    import Debug
#    Debug.printCall(None, (line, width))
    from matplotlib.backends.backend_agg import RendererAgg as Renderer
    from matplotlib.lines import Line2D
    useValue = True
    try:
        from matplotlib.transforms import Value
    except ImportError:
        useValue = False

    from PyQt4 import QtGui

    attributes = ["antialiased", "color", "dash_capstyle", "dash_joinstyle",
                  "linestyle", "linewidth", "marker", "markeredgecolor",
                  "markeredgewidth", "markerfacecolor", "markersize",
                  "solid_capstyle", "solid_joinstyle"]

    lineAttributes = dict()
    for attribute in attributes:
        lineAttributes[attribute] = getattr(line, "get_" + attribute)()

    height = max(lineAttributes["linewidth"], lineAttributes["markersize"] * 1.4 + 2 + 2*lineAttributes["markeredgewidth"])
    pixmapWidth = int(width + lineAttributes["markersize"] * 1.4 + 2 + 2*lineAttributes["markeredgewidth"]) + 1
    markerSize = lineAttributes["markersize"]
    if(useValue):
        renderer = Renderer(pixmapWidth, height, Value(dpi))
    else:
        renderer = Renderer(pixmapWidth, height, dpi)

    linePos = int(height / 2 + 1)
    sampleLine = Line2D([markerSize, pixmapWidth - markerSize], [linePos, linePos], **lineAttributes)
    sampleLine.draw(renderer)

    lineImageStr = renderer.tostring_argb()
    lineARGB = [map(ord, lineImageStr[i:i+4]) for i in xrange(0, len(lineImageStr), 4)]

    image = QtGui.QImage(pixmapWidth, height, QtGui.QImage.Format_ARGB32)
    for x in xrange(pixmapWidth):
        for y in xrange(int(height)):
            argb = lineARGB[x + y * pixmapWidth]
            image.setPixel(x, y, QtGui.qRgba(argb[1], argb[2], argb[3], argb[0]))
    return image