Esempio n. 1
0
def create_resource_image(resource, size=(32, 32), color=(250, 250, 210), format="JPEG", **instance_kwargs):
    """
    Create a ResourceImage object with image data with the given specs.

    :param resource: Resource to attach the ResourceImage to.
    :type resource: resources.models.Resource
    :param size: Size tuple
    :type size: tuple[int, int]
    :param color: RGB color triple
    :type color: tuple[int, int, int]
    :param format: PIL image format specifier
    :type format: str
    :param instance_kwargs: Other kwargs for `ResourceImage`. Some values are sanely prefilled.
    :type instance_kwargs: dict
    :return: Saved ResourceImage
    :rtype: resources.models.ResourceImage
    """
    instance_kwargs.setdefault("sort_order", resource.images.count() + 1)
    instance_kwargs.setdefault("type", "main")
    instance_kwargs.setdefault("image", ContentFile(
        get_test_image_data(size=size, color=color, format=format),
        name="%s.%s" % (instance_kwargs["sort_order"], format.lower())
    ))
    ri = ResourceImage(resource=resource, **instance_kwargs)
    ri.full_clean()
    ri.save()
    return ri
Esempio n. 2
0
def test_invalid_image(space_resource):
    data = b"this is text, not an image!"
    ri = ResourceImage(resource=space_resource,
                       sort_order=8,
                       type="main",
                       image=ContentFile(data, name="bogus.xyz"))
    with pytest.raises(InvalidImage) as ei:
        ri.full_clean()
    assert "cannot identify" in ei.value.message
Esempio n. 3
0
def test_invalid_image(space_resource):
    data = b"this is text, not an image!"
    ri = ResourceImage(
        resource=space_resource,
        sort_order=8,
        type="main",
        image=ContentFile(data, name="bogus.xyz")
    )
    with pytest.raises(InvalidImage) as ei:
        ri.full_clean()
    assert "cannot identify" in ei.value.message
Esempio n. 4
0
def test_image_transcoding_bypass(space_resource, format):
    """
    Test that JPEGs and PNGs bypass transcoding
    """
    data = get_test_image_data(format=format)
    ri = ResourceImage(resource=space_resource,
                       sort_order=8,
                       type="main",
                       image=ContentFile(data, name="nice.%s" % format))
    ri.full_clean()
    assert ri.image_format == format  # Transcoding did not occur
    assert Image.open(ri.image).format == format  # no, no transcoding
    ri.image.seek(0)  # PIL may have `seek`ed or read the stream
    assert ri.image.read() == data  # the bitstream is identical
Esempio n. 5
0
def test_image_transcoding(space_resource, format, image_type):
    """
    Test that images get transcoded into JPEG or PNG if they're not JPEG/PNG
    """
    data = get_test_image_data(format=format)
    ri = ResourceImage(resource=space_resource,
                       sort_order=8,
                       type=image_type,
                       image=ContentFile(data, name="long_horse.%s" % format))
    expected_format = ("PNG" if image_type in ("map",
                                               "ground_plan") else "JPEG")
    ri.full_clean()
    assert ri.image_format == expected_format  # Transcoding occurred
    assert Image.open(ri.image).format == expected_format  # .. it really did!
Esempio n. 6
0
def test_image_transcoding(space_resource, format, image_type):
    """
    Test that images get transcoded into JPEG or PNG if they're not JPEG/PNG
    """
    data = get_test_image_data(format=format)
    ri = ResourceImage(
        resource=space_resource,
        sort_order=8,
        type=image_type,
        image=ContentFile(data, name="long_horse.%s" % format)
    )
    expected_format = ("PNG" if image_type in ("map", "ground_plan") else "JPEG")
    ri.full_clean()
    assert ri.image_format == expected_format  # Transcoding occurred
    assert Image.open(ri.image).format == expected_format  # .. it really did!
Esempio n. 7
0
def test_image_transcoding_bypass(space_resource, format):
    """
    Test that JPEGs and PNGs bypass transcoding
    """
    data = get_test_image_data(format=format)
    ri = ResourceImage(
        resource=space_resource,
        sort_order=8,
        type="main",
        image=ContentFile(data, name="nice.%s" % format)
    )
    ri.full_clean()
    assert ri.image_format == format  # Transcoding did not occur
    assert Image.open(ri.image).format == format  # no, no transcoding
    ri.image.seek(0)  # PIL may have `seek`ed or read the stream
    assert ri.image.read() == data  # the bitstream is identical
Esempio n. 8
0
def create_resource_image(resource,
                          size=(32, 32),
                          color=(250, 250, 210),
                          format="JPEG",
                          **instance_kwargs):
    """
    Create a ResourceImage object with image data with the given specs.

    :param resource: Resource to attach the ResourceImage to.
    :type resource: resources.models.Resource
    :param size: Size tuple
    :type size: tuple[int, int]
    :param color: RGB color triple
    :type color: tuple[int, int, int]
    :param format: PIL image format specifier
    :type format: str
    :param instance_kwargs: Other kwargs for `ResourceImage`. Some values are sanely prefilled.
    :type instance_kwargs: dict
    :return: Saved ResourceImage
    :rtype: resources.models.ResourceImage
    """
    instance_kwargs.setdefault("sort_order", resource.images.count() + 1)
    instance_kwargs.setdefault("type", "main")
    instance_kwargs.setdefault(
        "image",
        ContentFile(get_test_image_data(size=size, color=color, format=format),
                    name="%s.%s" %
                    (instance_kwargs["sort_order"], format.lower())))
    ri = ResourceImage(resource=resource, **instance_kwargs)
    ri.full_clean()
    ri.save()
    return ri