Python PIL.ImageFilter.MedianFilter() Examples

The following are 10 code examples of PIL.ImageFilter.MedianFilter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PIL.ImageFilter , or try the search function .
Example #1
Source File: transformers.py    From deepchem with MIT License 6 votes vote down vote up
def median_filter(self, size):
    """ Calculates a multidimensional median filter

    Parameters
    ----------
    size: int
	The kernel size in pixels.

    Returns
    ----------
    The median filtered image.
    """
    from PIL import Image, ImageFilter
    image = Image.fromarray(self.Image)
    image = image.filter(ImageFilter.MedianFilter(size=size))
    return np.array(image) 
Example #2
Source File: test_image_filter.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_sanity(self):

        def filter(filter):
            for mode in ["L", "RGB", "CMYK"]:
                im = hopper(mode)
                out = im.filter(filter)
                self.assertEqual(out.mode, im.mode)
                self.assertEqual(out.size, im.size)

        filter(ImageFilter.BLUR)
        filter(ImageFilter.CONTOUR)
        filter(ImageFilter.DETAIL)
        filter(ImageFilter.EDGE_ENHANCE)
        filter(ImageFilter.EDGE_ENHANCE_MORE)
        filter(ImageFilter.EMBOSS)
        filter(ImageFilter.FIND_EDGES)
        filter(ImageFilter.SMOOTH)
        filter(ImageFilter.SMOOTH_MORE)
        filter(ImageFilter.SHARPEN)
        filter(ImageFilter.MaxFilter)
        filter(ImageFilter.MedianFilter)
        filter(ImageFilter.MinFilter)
        filter(ImageFilter.ModeFilter)
        filter(ImageFilter.GaussianBlur)
        filter(ImageFilter.GaussianBlur(5))
        filter(ImageFilter.BoxBlur(5))
        filter(ImageFilter.UnsharpMask)
        filter(ImageFilter.UnsharpMask(10))

        self.assertRaises(TypeError, filter, "hello") 
Example #3
Source File: test_image_filter.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_rankfilter(self):

        def rankfilter(mode):
            im = Image.new(mode, (3, 3), None)
            im.putdata(list(range(9)))
            # image is:
            #   0 1 2
            #   3 4 5
            #   6 7 8
            minimum = im.filter(ImageFilter.MinFilter).getpixel((1, 1))
            med = im.filter(ImageFilter.MedianFilter).getpixel((1, 1))
            maximum = im.filter(ImageFilter.MaxFilter).getpixel((1, 1))
            return minimum, med, maximum

        self.assertEqual(rankfilter("1"), (0, 4, 8))
        self.assertEqual(rankfilter("L"), (0, 4, 8))
        self.assertRaises(ValueError, rankfilter, "P")
        self.assertEqual(rankfilter("RGB"), ((0, 0, 0), (4, 0, 0), (8, 0, 0)))
        self.assertEqual(rankfilter("I"), (0, 4, 8))
        self.assertEqual(rankfilter("F"), (0.0, 4.0, 8.0)) 
Example #4
Source File: helpers.py    From OdooQuant with GNU General Public License v3.0 6 votes vote down vote up
def detect_gf_result(image_path):
    from PIL import ImageFilter, Image
    import pytesseract
    img = Image.open(image_path)
    for x in range(img.width):
        for y in range(img.height):
            if img.getpixel((x, y)) < (100, 100, 100):
                img.putpixel((x, y), (256, 256, 256))
    gray = img.convert('L')
    two = gray.point(lambda x: 0 if 68 < x < 90 else 256)
    min_res = two.filter(ImageFilter.MinFilter)
    med_res = min_res.filter(ImageFilter.MedianFilter)
    for _ in range(2):
        med_res = med_res.filter(ImageFilter.MedianFilter)
    res = pytesseract.image_to_string(med_res, config='-psm 6')
    return res.replace(' ', '') 
