Example #1
0
    def test_invalid_fit_size(self):
        test_image = Image.open(self.img_dir / "test.png")
        test_image.info["filename"] = "test.png"
        query = {"fit": "5x0"}

        with self.assertRaises(its.errors.ITSClientError):
            process_transforms(test_image, query)
Example #2
0
    def test_invalid_crop_focal_percentages(self):
        test_image = Image.open(self.img_dir / "test.png")
        test_image.info["filename"] = "test.png"
        query = {"crop": "100x100x150x150"}

        with self.assertRaises(its.errors.ITSClientError):
            process_transforms(test_image, query)
Example #3
0
    def test_resize_format(self):
        test_image = Image.open(self.img_dir / "test.png")
        query = {"resize": "100x100", "format": "foo"}

        with self.assertRaises(its.errors.ITSClientError):
            result = process_transforms(test_image, query)
            optimize(result, query)
Example #4
0
 def test_resize_without_width(self):
     test_image = Image.open(self.img_dir / "abe.jpg")
     test_image.info["filename"] = "abe.jpg"
     query = {"resize": "x100"}
     result = process_transforms(test_image, query)
     self.assertEqual(result.width, 76)
     self.assertEqual(result.height, 100)
Example #5
0
def process_request(namespace, query, filename):

    try:
        image = loader(namespace, filename)
    except NotFoundError as e:
        abort(404)
    """
    PIL doesn't support SVG and ITS doesn't change them in any way,
    so loader returns a ByesIO object so the images will still be returned to the browser.
    This BytesIO object is returned from each loader class's get_fileobj() function.
    """
    if isinstance(image, BytesIO):
        output = image
        mime_type = MIME_TYPES["SVG"]
    else:
        image.info['filename'] = filename
        result = process_transforms(image, query)

        # image conversion and compression
        # cache result
        result = optimize(result, query)

        if result.format is None:
            result.format = image.format

        mime_type = MIME_TYPES[result.format.upper()]

        output = BytesIO()
        result.save(output, format=result.format.upper())

    return Response(response=output.getvalue(), mimetype=mime_type)
Example #6
0
 def test_resize_integrity_larger(self):
     test_image = Image.open(self.img_dir / "test.png")
     test_image.info["filename"] = "test.png"
     query = {"resize": "700x550"}
     expected = Image.open(self.img_dir / "expected/test_resize_700x550.png")
     actual = process_transforms(test_image, query)
     comparison = compare_pixels(expected, actual)
     self.assertGreaterEqual(comparison, self.threshold)
Example #7
0
def process_request(namespace: str, query: Dict[str, str],
                    filename: str) -> Response:
    fit_synonyms = {"crop", "focalcrop"}
    if len((fit_synonyms | {"fit"}) & set(query.keys())) > 1:
        raise ITSClientError(
            "use only one of these synonyms: fit, crop, focalcrop")

    for fit_snynonym in fit_synonyms:
        if fit_snynonym in query:
            query["fit"] = query[fit_snynonym]
            del query[fit_snynonym]

    if namespace not in NAMESPACES:
        abort(
            400, "{namespace} is not a configured namespace".format(
                namespace=namespace))

    namespace_config = NAMESPACES[namespace]
    if namespace_config.get("redirect"):
        location = get_redirect_location(namespace, query, filename)
        return redirect(location=location, code=301)

    try:
        image = loader(namespace, filename)
    except NotFoundError:
        abort(404)

    # PIL doesn't support SVG and ITS doesn't change them in any way,
    # so loader returns a ByesIO object so the images will still be returned to the browser.
    # This BytesIO object is returned from each loader class's get_fileobj() function.
    if isinstance(image, BytesIO):
        output = image
        mime_type = MIME_TYPES["SVG"]
    else:
        image.info["filename"] = filename
        result = process_transforms(image, query)

        # image conversion and compression
        # cache result
        result = optimize(result, query)

        if result.format is None:
            result.format = image.format

        mime_type = MIME_TYPES[result.format.upper()]

        output = BytesIO()
        result.save(output, format=result.format.upper())

    # our images are cacheable for one year
    # NOTE this would be the right place to do clever things like:
    # allow developers to deactivate caching locally
    # include validation tokens (etags)
    resp_headers = {"Cache-Control": "max-age=31536000"}

    return Response(response=output.getvalue(),
                    headers=resp_headers,
                    mimetype=mime_type)
Example #8
0
 def test_resize_integrity_smaller(self):
     test_image = Image.open(self.img_dir / "test.png")
     test_image.info["filename"] = "test.png"
     query = {"resize": "100x100"}
     expected = Image.open(self.img_dir / "expected/test_resize.png")
     actual = process_transforms(test_image, query)
     # can't use norm since resizing can cause noise
     comparison = compare_pixels(expected, actual)
     self.assertGreaterEqual(comparison, self.threshold)
Example #9
0
 def test_overlay_12x78(self):
     test_image = Image.open(self.img_dir / "abstract.png")
     test_image.info['filename'] = "abstract.png"
     query = {'overlay': '12x78x' + self.overlays['five']}
     expected = Image.open(self.img_dir /
                           "expected/abstract_five_12x78.png")
     actual = process_transforms(test_image, query)
     comparison = compare_pixels(expected, actual)
     self.assertGreaterEqual(comparison, self.threshold)
Example #10
0
    def test_overlay(self):
        print(self.img_dir)

        test_image = Image.open(self.img_dir / "abstract.png")
        test_image.info['filename'] = "abstract.png"
        query = {'overlay': '45x45x' + self.overlays['five']}
        expected = Image.open(self.img_dir /
                              "expected/abstract_overlay_five.png")
        actual = process_transforms(test_image, query)
        compare_pixels(expected, actual)
Example #11
0
 def test_svg_passthrough(self):
     test_image = BytesIO(
         open(self.img_dir / "wikipedia_logo.svg", "rb").read())
     query = {
         'fit': '10x10',
         'format': 'png',
         'resize': '500x500',
         'filename': "wikipedia_logo.svg"
     }
     result = process_transforms(test_image, query)
     result = optimize(result, query)
     self.assertEqual(isinstance(result, BytesIO), True)
Example #12
0
    def test_invalid_resize(self):
        test_image = Image.open(self.img_dir / "test.png")
        query = {"resize": "100"}

        with self.assertRaises(its.errors.ITSClientError):
            process_transforms(test_image, query)
Example #13
0
 def test_resize_size(self):
     test_image = Image.open(self.img_dir / "abstract.png")
     test_image.info["filename"] = "abstract.png"
     query = {"resize": "10x10"}
     result = process_transforms(test_image, query)
     self.assertEqual(result.size, (10, 10))