Beispiel #1
0
    def test_can_fit_in(self):
        for item in FIT_IN_CROP_DATA:
            context = item[0].to_context()
            engine = context.modules.engine

            trans = Transformer(context)
            trans.transform(self.validate_fit_in(item, context, engine, trans))
    def _prepare_engine(self, topic, callback):
        context = topic[0].to_context()
        self.engine = context.modules.engine
        self.test_data = topic

        trans = Transformer(context)
        trans.transform(callback)
Beispiel #3
0
    def test_can_extract_cover(self):
        data = TestData(source_width=800,
                        source_height=600,
                        target_width=-800,
                        target_height=-600,
                        halign="right",
                        valign="top",
                        focal_points=[],
                        crop_left=None,
                        crop_top=None,
                        crop_right=None,
                        crop_bottom=None)

        ctx = data.to_context()
        ctx.request.filters = 'cover()'
        ctx.request.image = 'some.gif'
        ctx.request.extension = 'GIF'
        ctx.request.engine.extension = '.gif'
        ctx.config.USE_GIFSICLE_ENGINE = True

        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(self.handle_extract_cover(engine))
        expect(self.has_handled).to_be_true()
Beispiel #4
0
    def _prepare_engine(self, topic, callback):
        context = topic[0].to_context()
        self.engine = context.modules.engine
        self.test_data = topic

        trans = Transformer(context)
        trans.transform(callback)
    def test_can_fit_in(self):
        for item in FIT_IN_CROP_DATA:
            context = item[0].to_context()
            engine = context.modules.engine

            trans = Transformer(context)
            trans.transform(self.validate_fit_in(item, context, engine, trans))
Beispiel #6
0
 def topic(self, callback):
     self.test_data = TestData(
         source_width=800, source_height=600,
         target_width=400, target_height=150,
         halign="center", valign="middle",
         focal_points=[],
         crop_left=0, crop_top=75, crop_right=800, crop_bottom=375
     )
     context = self.test_data.to_context(detectors=[MockErrorSyncDetector], ignore_detector_error=False)
     trans = Transformer(context)
     trans.transform(callback)
 def test_can_resize_images_with_detection_error(self):
     test_data = TestData(
         source_width=800, source_height=600,
         target_width=400, target_height=150,
         halign="center", valign="middle",
         focal_points=[],
         crop_left=0, crop_top=75, crop_right=800, crop_bottom=375
     )
     context = test_data.to_context(detectors=[MockErrorSyncDetector], ignore_detector_error=True)
     trans = Transformer(context)
     trans.transform(self.validate_resize(test_data))
Beispiel #8
0
def assert_fit_in_resize(data):
    data, expectations = data

    trans = Transformer(data.to_context())
    trans.transform()

    assert not data.engine.calls['crop']

    assert data.engine.calls['resize']
    assert len(data.engine.calls['resize']) == 1

    assert data.engine.calls['resize'][0]['width'] == expectations[0]
    assert data.engine.calls['resize'][0]['height'] == expectations[1]
Beispiel #9
0
def assert_fit_in_resize(data):
    data, expectations = data

    trans = Transformer(data.to_context())
    trans.transform()

    assert not data.engine.calls['crop']

    assert data.engine.calls['resize']
    assert len(data.engine.calls['resize']) == 1

    assert data.engine.calls['resize'][0]['width'] == expectations[0]
    assert data.engine.calls['resize'][0]['height'] == expectations[1]
Beispiel #10
0
        def topic(self):
            data = TestData(
                        source_width=800, source_height=600,
                        target_width=-800, target_height=-600,
                        halign="right", valign="top",
                        focal_points=[],
                        crop_left=None, crop_top=None, crop_right=None, crop_bottom=None
                    )

            ctx = data.to_context()
            trans = Transformer(ctx)
            trans.transform()

            return ctx['engine']
    def test_can_resize_images_with_detection_error_not_ignoring_it(self):
        test_data = TestData(
            source_width=800, source_height=600,
            target_width=400, target_height=150,
            halign="center", valign="middle",
            focal_points=[],
            crop_left=0, crop_top=75, crop_right=800, crop_bottom=375
        )
        context = test_data.to_context(detectors=[MockErrorSyncDetector], ignore_detector_error=False)
        trans = Transformer(context)
        with expect.error_to_happen(Exception, message='x'):
            trans.transform(None)

        expect(test_data.engine.calls['resize']).to_length(0)
