Example #1
0
def crop_image(image, threshold):
    """
    Найти непрозрачную область на изображении и вырезать её
    :param image: Изображение
    :param threshold: Порог прозрачности для обрезания
    :return: cropped_image - вырезанное изображение
             x, y, width, height - координаты и размер вырезаннго прямоугольника
    """
    cropper = CropTransparent(image.width(), image.height(), threshold, str(image.constBits()))
    x = cropper.getCroppedOffsetX()
    y = cropper.getCroppedOffsetY()
    width = cropper.getCroppedWidth()
    height = cropper.getCroppedHeight()

    cropped_image = image.copy(x, y, width, height)

    return cropped_image, x, y, width, height
Example #2
0
    def test_fire_png(self):
        fire = QImage("data/fire.png")

        cropper = CropTransparent(fire.width(), fire.height(), 50, str(fire.constBits()))

        self.assertEqual(cropper.getCroppedOffsetX(), 16)
        self.assertEqual(cropper.getCroppedOffsetY(), 15)
        self.assertEqual(cropper.getCroppedHeight(), 226)
        self.assertEqual(cropper.getCroppedWidth(), 226)

        crop_rect = cropper.getRect()

        self.assertEqual(crop_rect.x, 16)
        self.assertEqual(crop_rect.y, 15)
        self.assertEqual(crop_rect.width, 226)
        self.assertEqual(crop_rect.height, 226)
Example #3
0
    def test_imagecrop4x4(self):
        width = 6
        height = 6
        threshold = 50

        data = str(
            bytearray(
                [
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    100,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                    1,
                    1,
                    1,
                    10,
                ]
            )
        )

        cropper = CropTransparent(width, height, threshold, data)

        self.assertEqual(cropper.getCroppedHeight(), 3)
        self.assertEqual(cropper.getCroppedWidth(), 3)
        self.assertEqual(cropper.getCroppedOffsetX(), 2)
        self.assertEqual(cropper.getCroppedOffsetY(), 2)

        crop_rect = cropper.getRect()

        self.assertEqual(crop_rect.x, 2)
        self.assertEqual(crop_rect.y, 2)
        self.assertEqual(crop_rect.width, 3)
        self.assertEqual(crop_rect.height, 3)