[Python] detects incomplete downloads, half gray JPG, JPEG, PNG picture script

Why has the problem is it?
1, network problems: the network is unstable, the received information is not a complete picture. That response in question;
2, local disk is full: sometimes received picture information is complete, but save any local disk, after all, have a picture of it several Mb.

How to detect errors pictures?
Tried so many ways, a better approach is to determine the identity jpg, jpeg, png file ending.

JPG file ending identifies: \ xFF \ xd9
JPEG file ending identifies: \ xFF \ xd9
PNG file ending identifies: \ xaeB` \ x82
detect incomplete picture Code

class CheckImage(object):

    def __init__(self, img):
        with open(img, "rb") as f:
            f.seek(-2, 2)
            self.img_text = f.read()
            f.close()

    check_jpg_jpeg DEF (Self):
        "" "detection jpg image integrity, complete returns True, incomplete returns False" ""
        buf = self.img_text
        return buf.endswith (b '\ xFF \ xd9')

    check_png DEF (Self):
        "" "detection png image integrity, complete returns True, incomplete returns False" ""

        buf = self.img_text
        return buf.endswith(b'\xaeB`\x82')

Detect a folder without the full picture, and delete

import os
from CheckImage import CheckImage


class CheckBrockImage(object):
    def __init__(self, train_dir):
        self.train_dir = train_dir
        self.completeFile = 0
        self.incompleteFile = 0

    get_imgs DEF (Self):
        "" "through all the pictures in a folder" ""
        for File in os.listdir (self.train_dir):
            IF os.path.splitext (File) [1] .lower () = = '.jpg' or os.path.splitext (File) [. 1] .lower () == ".jpeg":
                RET = self.check_img (File)
                IF RET:
                    self.completeFile +. 1 =

                the else:
                    self.incompleteFile self.incompleteFile + = 1
                    # self.img_remove (File) # delete incomplete picture

    def img_remove(self, file):
        """删除图片"""
        os.remove(self.train_dir + file)

    check_img DEF (Self, img_file):
        "" "detect the integrity of the picture, the picture complete returns True, the picture is incomplete returns False" ""
        return checkImage (self.train_dir + img_file) .check_jpg_jpeg ()

    RUN DEF (Self):
        "" "executable file" ""
        self.get_imgs ()
        Print ( 'incomplete picture:% d months'% self.incompleteFile)
        Print ( 'the complete picture:% d months'% self.completeFile)


the __name__ == IF '__main__':
    train_dir = 'C: / the Users / 79993 / Desktop / BrockImg / TextImg /' # detection folder
    imgs = CheckBrockImage (train_dir)
    imgs.run ()
 

Skip incomplete picture jpg

 with open(os.path.join(path, filename), 'rb') as f:
    
                check_chars = f.read()[-2:]
                if check_chars != b'\xff\xd9':
        
                  continue
                else:
                  img = cv2.imread(os.path.join(path, filename), 1)
 

Published 41 original articles · won praise 7 · views 3668

Guess you like

Origin blog.csdn.net/weixin_43091087/article/details/105093506