示例#1
0
    def test_split_pick_both(self):
        r1 = Rect(Point(10, 10), Size(512, 512))
        r2 = Rect(Point(), Size(110, 75))
        r3 = Rect(Point(), Size(70, 95))
        r4 = Rect(Point(), Size(90, 55))

        s, rs1, rs2, r = r1.split(r2, Rect.RULE_PICK_BOTH)
        self.assertEqual(s, 2)
        self.assertEqual(rs1, Rect(r1.origin + Point(r2.size.width, 0),
                                   Size(r1.size.width - r2.size.width, r1.size.height)))
        self.assertEqual(rs2, Rect(r1.origin + Point(0, r2.size.height),
                                   Size(r1.size.width, r1.size.height - r2.size.height)))
        self.assertEqual(r, False)

        s, rs3, rs4, r = rs1.split(r3, Rect.RULE_PICK_BOTH)
        self.assertEqual(s, 2)
        self.assertEqual(rs3, Rect(rs1.origin + Point(r3.size.width, 0),
                                   Size(rs1.size.width - r3.size.width, rs1.size.height)))
        self.assertEqual(rs4, Rect(rs1.origin + Point(0, r3.size.height),
                                   Size(rs1.size.width, rs1.size.height - r3.size.height)))
        self.assertEqual(r, False)

        s, rs5, rs6, r = rs3.split(r4, Rect.RULE_PICK_BOTH)
        self.assertEqual(s, 2)
        self.assertEqual(rs5, Rect(rs3.origin + Point(r4.size.width, 0),
                                   Size(rs3.size.width - r4.size.width, rs3.size.height)))
        self.assertEqual(rs6, Rect(rs3.origin + Point(0, r4.size.height),
                                   Size(rs3.size.width, rs3.size.height - r4.size.height)))
        self.assertEqual(r, False)
示例#2
0
    def __init__(self, directory, filename):
        self.directory = directory
        self.filename = filename

        image = QImage(self.directory + QDir.separator() + self.filename)

        Rect.__init__(self, Point(), Size(image.width(), image.height()))

        cropped_image, x, y, width, height = self.__getCrop(image, False)

        self.crop = Rect(Point(x, y), Size(width, height))

        self.rotated = False
示例#3
0
    def test_intersection(self):

        r1 = Rect(Point(10, 10), Size(30, 30))
        r2 = Rect(Point(20, 20), Size(10, 10))

        r = r1.intersection(r2)
        self.assertEqual(r, r2)

        r3 = Rect(Point(10, 10), Size(20, 40))
        r4 = Rect(Point(15, 15), Size(15, 15))

        r = r3.intersection(r4)
        self.assertEqual(r, Rect(Point(15, 15), Size(15, 15)))

        r5 = Rect(Point(4, 4), Size(8, 8))
        r6 = Rect(Point(9, 9), Size(12, 12))

        r = r5.intersection(r6)
        self.assertEqual(r, Rect(Point(9, 9), Size(3, 3)))

        r7 = Rect(Point(5, 5), Size(5, 5))
        r8 = Rect(Point(5, 10), Size(10, 10))

        r = r7.intersection(r8)
        self.assertEqual(r, Rect(Point(5, 10), Size(5, 0)))
示例#4
0
    def test_area(self):
        r = Rect(Point(0, 0), Size(10, 10))
        self.assertEqual(r.area(), 100)

        r = Rect(Point(10, 10), Size(20, 20))
        self.assertEqual(r.area(), 400)

        r = Rect(Point(20, 20), Size(50, 50))
        self.assertEqual(r.area(), 2500)

        r = Rect(Point(100, 100), Size(200, 300))
        self.assertEqual(r.area(), 60000)