Beispiel #12
0
        def topic(self, callback):
            data = TestData(
                source_width=800, source_height=600,
                target_width=-800, target_height=-600,
                halign="right", valign="top",
                focal_points=[],
                crop_left=None, crop_top=None, crop_right=None, crop_bottom=None
            )

            ctx = data.to_context()
            self.engine = ctx.modules.engine

            trans = Transformer(ctx)
            trans.transform(callback)
        def topic(self, callback):
            data = TestData(
                        source_width=800, source_height=600,
                        target_width=-800, target_height=-600,
                        halign="right", valign="top",
                        focal_points=[],
                        crop_left=None, crop_top=None, crop_right=None, crop_bottom=None
                    )

            ctx = data.to_context()
            self.engine = ctx.modules.engine

            trans = Transformer(ctx)
            trans.transform(callback)
Beispiel #14
0
    def test_can_resize_images_with_detection_error_not_ignoring_it(self):
        test_data = TestData(
            source_width=800, source_height=600,
            target_width=400, target_height=150,
            halign="center", valign="middle",
            focal_points=[],
            crop_left=0, crop_top=75, crop_right=800, crop_bottom=375
        )
        context = test_data.to_context(detectors=[MockErrorSyncDetector], ignore_detector_error=False)
        trans = Transformer(context)

        trans.transform(lambda: None)

        expect(test_data.engine.calls['resize']).to_length(0)
    def test_invalid_crop(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=800, target_height=600,
            halign="right", valign="top",
            focal_points=[],
            crop_left=200, crop_top=0, crop_right=100, crop_bottom=100
        )

        ctx = data.to_context()
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(self.handle_invalid_crop(engine))
        expect(self.has_handled).to_be_true()
Beispiel #16
0
    def test_get_target_dimensions(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=600, target_height=400,
            halign="right", valign="top",
            focal_points=[],
            crop_left=200, crop_top=0, crop_right=100, crop_bottom=100
        )

        ctx = data.to_context()
        trans = Transformer(ctx)
        dimensions = trans.get_target_dimensions()
        expect(dimensions).to_equal((600, 400))
        trans.transform(lambda: 1)
        expect(dimensions).to_equal((600, 400))
Beispiel #17
0
    def test_get_target_dimensions(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=600, target_height=400,
            halign="right", valign="top",
            focal_points=[],
            crop_left=200, crop_top=0, crop_right=100, crop_bottom=100
        )

        ctx = data.to_context()
        trans = Transformer(ctx)
        dimensions = trans.get_target_dimensions()
        expect(dimensions).to_equal((600, 400))
        trans.transform(lambda: 1)
        expect(dimensions).to_equal((600, 400))
Beispiel #18
0
 def test_can_resize_with_stretch():
     data = TestData(
         source_width=800, source_height=600,
         target_width=800, target_height=200,
         halign="right", valign="top",
         focal_points=[],
         crop_left=None, crop_top=None, crop_right=None, crop_bottom=None,
         stretch=True
     )
     ctx = data.to_context()
     engine = ctx.modules.engine
     trans = Transformer(ctx)
     trans.transform(lambda: None)
     expect(engine.calls['resize']).to_equal([{'width': 800, 'height': 200}])
     expect(engine.calls['crop']).to_be_empty()
Beispiel #19
0
    def test_invalid_crop(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=800, target_height=600,
            halign="right", valign="top",
            focal_points=[],
            crop_left=200, crop_top=0, crop_right=100, crop_bottom=100
        )

        ctx = data.to_context()
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(lambda: None)
        expect(engine.calls['crop']).to_be_empty()
