Ejemplo n.º 1
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)
Ejemplo n.º 2
0
    def test_png_quality_vs_size(self):
        test_image = Image.open(self.img_dir / "test.png")
        quality_1 = optimize(test_image, {"quality": "1"})
        with tempfile.NamedTemporaryFile(dir=".", delete=True) as tmp_file_1:
            quality_1.save(tmp_file_1.name, format=quality_1.format)
            q1_size = os.stat(tmp_file_1.name).st_size

        quality_10 = optimize(test_image, {"quality": "10"})
        with tempfile.NamedTemporaryFile(dir=".", delete=True) as tmp_file_2:
            quality_10.save(tmp_file_2.name, format=quality_10.format)
            q10_size = os.stat(tmp_file_2.name).st_size

        self.assertLessEqual(q1_size, q10_size)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 def test_jpg_progressive(self):
     test_image = Image.open(self.img_dir / "middle.png")
     result = optimize(test_image, {"format": "jpg"})
     self.assertEqual(result.info["progressive"], 1)
Ejemplo n.º 7
0
 def test_jpg_progressive(self):
     test_image = Image.open(self.img_dir / "middle.png")
     result = optimize(test_image, {'format': 'jpg'})
     self.assertEqual(result.info['progressive'], 1)