class SetPixelFloat(UsesQApplication):
    '''Test case for calling setPixel with float as argument'''
    def setUp(self):
        #Acquire resources
        super(SetPixelFloat, self).setUp()
        self.color = qRgb(255, 0, 0)
        self.image = QImage(200, 200, QImage.Format_RGB32)

    def tearDown(self):
        #Release resources
        del self.color
        del self.image
        super(SetPixelFloat, self).tearDown()

    def testFloat(self):
        #QImage.setPixel(float, float, color) - Implicit conversion
        self.image.setPixel(3.14, 4.2, self.color)
        self.assertEqual(self.image.pixel(3.14, 4.2), self.color)
class SetPixelFloat(UsesQApplication):
    '''Test case for calling setPixel with float as argument'''

    def setUp(self):
        #Acquire resources
        super(SetPixelFloat, self).setUp()
        self.color = qRgb(255, 0, 0)
        self.image = QImage(200, 200, QImage.Format_RGB32)

    def tearDown(self):
        #Release resources
        del self.color
        del self.image
        super(SetPixelFloat, self).tearDown()

    def testFloat(self):
        #QImage.setPixel(float, float, color) - Implicit conversion
        self.image.setPixel(3.14, 4.2, self.color)
        self.assertEqual(self.image.pixel(3.14, 4.2), self.color)
Example #3
0
    def populateScene(self):

        self.scene = QGraphicsScene(self)

        image = QImage(":/qt4logo.png")
        xx: int = 0
        nitems: int = 0
        for i in range(-11000, 11000, 110):
            xx += 1
            yy: int = 0
            for j in range(-7000, 7000, 70):
                yy += 1
                x = (i + 11000) / 22000.0
                y = (j + 7000) / 14000.0

                color = QColor(
                    image.pixel(int(image.width() * x),
                                int(image.height() * y)))
                item = Chip(color, xx, yy)
                item.setPos(QPointF(i, j))
                self.scene.addItem(item)
                nitems += 1
Example #4
0
def _test_pipedimagerpq():
    # vertices of a pentagon (roughly) centered in a 1000 x 1000 square
    pentagonpts = (
        (504.5, 100.0),
        (100.0, 393.9),
        (254.5, 869.4),
        (754.5, 869.4),
        (909.0, 393.9),
    )
    linepts = ((350, 50), (200, 150), (400, 250), (300, 350), (150, 250),
               (100, 450))
    # start PyQt
    testapp = QApplication(["PipedImagerPQ"])
    # create the list of commands to submit
    drawcmnds = []
    drawcmnds.append({"action": "setTitle", "title": "Tester"})
    drawcmnds.append({"action": "show"})
    drawcmnds.append({"action": "clear", "color": "black"})
    drawcmnds.append({"action": "screenInfo"})
    # create the image to be displayed
    testimage = QImage(500, 500, QImage.Format_ARGB32_Premultiplied)
    # initialize a black background
    testimage.fill(0xFF000000)
    # draw some things in the image
    testpainter = QPainter(testimage)
    testpainter.setBrush(QBrush(QColor(0, 255, 0, 128), Qt.SolidPattern))
    testpainter.setPen(
        QPen(QBrush(QColor(255, 0, 0, 255), Qt.SolidPattern), 5.0,
             Qt.SolidLine, Qt.SquareCap, Qt.MiterJoin))
    testpainter.drawRect(QRectF(5.0, 255.0, 240.0, 240.0))
    testpainter.setBrush(QBrush(QColor(0, 0, 255, 255), Qt.SolidPattern))
    testpainter.setPen(
        QPen(QBrush(QColor(0, 0, 0, 255), Qt.SolidPattern), 5.0, Qt.DashLine,
             Qt.RoundCap, Qt.RoundJoin))
    testpainter.drawPolygon(
        QPolygonF([
            QPointF(.25 * ptx, .25 * pty + 250) for (ptx, pty) in pentagonpts
        ]))
    testpainter.setBrush(Qt.NoBrush)
    testpainter.setPen(
        QPen(QBrush(QColor(255, 255, 255, 255), Qt.SolidPattern), 3.0,
             Qt.DashLine, Qt.RoundCap, Qt.RoundJoin))
    testpainter.drawPolyline(
        QPolygonF([QPointF(pts, pty) for (pts, pty) in linepts]))
    testpainter.end()
    # add the image command
    testimgwidth = testimage.width()
    testimgheight = testimage.height()
    testimgstride = testimage.bytesPerLine()
    # not a good way to get the pixel data
    testimgdata = bytearray(testimgheight * testimgstride)
    k = 0
    for pty in range(testimgheight):
        for ptx in range(testimgwidth):
            pixval = testimage.pixel(ptx, pty)
            (aval, rgbval) = divmod(pixval, 256 * 256 * 256)
            (rval, gbval) = divmod(rgbval, 256 * 256)
            (gval, bval) = divmod(gbval, 256)
            testimgdata[k] = bval
            k += 1
            testimgdata[k] = gval
            k += 1
            testimgdata[k] = rval
            k += 1
            testimgdata[k] = aval
            k += 1
    testblocksize = 4000
    testnumblocks = (testimgheight * testimgstride + testblocksize -
                     1) // testblocksize
    drawcmnds.append({
        "action": "newImage",
        "width": testimgwidth,
        "height": testimgheight,
        "stride": testimgstride
    })
    for k in range(testnumblocks):
        if k < (testnumblocks - 1):
            blkdata = testimgdata[k * testblocksize:(k + 1) * testblocksize]
        else:
            blkdata = testimgdata[k * testblocksize:]
        drawcmnds.append({
            "action": "newImage",
            "blocknum": k + 1,
            "numblocks": testnumblocks,
            "startindex": k * testblocksize,
            "blockdata": blkdata
        })
    # finish the command list
    drawcmnds.append({"action": "show"})
    drawcmnds.append({"action": "exit"})
    # create a PipedImagerPQ in this process
    (cmndrecvpipe, cmndsendpipe) = multiprocessing.Pipe(False)
    (rspdrecvpipe, rspdsendpipe) = multiprocessing.Pipe(False)
    testviewer = PipedImagerPQ(cmndrecvpipe, rspdsendpipe)
    # create a command submitter dialog
    tester = _CommandSubmitterPQ(testviewer, cmndsendpipe, rspdrecvpipe,
                                 drawcmnds)
    tester.show()
    # let it all run
    testresult = testapp.exec_()
    if testresult != 0:
        sys.exit(testresult)