Beispiel #20
0
    async def test_can_resize_with_stretch(self):
        data = TestData(
            source_width=800,
            source_height=600,
            target_width=800,
            target_height=200,
            halign="right",
            valign="top",
            focal_points=[],
            crop_left=None,
            crop_top=None,
            crop_right=None,
            crop_bottom=None,
            stretch=True,
        )
        ctx = data.to_context()
        engine = ctx.modules.engine
        trans = Transformer(ctx)

        await trans.transform()

        expect(engine.calls["resize"]).to_equal([{
            "width": 800,
            "height": 200
        }])
        expect(engine.calls["crop"]).to_be_empty()
Beispiel #21
0
    async def test_can_extract_cover(self):
        data = TestData(
            source_width=800,
            source_height=600,
            target_width=-800,
            target_height=-600,
            halign="right",
            valign="top",
            focal_points=[],
            crop_left=None,
            crop_top=None,
            crop_right=None,
            crop_bottom=None,
        )
        ctx = data.to_context()
        ctx.request.filters = "cover()"
        ctx.request.image = "some.gif"
        ctx.request.extension = "GIF"
        ctx.request.engine.extension = ".gif"
        ctx.config.USE_GIFSICLE_ENGINE = True
        engine = ctx.modules.engine
        trans = Transformer(ctx)

        await trans.transform()

        expect(engine.calls["cover"]).to_equal(1)
Beispiel #22
0
    def test_can_fit_in(self):
        for (test_data, (width, height, should_resize)) in FIT_IN_CROP_DATA:
            context = test_data.to_context()
            engine = context.modules.engine

            trans = Transformer(context)
            trans.transform(lambda: None)

            expect(engine.calls['crop']).to_be_empty()

            if should_resize:
                expect(engine.calls['resize']).to_length(1)
                expect(engine.calls['resize'][0]['width']).to_equal(width)
                expect(engine.calls['resize'][0]['height']).to_equal(height)
            else:
                expect(engine.calls['resize']).to_be_empty()
Beispiel #23
0
    def test_can_fit_in(self):
        for (test_data, (width, height, should_resize)) in FIT_IN_CROP_DATA:
            context = test_data.to_context()
            engine = context.modules.engine

            trans = Transformer(context)
            trans.transform(lambda: None)

            expect(engine.calls['crop']).to_be_empty()

            if should_resize:
                expect(engine.calls['resize']).to_length(1)
                expect(engine.calls['resize'][0]['width']).to_equal(width)
                expect(engine.calls['resize'][0]['height']).to_equal(height)
            else:
                expect(engine.calls['resize']).to_be_empty()
Beispiel #24
0
    def test_can_transform_with_flip(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=-800, target_height=-600,
            halign="right", valign="top",
            focal_points=[],
            crop_left=None, crop_top=None, crop_right=None, crop_bottom=None
        )

        ctx = data.to_context()
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(lambda: None)
        expect(engine.calls['horizontal_flip']).to_equal(1)
        expect(engine.calls['vertical_flip']).to_equal(1)
Beispiel #25
0
    def get_filter(self, filter_name, params_string="", config_context=None):
        config = Config(
            FILTERS=[filter_name],
            LOADER="thumbor.loaders.file_loader",
            FILE_LOADER_ROOT_PATH=join(dirname(realpath(__file__)), "fixtures",
                                       "filters"),
        )
        importer = Importer(config)
        importer.import_modules()

        req = RequestParameters()

        srv = ServerParameters(8888, "localhost", "./tests/test.conf", None,
                               "DEBUG", None)
        srv._security_key = "MY_SECURE_KEY"

        context = Context(config=config, importer=importer, server=srv)
        context.request = req
        context.request.engine = context.modules.engine
        context.request_handler = mock.MagicMock(request=mock.MagicMock(
            protocol="http",
            host="localhost:8888",
        ))

        if config_context is not None:
            config_context(context)

        self.context = context

        fltr = importer.filters[0]
        fltr.pre_compile()

        context.transformer = Transformer(context)

        return fltr(params_string, context=context)
