예제 #1
0
 def produce_impl(self):
     dbu = self.layout.dbu
     # resolve path
     info = pya.QFileInfo(self.path)
     if info.isRelative():
         view = pya.Application.instance().main_window().current_view()
         designfile = view.active_cellview().filename()
         if designfile == "":
             self.error = "Error: In order to use relative file path, design must be saved first"
             return
         designdir = pya.QFileInfo(designfile).dir()
         path = designdir.absoluteFilePath(self.path)
     else:
         path = self.path
     # open image
     image = pya.QImage(path)
     if image.isNull():
         self.error = "Error opening image"
         return
     image = image.convertToFormat(pya.QImage.Format_Grayscale8)
     width = image.width()
     height = image.height()
     tilesx = math.ceil(width / self.t)
     tilesy = math.ceil(height / self.t)
     for tiley in range(tilesy):
         for tilex in range(tilesx):
             tile = image.copy(tilex * self.t, tiley * self.t, self.t,
                               self.t)
             polygons = []
             # generate pixels
             rangex = rangey = self.t
             if self.t * (tilex + 1) > width:
                 rangex = width % self.t
             if self.t * (tiley + 1) > height:
                 rangey = height % self.t
             for y in range(rangey):
                 for x in range(rangex):
                     color = pya.QColor(tile.pixel(x, y))
                     color = (color.red + color.green + color.blue) / 3
                     if (color > self.th
                             and not self.inv) or (color <= self.th
                                                   and self.inv):
                         x1 = (tilex * self.t + x) * self.px / dbu
                         y1 = -(tiley * self.t + y) * self.px / dbu
                         x2 = (tilex * self.t + x + 1) * self.px / dbu
                         y2 = -(tiley * self.t + y + 1) * self.px / dbu
                         polygons.append(
                             pya.Polygon(
                                 pya.Box(
                                     pya.Point.from_dpoint(
                                         pya.DPoint(x1, y1)),
                                     pya.Point.from_dpoint(
                                         pya.DPoint(x2, y2)))))
             # merge
             processor = pya.EdgeProcessor()
             merged = processor.simple_merge_p2p(polygons, False, False)
             for polygon in merged:
                 self.cell.shapes(self.l_layer).insert(polygon)
     self.error = None
예제 #2
0
파일: qtbinding.py 프로젝트: seeqc/klayout
    def test_52(self):

        # issue #708 (Image serialization to QByteArray)
        img = pya.QImage(10, 10, pya.QImage.Format_Mono)
        img.fill(0)

        buf = pya.QBuffer()
        img.save(buf, "PNG")

        self.assertEqual(len(buf.data) > 100, True)
        self.assertEqual(buf.data[0:8], b'\x89PNG\r\n\x1a\n')
예제 #3
0
    def select_file_button_clicked(self, checked):
        """ Event handler: button clicked """
        image_path = pya.QFileDialog.getOpenFileName(self)
        self.input_text.setText(image_path)

        #print("path = {}".format(absolute_path))
        #pixmap = QPixmap(image_path)
        #pixmap = pixmap.scaled(320, 320, Qt_AspectRatioMode.KeepAspectRatio)
        #self.raw_image_label.setPixmap(pixmap)

        with open(image_path, "rb") as f:
            content = f.read()

        self.raw_image = pya.QImage()
        self.raw_image.loadFromData(content)
        self.raw_image = self.raw_image.convertToFormat(
            pya.QImage.Format.Format_Grayscale8)

        self.image_array = [[0] * self.raw_image.height()
                            for _ in range(self.raw_image.width())]
        print("init empty array size: {} x {}".format(len(
            self.image_array), len(self.image_array[0])))

        total_pixel_count = self.raw_image.width() * self.raw_image.height()
        count = 0
        for x in range(self.raw_image.width()):
            for y in range(self.raw_image.height()):
                pixel_value = self.raw_image.pixel(x, y)
                pixel_color = pya.QColor(pixel_value)
                #pixel_rgb = (pixel_color.red, pixel_color.green, pixel_color.blue)
                pixel_gray = int(pixel_color.red * 0.299 +
                                 pixel_color.green * 0.587 +
                                 pixel_color.blue * 0.114)
                #print(x, y, pixel_gray)
                self.image_array[x][y] = pixel_gray
                count += 1

            self.progress_bar.setValue(int(count / total_pixel_count))

        pixmap = pya.QPixmap.fromImage(self.raw_image)
        pixmap = pixmap.scaled(480, 480,
                               pya.Qt.AspectRatioMode.KeepAspectRatio)
        self.raw_image_label.setPixmap(pixmap)
        self.raw_image_label.show()

        self.raw_image_info_label.setText("raw image size: {} x {}".format(
            self.raw_image.width(), self.raw_image.height()))
        self.raw_image_info_label.setVisible(True)

        self.progress_bar.setVisible(False)