示例#5
0
class Image(Rect):
    """
    self.origin - показывает положение в атласе
    self.size   - оригинальный размер изображения
    self.crop   - область изображения взятого из оригинала после обрезания прозрачных краёв
    """

    BorderPadding = 1
    CropThreshold = 10

    def __init__(self, directory, filename):
        self.directory = directory
        self.filename = filename

        image = QImage(self.directory + QDir.separator() + self.filename)

        Rect.__init__(self, Point(), Size(image.width(), image.height()))

        cropped_image, x, y, width, height = self.__getCrop(image, False)

        self.crop = Rect(Point(x, y), Size(width, height))

        self.rotated = False

    def area(self):
        return self.crop.area()

    def draw(self, painter, offset=Point()):
        cropped_image, x, y, width, height = self.__getCrop()

        origin_offset = QPoint(self.origin.x + offset.x, self.origin.y + offset.y)

        if self.rotated:
            old_transform = QTransform(painter.transform())
            painter.setTransform(painter.transform().translate(origin_offset.x() + self.crop.size.height,
                                                               origin_offset.y()).rotate(90, Qt.ZAxis))
            painter.drawImage(0, 0, cropped_image)
            painter.setTransform(old_transform)
        else:
            painter.drawImage(origin_offset, cropped_image)
            # Rect(self.origin, self.crop.size).draw(painter, offset)

    def toJson(self):
        return {
            'offset': {
                'x': self.origin.x,
                'y': self.origin.y
            },
            'original_size': {
                'width': self.size.width,
                'height': self.size.height
            },
            'crop': {
                'origin': {
                    'x': self.crop.origin.x,
                    'y': self.crop.origin.y
                },
                'size': {
                    'width': self.crop.size.width,
                    'height': self.crop.size.height
                }
            },
            'rotated': self.rotated,
            'filename': self.filename
        }

    # noinspection PyUnreachableCode
    def __str__(self):
        return '{klass}({file}, {origin}, {size}, {crop}, {rotated})'.format(klass=self.__class__.__name__,
                                                                             file=self.filename,
                                                                             crop=self.crop,
                                                                             origin=self.origin,
                                                                             size=self.size,
                                                                             rotated=self.rotated)

    def __repr__(self):
        return self.__str__()

    def __getCrop(self, image=None, loadCrop=True):
        if self.__hasCrop():
            cropped_image, crop_info = self.__loadCrop(loadCrop)
            x, y, width, height = crop_info['x'], crop_info['y'], crop_info['width'], crop_info['height']
        else:
            if not image:
                raise RuntimeError('Image is none')
            cropped_image, x, y, width, height = crop_image(image, Image.CropThreshold)
            self.__saveCrop(cropped_image, x, y, width, height)
        return cropped_image, x, y, width, height

    def __fileNameWoExt(self):
        image = QFileInfo(self.directory + QDir.separator() + self.filename)
        return image.absoluteDir().absolutePath() + QDir.separator() + image.baseName()

    def __hasCrop(self):
        name_wo_ext = self.__fileNameWoExt()
        return QFileInfo(name_wo_ext + '.json').exists() and QFileInfo(name_wo_ext + '.crop').exists()

    def __saveCrop(self, image, x, y, width, height):
        image.save(self.__fileNameWoExt() + '.crop', 'PNG')
        with open(self.__fileNameWoExt() + '.json', 'w') as f:
            json.dump({
                'x': x,
                'y': y,
                'width': width,
                'height': height
            }, fp=f)

    def __loadCrop(self, loadCrop=True):
        name_wo_ext = self.__fileNameWoExt()
        json_data = open(name_wo_ext + '.json').read()
        image = QImage()
        if loadCrop:
            image.load(name_wo_ext + '.crop', 'PNG')
        return image, json.loads(json_data)