Beispiel #26
0
    def get_filter(self, filter_name, params_string="", config_context=None):
        config = Config(FILTERS=[filter_name],
                        LOADER='thumbor.loaders.file_loader',
                        FILE_LOADER_ROOT_PATH=join(dirname(realpath(__file__)),
                                                   'fixtures', 'filters'))
        importer = Importer(config)
        importer.import_modules()

        req = RequestParameters()

        context = Context(config=config, importer=importer)
        context.request = req
        context.request.engine = context.modules.engine

        if config_context is not None:
            config_context(context)

        self.context = context

        fltr = importer.filters[0]
        fltr.pre_compile()

        context.transformer = Transformer(context)

        return fltr(params_string, context=context)
    def test_can_transform_meta_with_orientation(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=100, target_height=100,
            halign="right", valign="top",
            focal_points=[],
            crop_left=None, crop_top=None, crop_right=None, crop_bottom=None,
            meta=True
        )

        ctx = data.to_context()
        ctx.config.RESPECT_ORIENTATION = True
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(self.handle_transform_with_meta(engine))
        expect(self.has_handled).to_be_true()
Beispiel #28
0
    def test_can_transform_meta_with_orientation(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=100, target_height=100,
            halign="right", valign="top",
            focal_points=[],
            crop_left=None, crop_top=None, crop_right=None, crop_bottom=None,
            meta=True
        )

        ctx = data.to_context()
        ctx.config.RESPECT_ORIENTATION = True
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(lambda: None)
        expect(engine.calls['reorientate']).to_equal(1)
Beispiel #29
0
            def transform():
                self.normalize_crops(normalized, req, engine)

                if req.meta:
                    self.context.request.engine = JSONEngine(engine, req.image_url, req.meta_callback)

                after_transform_cb = functools.partial(self.after_transform, self.context)
                Transformer(self.context).transform(after_transform_cb)
Beispiel #30
0
    def test_invalid_crop(self):
        data = TestData(source_width=800,
                        source_height=600,
                        target_width=800,
                        target_height=600,
                        halign="right",
                        valign="top",
                        focal_points=[],
                        crop_left=200,
                        crop_top=0,
                        crop_right=100,
                        crop_bottom=100)

        ctx = data.to_context()
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(lambda: None)
        expect(engine.calls['crop']).to_be_empty()
Beispiel #31
0
    def test_can_transform_with_flip(self):
        data = TestData(source_width=800,
                        source_height=600,
                        target_width=-800,
                        target_height=-600,
                        halign="right",
                        valign="top",
                        focal_points=[],
                        crop_left=None,
                        crop_top=None,
                        crop_right=None,
                        crop_bottom=None)

        ctx = data.to_context()
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(lambda: None)
        expect(engine.calls['horizontal_flip']).to_equal(1)
        expect(engine.calls['vertical_flip']).to_equal(1)
Beispiel #32
0
        def topic(self, callback):
            data = TestData(
                source_width=800, source_height=600,
                target_width=-800, target_height=-600,
                halign="right", valign="top",
                focal_points=[],
                crop_left=None, crop_top=None, crop_right=None, crop_bottom=None
            )

            ctx = data.to_context()
            ctx.request.filters = 'cover()'
            ctx.request.image = 'some.gif'
            ctx.request.extension= 'GIF'
            ctx.request.engine.extension = '.gif'
            ctx.config.USE_GIFSICLE_ENGINE = True

            self.engine = ctx.modules.engine

            trans = Transformer(ctx)
            trans.transform(callback)
Beispiel #33
0
    def test_can_extract_cover(self):
        data = TestData(
            source_width=800, source_height=600,
            target_width=-800, target_height=-600,
            halign="right", valign="top",
            focal_points=[],
            crop_left=None, crop_top=None, crop_right=None, crop_bottom=None
        )

        ctx = data.to_context()
        ctx.request.filters = 'cover()'
        ctx.request.image = 'some.gif'
        ctx.request.extension = 'GIF'
        ctx.request.engine.extension = '.gif'
        ctx.config.USE_GIFSICLE_ENGINE = True

        engine = ctx.modules.engine

        trans = Transformer(ctx)
        trans.transform(lambda: None)
        expect(engine.calls['cover']).to_equal(1)
