如何从图像中去除多余的像素?

2024-06-28 19:31:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python为一个站点创建一个captcha解算器,但是我无法移除额外的像素(那些周围没有其他像素的像素)

我已经设法接近了,几乎删除了每一个额外的像素,但我的代码只工作了一次,然后它就不再对图像产生任何影响了。在

try:
    from PIL import Image
except ImportError:
    import Image
import pytesseract
from operator import itemgetter
import PIL.ImageOps


for x in range(im2.size[1]):
  for y in range(im2.size[0]):
    pix = im2.getpixel((y,x))
    im2.putpixel((0,21), (0,0,0))
    if pix < 180:
        try:
            left = im2.getpixel((y-1,x))
        except IndexError:
            left = 255
            pass
        try:
            right = im2.getpixel((y+1,x))
        except IndexError:
            right = 255
            pass
        try:
            up = im2.getpixel((y,x-1))
        except IndexError:
            up = 255
            pass
        try:
            down = im2.getpixel((y,x+1))
        except IndexError:
            down = 255
            pass
        if (left == 255 and right == 255) or (up == 255 and down == 255):
            im2.putpixel((y,x),(255,255,255))

我想删除任何像素没有其他像素在侧面或上下。在

this is the image i am using to test it

this is it before i ran the code once


Tags: fromimageimportrightpilpass像素left