2

I used python OpenCV to detect and crop text from images, in this case, there are some piece of images which doesn't have letters or any other symbols are also cropped(empty area) are added to the folder I want to remove those specific images I used cv2.findNonZeros to delete this images but I cannot achieve this, enter image description here

I want to delete this image using python in my code is there any simple logics to achieve this?

suji
  • 407
  • 4
  • 17
  • 1
    Do you want to delete the infividual files from your disk? see os.remove https://docs.python.org/3/library/os.html#os.remove – Equinox Jun 27 '20 at 12:51

2 Answers2

1

Your algorithm should look like this: For each image in folder check if all pixel are white (here you can also check if file is image). If all pixel are white delete file.

  1. How to loop through directory: Loop through directory of images and rotate them all x degrees and save to directory

  2. Check if image is all white pixels: Check if image is all white pixels with OpenCV

  3. Delete a file: https://www.dummies.com/programming/python/how-to-delete-a-file-in-python/

With this links you can easy solve your problem

xszym
  • 928
  • 1
  • 5
  • 11
1

So you need to delete all images that are of a single color, i.e. Complete Black/White/Any Other Color.

import Image, os
import os
path = "C:\\Users\\Ajju\\Desktop\\test_images"
for filename in os.listdir(path):
    img = Image.open(path + '\\' + filename)
    clrs = img.getcolors()
    print filename, len(clrs)
    if len(clrs) == 1:
        os.remove(path + '\\' + filename)

Notes :

  1. Do not put your script in the same folder as images, because it will try to open your script as an image, and throw an error.

  2. len(clrs) actually shows how many colors are present in an image. If its 1, means the image is of one color.