Beispiel #34
0
 def test_can_resize_with_stretch():
     data = TestData(source_width=800,
                     source_height=600,
                     target_width=800,
                     target_height=200,
                     halign="right",
                     valign="top",
                     focal_points=[],
                     crop_left=None,
                     crop_top=None,
                     crop_right=None,
                     crop_bottom=None,
                     stretch=True)
     ctx = data.to_context()
     engine = ctx.modules.engine
     trans = Transformer(ctx)
     trans.transform(lambda: None)
     expect(engine.calls['resize']).to_equal([{
         'width': 800,
         'height': 200
     }])
     expect(engine.calls['crop']).to_be_empty()
Beispiel #35
0
        def callback(buffer):
            if buffer is None:
                self._error(404)
                return

            context = dict(loader=self.loader,
                           engine=self.engine,
                           storage=self.storage,
                           buffer=buffer,
                           should_crop=should_crop,
                           crop_left=crop_left,
                           crop_top=crop_top,
                           crop_right=crop_right,
                           crop_bottom=crop_bottom,
                           fit_in=fit_in,
                           should_flip_horizontal=horizontal_flip,
                           width=width,
                           should_flip_vertical=vertical_flip,
                           height=height,
                           halign=halign,
                           valign=valign,
                           extension=extension,
                           focal_points=[])

            self.engine.load(buffer, extension)

            if meta:
                context['engine'] = JSONEngine(self.engine, image)

            if self.detectors and should_be_smart:
                with tempfile.NamedTemporaryFile(suffix='.jpg') as temp_file:
                    jpg_buffer = buffer if extension in (
                        '.jpg', '.jpeg') else self.engine.read('.jpg')
                    temp_file.write(jpg_buffer)
                    temp_file.seek(0)
                    context['file'] = temp_file.name
                    self.detectors[0](index=0,
                                      detectors=self.detectors).detect(context)

            Transformer(context).transform()

            content_type = "application/json" if meta else CONTENT_TYPE[
                context['extension']]
            self.set_header('Content-Type', content_type)

            results = context['engine'].read(context['extension'])

            self.write(results)
            self.finish()
Beispiel #36
0
    async def test_can_fit_in(self):
        for (test_data, (width, height, should_resize)) in FIT_IN_CROP_DATA:
            context = test_data.to_context()
            engine = context.modules.engine
            trans = Transformer(context)

            await trans.transform()

            expect(engine.calls["crop"]).to_be_empty()
            if should_resize:
                expect(engine.calls["resize"]).to_length(1)
                expect(engine.calls["resize"][0]["width"]).to_equal(width)
                expect(engine.calls["resize"][0]["height"]).to_equal(height)
            else:
                expect(engine.calls["resize"]).to_be_empty()
Beispiel #37
0
        def callback(normalized, buffer=None, engine=None):
            req = self.context.request

            if engine is None:
                if buffer is None:
                    self._error(404)
                    return

                engine = self.context.modules.engine
                engine.load(buffer, req.extension)

            new_crops = None
            if normalized and req.should_crop:
                crop_left = req.crop['left']
                crop_top = req.crop['top']
                crop_right = req.crop['right']
                crop_bottom = req.crop['bottom']

                actual_width, actual_height = engine.size

                if not req.width and not req.height:
                    actual_width = engine.size[0]
                    actual_height = engine.size[1]
                elif req.width:
                    actual_height = engine.get_proportional_height(
                        engine.size[0])
                elif req.height:
                    actual_width = engine.get_proportional_width(
                        engine.size[1])

                new_crops = self.translate_crop_coordinates(
                    engine.source_width, engine.source_height, actual_width,
                    actual_height, crop_left, crop_top, crop_right,
                    crop_bottom)
                req.crop['left'] = new_crops[0]
                req.crop['top'] = new_crops[1]
                req.crop['right'] = new_crops[2]
                req.crop['bottom'] = new_crops[3]

            if req.meta:
                self.context.modules.engine = JSONEngine(
                    engine, req.image_url, req.meta_callback)

            after_transform_cb = functools.partial(self.after_transform,
                                                   self.context)
            Transformer(self.context).transform(after_transform_cb)