Example #5
0
class IconEditor(QWidget):
    def __init__(self):
        super(IconEditor, self).__init__()
        self.setAttribute(Qt.WA_StaticContents)
        self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
        self.curColor = Qt.black
        self.zoom = 8 * 1
        self.image = QImage(16, 16, QImage.Format_ARGB32)
        self.image.fill(qRgba(0, 0, 0, 0))

    def penColor(self):
        return self.curColor

    def setPenColor(self, newColor):
        self.curColor = newColor

    def zoomFactor(self):
        return self.zoom

    def setZoomFactor(self, newZoom):
        if newZoom < 1:
            newZoom = 1

        if newZoom != self.zoom:
            self.zoom = newZoom
            self.update()
            self.updateGeometry()

    def iconImage(self):
        return self.image

    def setIconImage(self, newImage):

        if newImage != self.image:
            print('updating image')
            self.image = newImage.convertToFormat(QImage.Format_ARGB32)
            self.update()
            self.updateGeometry()

    def sizeHint(self):
        size = self.zoom * self.image.size()
        if self.zoom >= 3:
            size += QSize(1, 1)
        return size

    def pixelRect(self, i, j):
        if self.zoom >= 3:
            return QRect(self.zoom * i + 1, self.zoom * j + 1, self.zoom - 1,
                         self.zoom - 1)
        else:
            return QRect(self.zoom * i, self.zoom * j, self.zoom, self.zoom)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.setImagePixel(event.pos(), True)
            print('mouse left press')
        elif event.button() == Qt.RightButton:
            self.setImagePixel(event.pos(), False)
            print('mouse right press')

    def mouseMoveEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.setImagePixel(event.pos(), True)
        elif event.buttons() & Qt.RightButton:
            self.setImagePixel(event.pos(), False)

    def setImagePixel(self, pos, opaque):
        i = pos.x() // self.zoom
        j = pos.y() // self.zoom
        print(f'setting pixel ({i}, {j})')
        if self.image.rect().contains(i, j):
            if opaque:
                penC = QColor('black')
                self.image.setPixel(i, j, penC.rgba())
                # self.image.setPixel(i, j, QColor(255, i*2.56, j*2.56, 255).rgb())
            else:
                print('#' * 10)
                self.image.setPixel(QPoint(i, j), qRgba(0, 0, 0, 0))
            print(f'Pixel Rect: {self.pixelRect(i,j)}')
            self.update(self.pixelRect(i, j))

    def paintEvent(self, event):
        painter = QPainter(self)
        if self.zoom >= 3:
            painter.setPen(self.palette().foreground().color())
            # painter.setPen(QPen('red'))
            for i in range(0, self.image.width()):
                painter.drawLine(self.zoom * i, 0, self.zoom * i,
                                 self.zoom * self.image.height())
            for j in range(0, self.image.height()):
                painter.drawLine(0, self.zoom * j,
                                 self.zoom * self.image.width(), self.zoom * j)

        for i in range(0, self.image.width()):
            for j in range(0, self.image.height()):
                rect = self.pixelRect(i, j)
                if event.region().intersected(rect):
                    color = QColor.fromRgba(self.image.pixel(i, j))
                    if color.alpha() < 255:
                        painter.fillRect(rect, Qt.white)
                    painter.fillRect(rect, color)

    penColorProperty = property(QColor, penColor, setPenColor)
    iconImageProperty = property(QImage, iconImage, setIconImage)
    zoomFactorProperty = property(int, zoomFactor, setZoomFactor)