Esempio n. 1
0
    def sepia_image(self, request, *args, **kwargs):
        """ Changes image color to sepia """
        raw_image = self.get_object()
        new_img_name_prefix = "sepia_"
        new_folder_name = "sepia"
        new_img_name, image = utils.preprocessing_img(new_img_name_prefix,
                                                      new_folder_name,
                                                      raw_image)

        try:
            img_sepia = cv2.transform(
                image,
                np.matrix([[0.272, 0.534, 0.131], [0.349, 0.686, 0.168],
                           [0.393, 0.769, 0.189]]))

            # Check which entries have a value greater than 255 and set it to 255
            img_sepia[np.where(img_sepia > 255)] = 255
        except OSError:
            raise Http404("Image not found")
        else:
            # creates object comparable to temporary file in RAM
            is_success, buffer = cv2.imencode(".jpg", img_sepia)
            io_buf = io.BytesIO(buffer)
            data = ImageFile(io_buf)
            new_image = TheImage()
            new_image.category = 4
            new_image.file.save(os.path.join(new_folder_name, new_img_name),
                                data, True)
            return Response("Image has been desaturated to sepia.")
Esempio n. 2
0
    def gray_image(self, request, *args, **kwargs):
        """ Converts image to black and white """
        raw_image = self.get_object()
        new_img_name_prefix = "gray_"
        new_folder_name = "gray"
        new_img_name, image = utils.preprocessing_img(new_img_name_prefix,
                                                      new_folder_name,
                                                      raw_image)

        try:
            gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        except OSError:
            raise Http404("Image not found")
        else:
            # creates object comparable to temporary file in RAM
            is_success, buffer = cv2.imencode(".jpg", gray_image)
            io_buf = io.BytesIO(buffer)
            data = ImageFile(io_buf)
            new_image = TheImage()
            new_image.category = 2
            new_image.file.save(os.path.join(new_folder_name, new_img_name),
                                data, True)
            return Response("Image has been desaturated to gray.")
Esempio n. 3
0
    def flip_image_horizontally(self, request, *args, **kwargs):
        """ Flipps the image horizontally """
        raw_image = self.get_object()
        new_img_name_prefix = "flipped_hor_"
        new_folder_name = "flipped_horizontally"
        new_img_name, image = utils.preprocessing_img(new_img_name_prefix,
                                                      new_folder_name,
                                                      raw_image)

        try:
            flipped = cv2.flip(image, 1)
        except OSError:
            raise Http404("Image not found")
        else:
            # creates object comparable to temporary file in RAM
            is_success, buffer = cv2.imencode(".jpg", flipped)
            io_buf = io.BytesIO(buffer)
            data = ImageFile(io_buf)
            new_image = TheImage()
            new_image.category = 3
            new_image.file.save(os.path.join(new_folder_name, new_img_name),
                                data, True)
            return Response("Image has been flipped horizontally.")
Esempio n. 4
0
    def blur_image(self, request, *args, **kwargs):
        """ Blurrs the image and saves in database """
        raw_image = self.get_object()
        new_img_name_prefix = "blurred_"
        new_folder_name = "blurred"
        new_img_name, image = utils.preprocessing_img(new_img_name_prefix,
                                                      new_folder_name,
                                                      raw_image)

        try:
            blurred_img = cv2.GaussianBlur(image, (21, 21), cv2.BORDER_DEFAULT)
        except OSError:
            raise Http404("Image not found")
        else:
            # creates object comparable to temporary file in RAM
            is_success, buffer = cv2.imencode(".jpg", blurred_img)
            io_buf = io.BytesIO(buffer)
            data = ImageFile(io_buf)
            new_image = TheImage()
            new_image.category = 1
            new_image.file.save(os.path.join(new_folder_name, new_img_name),
                                data)
            return Response("Image has been blurred.")
Esempio n. 5
0
    def post(self, request, pk):
        def postprocessing_img(buffer):
            io_buf = io.BytesIO(buffer)
            data = ImageFile(io_buf)
            new_image = TheImage()
            new_image.category = 3
            try:
                new_image.file.save(
                    os.path.join(new_folder_name, new_img_name), data, True)
                return redirect(f"/result/{new_image.pk}")
            except OSError:
                raise Http404("Image not found")

        chosen_modification = request.POST.get("modificationType")
        raw_image = TheImage.objects.get(pk=pk)

        if chosen_modification == "blurred":
            new_img_name_prefix = "blurred_"
            new_folder_name = "blurred"
            new_img_name, image = utils.preprocessing_img(
                new_img_name_prefix, new_folder_name, raw_image)
            try:
                blurred_img = cv2.GaussianBlur(image, (21, 21),
                                               cv2.BORDER_DEFAULT)
                # creates object comparable to temporary file in RAM
                is_success, buffer = cv2.imencode(".jpg", blurred_img)
            except OSError:
                raise Http404("Image not found")
            return postprocessing_img(buffer)

        elif chosen_modification == "sepia":
            new_img_name_prefix = "sepia_"
            new_folder_name = "sepia"
            new_img_name, image = utils.preprocessing_img(
                new_img_name_prefix, new_folder_name, raw_image)
            try:
                img_sepia = cv2.transform(
                    image,
                    np.matrix([[0.272, 0.534, 0.131], [0.349, 0.686, 0.168],
                               [0.393, 0.769, 0.189]]))
                # Check which entries have a value greater than 255 and set it to 255
                img_sepia[np.where(img_sepia > 255)] = 255
                # creates object comparable to temporary file in RAM
                is_success, buffer = cv2.imencode(".jpg", img_sepia)
            except OSError:
                raise Http404("Image not found")
            return postprocessing_img(buffer)

        elif chosen_modification == "gray":
            new_img_name_prefix = "gray_"
            new_folder_name = "gray"
            new_img_name, image = utils.preprocessing_img(
                new_img_name_prefix, new_folder_name, raw_image)
            try:
                gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                # creates object comparable to temporary file in RAM
                is_success, buffer = cv2.imencode(".jpg", gray_image)
            except OSError:
                raise Http404("Image not found")
            return postprocessing_img(buffer)

        elif chosen_modification == "flipped-horizontally":
            new_img_name_prefix = "flipped_hor_"
            new_folder_name = "flipped_horizontally"
            new_img_name, image = utils.preprocessing_img(
                new_img_name_prefix, new_folder_name, raw_image)
            try:
                flipped = cv2.flip(image, 1)
                # creates object comparable to temporary file in RAM
                is_success, buffer = cv2.imencode(".jpg", flipped)
            except OSError:
                raise Http404("Image not found")
            return postprocessing_img(buffer)