Beispiel #38
0
        def callback(normalized, buffer=None, engine=None):
            req = self.context.request

            if engine is None:
                if buffer is None:
                    self._error(404)
                    return

                engine = self.context.modules.engine
                engine.load(buffer, req.extension)

            self.normalize_crops(normalized, req, engine)

            if req.meta:
                self.context.modules.engine = JSONEngine(engine, req.image_url, req.meta_callback)

            after_transform_cb = functools.partial(self.after_transform, self.context)
            Transformer(self.context).transform(after_transform_cb)
Beispiel #39
0
    def get_filter(self, filter_name, params_string="", config_context=None):
        config = Config(FILTERS=[filter_name], )
        importer = Importer(config)
        importer.import_modules()

        req = RequestParameters()

        context = Context(config=config, importer=importer)
        context.request = req
        context.request.engine = context.modules.engine

        if config_context is not None:
            config_context(context)

        fltr = importer.filters[0]
        fltr.pre_compile()

        context.transformer = Transformer(context)

        return fltr(params_string, context=context)
Beispiel #40
0
    async def test_can_transform_meta_with_orientation(self):
        data = TestData(
            source_width=800,
            source_height=600,
            target_width=100,
            target_height=100,
            halign="right",
            valign="top",
            focal_points=[],
            crop_left=None,
            crop_top=None,
            crop_right=None,
            crop_bottom=None,
            meta=True,
        )

        ctx = data.to_context()
        ctx.config.RESPECT_ORIENTATION = True
        engine = ctx.modules.engine

        trans = Transformer(ctx)
        await trans.transform()
        expect(engine.calls["reorientate"]).to_equal(1)
Beispiel #41
0
        def should_crop_properly(self, topic):
            context = topic.to_context()
            trans = Transformer(context)
            trans.transform()

            expect(topic).to_be_cropped()
Beispiel #42
0
 def test_can_resize_images_with_detectors(self):
     for item in TESTITEMS:
         context = item.to_context(detectors=[MockSyncDetector])
         trans = Transformer(context)
         trans.transform(lambda: None)
         self.validate_resize(item)
Beispiel #43
0
 def test_can_resize_images(self):
     for item in TESTITEMS:
         context = item.to_context()
         trans = Transformer(context)
         trans.transform(lambda: None)
         self.validate_resize(item)
Beispiel #44
0
 def topic(self, callback, topic):
     self.test_data = topic
     context = topic.to_context()
     trans = Transformer(context)
     trans.transform(callback)
Beispiel #45
0
 def topic(self, callback, topic):
     self.test_data = topic
     context = topic.to_context(detectors=[MockSyncDetector])
     trans = Transformer(context)
     trans.transform(callback)
 def topic(self, callback, topic):
     self.test_data = topic
     context = topic.to_context()
     trans = Transformer(context)
     trans.transform(callback)
Beispiel #47
0
def assert_proper_operations(data):
    trans = Transformer(data.to_context())
    trans.transform()

    assert data.has_cropped_properly(), data.crop_error_message
    assert data.has_resized_properly(), data.resize_error_message
Beispiel #48
0
    def _get_engine(self, topic):
        context = topic[0].to_context()
        trans = Transformer(context)
        trans.transform()

        return context['engine']
Beispiel #49
0
def assert_proper_operations(data):
    trans = Transformer(data.to_context())
    trans.transform()

    assert data.has_cropped_properly(), data.crop_error_message
    assert data.has_resized_properly(), data.resize_error_message