示例#1
0
def checkAbortWhenInputIsUnsupported():
    try:
        img = loadImage(123456789.0)
    except:
        assert True
    else:
        assert False
示例#2
0
    def inpaint(self, image, dilation=0.02, method=cv.INPAINT_TELEA):
        """
        Function to inpaint the text in side an image

        INPUTS:
            image - PIL.Image or numpy.ndarray or str:
                The Image object or pixel array or path to the image.
            dilation - float: The amount of dilation to apply to the text mask.
            method: The inpainting algorithm to used. cv.INPAINT_TELEA or cv.INPAINT_NS.
        RETURNS:
            PIL.Image: The inpainted image.
        """

        image = loadImage(image).convert("RGB")
        image_array = np.array(image)

        vertices = self.getTextBoxes(image_array, debug_show_boxes=False)
        mask = self.generateTextMask(
            vertices,
            image_array.shape[0],
            image_array.shape[1],
            dilation=dilation,
        )

        impainted_image = cv.inpaint(image_array, mask, 15, method)

        # convert to PIL format
        impainted_image = Image.fromarray(impainted_image)

        return impainted_image
示例#3
0
def removeText(img, threshold=254, close_kernel_size=5, dilate_kernel_size=12):
    """
    Remove the WHITE texts of an image by inpainting

    INPUTS:
        img - PIL.Image.Image or np.ndarray or str: The path of or the PIL
            Image object of the original image
        threshold - int: the threshold pixel value at above which the pixel
            will be inpainted
        close_kernel_size - int: the kernel size of the closing operation
        dilate_kernel_size - int: the kernel size of the dilation operation

    RETURNS:
        img_inpainted - PIL.Image.Image: image with text removed/inpainted
    """

    # load image into PIL Image type
    img = loadImage(img)

    # convert image to np arrays
    img_array_gray = np.array(img.convert("L"))
    img_array_colored = np.array(img)

    # generate text mask
    _, mask = cv.threshold(img_array_gray, threshold, 255, cv.THRESH_BINARY)

    kernel_close = cv.getStructuringElement(
        cv.MORPH_RECT, (close_kernel_size, close_kernel_size))
    mask = cv.morphologyEx(mask, cv.MORPH_CLOSE, kernel_close)

    kernel_dilate = cv.getStructuringElement(
        cv.MORPH_RECT, (dilate_kernel_size, dilate_kernel_size))
    mask = cv.dilate(mask, kernel_dilate)

    # inpaint the text regions
    img_inpainted = cv.inpaint(img_array_colored, mask, 15, cv.INPAINT_NS)

    # convert to PIL format
    img_inpainted = Image.fromarray(img_inpainted)

    return img_inpainted
示例#4
0
    def inpaint(self, image, extra_region=0.02, method=cv.INPAINT_TELEA):
        """
        function to inpaint the text region of a image
        return an image as PIL Image format
        """

        image = loadImage(image).convert("RGB")
        image_array = np.array(image)

        vertices = self.getTextBoxes(image_array, show_boxes=False)
        mask = self.generateTextMask(
            vertices, image_array.shape[0], image_array.shape[1], enlargement=extra_region)

        impainted_image = cv.inpaint(image_array, mask, 15, method)
        # plt.imshow(impainted_image, vmin=0, vmax=255)
        # plt.show()

        # convert to PIL format
        impainted_image = Image.fromarray(impainted_image)

        return impainted_image
示例#5
0
def testCanPassThroughPILImage():
    img_new = loadImage(img)
    checkTwoImageAreSame(img, img_new)
示例#6
0
def testCanImportImageFromLocalPath():
    img_new = loadImage("tests/mmxai/text_removal/test_img.jpg")

    checkTwoImageAreSame(img, img_new)
示例#7
0
def testCanImportImageFromURL():
    img_new = loadImage(url)

    checkTwoImageAreSame(img, img_new)
示例#8
0
def testCanImportImageFromNumpyArray():
    img_array = np.array(img)
    img_new = loadImage(img_array)

    checkTwoImageAreSame(img, img_new)