Example #5
Source File: captcha.py    From easytrader with MIT License 6 votes vote down vote up
def detect_gf_result(image_path):
    from PIL import ImageFilter, Image

    img = Image.open(image_path)
    if hasattr(img, "width"):
        width, height = img.width, img.height
    else:
        width, height = img.size
    for x in range(width):
        for y in range(height):
            if img.getpixel((x, y)) < (100, 100, 100):
                img.putpixel((x, y), (256, 256, 256))
    gray = img.convert("L")
    two = gray.point(lambda p: 0 if 68 < p < 90 else 256)
    min_res = two.filter(ImageFilter.MinFilter)
    med_res = min_res.filter(ImageFilter.MedianFilter)
    for _ in range(2):
        med_res = med_res.filter(ImageFilter.MedianFilter)
    return invoke_tesseract_to_recognize(med_res) 
Example #6
Source File: test_transformers.py    From deepchem with MIT License 5 votes vote down vote up
def test_median_filter(self):
    #Check median filter
    from PIL import Image, ImageFilter
    dt = DataTransforms(self.d)
    filtered = dt.median_filter(size=3)
    image = Image.fromarray(self.d)
    image = image.filter(ImageFilter.MedianFilter(size=3))
    check_filtered = np.array(image)
    assert np.allclose(check_filtered, filtered) 
Example #7
Source File: gfTrader.py    From vxTrader with MIT License 5 votes vote down vote up
def vcode(self):

        # 获取校验码
        r = self._session.get('https://trade.gf.com.cn/yzm.jpgx')
        r.raise_for_status()

        # 通过内存保存图片,进行识别
        img_buffer = BytesIO(r.content)
        img = Image.open(img_buffer)
        if hasattr(img, "width"):
            width, height = img.width, img.height
        else:
            width, height = img.size
        for x in range(width):
            for y in range(height):
                if img.getpixel((x, y)) < (100, 100, 100):
                    img.putpixel((x, y), (256, 256, 256))

        gray = img.convert('L')
        two = gray.point(lambda x: 0 if 68 < x < 90 else 256)
        min_res = two.filter(ImageFilter.MinFilter)
        med_res = min_res.filter(ImageFilter.MedianFilter)
        for _ in range(1):
            med_res = med_res.filter(ImageFilter.MedianFilter)

        # 通过tesseract-ocr的工具进行校验码识别
        vcode = pytesseract.image_to_string(med_res)
        img.close()
        img_buffer.close()

        vcode = vcode.replace(' ', '')
        if self.code_rule.findall(vcode) != []:
            logger.debug('vcode is: %s' % vcode)
            return vcode
        else:
            raise VerifyCodeError('verify code error: %s' % vcode) 
Example #8
Source File: __init__.py    From fakemenot with MIT License 5 votes vote down vote up
def _blow_up_image():
    try:
        img = Image.open(args.image)
    except FileNotFoundError:
        print(colored("[!] I couldn't find a file by that name. Fake you!",
                      'red'))
        return False
    except OSError:
        print(colored("[!] {} is not an image file!".format(args.image),
                      'red'))
        return False

    basewidth = 2500
    img = Image.open(args.image)
    wpercent = (basewidth / float(img.size[0]))
    hsize = int((float(img.size[1]) * float(wpercent)))
    # Resize happens here
    img = img.resize((basewidth, hsize), Image.ANTIALIAS)

    # Thanks Stack Overflow <3 : https://stackoverflow.com/a/37750605/5486120
    img = img.filter(ImageFilter.MedianFilter())
    enhancer = ImageEnhance.Contrast(img)
    img = enhancer.enhance(2)

    # Return the sexy image object
    return img 
Example #9
Source File: util.py    From elegance with MIT License 5 votes vote down vote up
def blur_image(pil_img,mode,r):
    if mode == "g":
        kernel = ImageFilter.GaussianBlur(radius=r)
    elif mode == "m":
        kernel = ImageFilter.MedianFilter(size=r)
    else:
        raise ValueError("{} not support".format(mode))
    return pil_img.filter(kernel) 
Example #10
Source File: utils.py    From Anime-Super-Resolution with MIT License 5 votes vote down vote up
def medianfilter(self, image, size=3):
        return image.filter(ImageFilter.MedianFilter(size=size))