Beispiel #1
0
 def retrieve(self, request, *args, **kwargs):
     instance = self.get_object()
     width = kwargs.get("width", settings.RETINA_DEFAULT_THUMBNAIL_SIZE)
     height = kwargs.get("height", settings.RETINA_DEFAULT_THUMBNAIL_SIZE)
     serializer_context = {"width": width, "height": height}
     serializer = B64ImageSerializer(instance, context=serializer_context)
     return Response(serializer.data)
    def test_image_no_parameters(self, factory):
        image = factory()
        serializer = B64ImageSerializer(image)
        image_itk = image.get_sitk_image()
        image_pil = B64ImageSerializer.convert_itk_to_pil(image_itk)
        image_bytes = B64ImageSerializer.create_thumbnail_as_b64(image_pil)
        assert serializer.data["content"] == image_bytes

        decoded_image_pil = PILImage.open(
            BytesIO(b64decode(serializer.data["content"])))
        assert decoded_image_pil.size == image_pil.size
    def test_image_resizes(self, factory, max_dimension):
        if max_dimension == "random":
            max_dimension = random.randint(1, 255)
        else:
            max_dimension = settings.RETINA_DEFAULT_THUMBNAIL_SIZE
        image = factory()
        serializer_context = {"width": max_dimension, "height": max_dimension}
        serializer = B64ImageSerializer(image, context=serializer_context)
        image_itk = image.get_sitk_image()
        image_pil = B64ImageSerializer.convert_itk_to_pil(image_itk)
        image_pil.thumbnail((max_dimension, max_dimension), PILImage.ANTIALIAS)

        decoded_image_pil = PILImage.open(
            BytesIO(b64decode(serializer.data["content"])))

        assert decoded_image_pil.size == image_pil.size
        assert max(decoded_image_pil.size) == max_dimension
 def test_non_existant_image_files(self):
     image = ImageFactoryWithoutImageFile()
     serializer = B64ImageSerializer(image)
     with pytest.raises(Http404):
         assert serializer.data