示例#6
0
    def test_split_sas(self):
        r1 = Rect(Point(10, 10), Size(512, 512))
        r2 = Rect(Point(), Size(110, 75))
        r3 = Rect(Point(), Size(70, 95))
        r4 = Rect(Point(), Size(90, 55))
        r5 = Rect(Point(), Size(50, 75))

        r6 = Rect(Point(), Size(110, 50))
        r7 = Rect(Point(), Size(70, 75))

        # Плохие примеры
        r8 = Rect(Point(), Size(512, 512))
        s, rs1_, rs2_, r = r1.split(r8, Rect.RULE_SAS)
        self.assertEqual(s, 0)
        self.assertEqual(rs1_, Rect())
        self.assertEqual(rs2_, Rect())
        self.assertEqual(r, False)

        r9 = Rect(Point(), Size(200, 70))
        s, rs1_, rs2_, r = r2.split(r9, Rect.RULE_SAS)
        self.assertEqual(s, 0)
        self.assertEqual(rs1_, Rect())
        self.assertEqual(rs2_, Rect())
        self.assertEqual(r, False)

        r10 = Rect(Point(), Size(50, 80))
        s, rs1_, rs2_, r = r2.split(r10, Rect.RULE_SAS)
        self.assertEqual(s, 2)
        self.assertEqual(rs1_, Rect(r2.origin + Point(r10.size.height, 0),
                                    Size(r2.size.width - r10.size.height, r2.size.height)))
        self.assertEqual(rs2_, Rect(r2.origin + Point(0, r10.size.width),
                                    Size(r10.size.height, r2.size.height - r10.size.width)))
        self.assertEqual(r, True)

        # 1
        s, ro1, ro2, r = r2.split(r6, Rect.RULE_SAS)
        self.assertEqual(s, 1)
        self.assertEqual(ro1, Rect(Point(r2.origin.x,
                                         r2.origin.y + r6.size.height),
                                   Size(r2.size.width,
                                        r2.size.height - r6.size.height)))
        self.assertEqual(ro2, Rect())

        s, ro1, ro2, r = r2.split(r7, Rect.RULE_SAS)
        self.assertEqual(s, 1)
        self.assertEqual(ro1, Rect(Point(r2.origin.x + r7.size.width,
                                         r2.origin.y),
                                   Size(r2.size.width - r7.size.width,
                                        r2.size.height)))
        self.assertEqual(ro2, Rect())

        # test SAS
        s, rs1, rs2, r = r1.split(r2, Rect.RULE_SAS)
        self.assertEqual(s, 2)
        self.assertEqual(rs1, Rect(Point(r1.origin.x + r2.size.width,
                                         r1.origin.y),
                                   Size(r1.size.width - r2.size.width,
                                        r1.size.height)))

        self.assertEqual(rs2, Rect(Point(r1.origin.x,
                                         r1.origin.y + r2.size.height),
                                   Size(r2.size.width,
                                        r1.size.height - r2.size.height)))

        s, rs3, rs4, r = rs1.split(r3, Rect.RULE_SAS)
        self.assertEqual(s, 2)
        self.assertEqual(rs3, Rect(Point(rs1.origin.x + r3.size.width,
                                         rs1.origin.y),
                                   Size(rs1.size.width - r3.size.width,
                                        r3.size.height)))

        self.assertEqual(rs4, Rect(Point(rs1.origin.x,
                                         rs1.origin.y + r3.size.height),
                                   Size(rs1.size.width,
                                        rs1.size.height - r3.size.height)))

        s, rs5, rs6, r = rs3.split(r4, Rect.RULE_SAS)
        self.assertEqual(s, 2)
        self.assertEqual(rs5, Rect(Point(rs3.origin.x + r4.size.width,
                                         rs3.origin.y),
                                   Size(rs3.size.width - r4.size.width,
                                        rs3.size.height)))

        self.assertEqual(rs6, Rect(Point(rs3.origin.x,
                                         rs3.origin.y + r4.size.height),
                                   Size(r4.size.width,
                                        rs3.size.height - r4.size.height)))
示例#7
0
 def __init__(self, max_size, origin=Point()):
     Rect.__init__(self, origin, Size())
     self.maxSize = max_size
     self.images = []