我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用PIL.ImageFilter.MinFilter()。
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(' ', '')
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)
def minfilter_img(imdir,outdir): im = Image.open(imdir) out_filename = outdir out_img = im.filter(ImageFilter.MinFilter).save(out_filename, 'JPEG', quality=100)
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)
def filter_footer(self, img): """Filter to remove the hight quality footer for an image.""" # Some sites like MangaFox add an extra footer in the original # image. This footer remove importan space in the Kindle, and # we need to remove it. # # The algorithm use as a leverage the normal noise present in # an scanned image, that is higher than the one in the footer. # This means that this filter will only work in medium quality # scanners, but possibly not in high quality ones. # # The process is like this: # # 1.- Binarize the image, moving the noise at the same level # that the real information. # # 2.- Use a MinFilter of size 3 to a big mass of pixels that # containg high frequency data. That usually means # pixels surrounded with blanks. # # 3.- Do a Gaussian filter to lower more the high frequency # data, moving the mass close arround the pixel. This # will lower more the pixels surrounded with gaps. # # 4.- Discard the pixels with low mass. # _img = ImageOps.invert(img.convert(mode='L')) _img = _img.point(lambda x: x and 255) _img = _img.filter(ImageFilter.MinFilter(size=3)) _img = _img.filter(ImageFilter.GaussianBlur(radius=5)) _img = _img.point(lambda x: (x >= 48) and x) # If the image is white, we do not have bbox return img.crop(_img.getbbox()) if _img.getbbox() else img