0

I'm trying to create a captcha solver for a site using python, but i'm not able to remove extra pixels (those with no other pixels around it)

i've managed to get really close, removing almost every extra pixel, but my code works only once, and then it stops having any effect on the image.

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))

i would like to remove any pixels with no other pixels either on the side or up and down.

this is the image i am using to test it

this is it before i ran the code once

9
  • I don't understand your question. You can't really "remove" a pixel from an image; images are raster based and rectangular so an image is W pixels wide by H pixels tall. If you "remove" one there will still have to be something there. So which pixels do you want to replace? And what do you want to replace them with? Jun 14, 2019 at 18:14
  • I want to replace every black pixel that has no other black pixels around it with a white one Jun 14, 2019 at 18:19
  • Where's your input image please? Is it pure black and white, colour or greyscale? Why have you removed your import statements so we can't see which libraries you are using or run your code? If you want folks to help you, it's generally a good idea to make it easy for them... ;-) Jun 14, 2019 at 18:24
  • i edited and included both the image and the imports Jun 14, 2019 at 18:35
  • What exactly do you mean your code only works once? And, if possible, edit your question and add code that shows this happening.
    – martineau
    Jun 14, 2019 at 18:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.