As the development language of artificial intelligence, Python is also the direction for many people to learn. Little ape gaga Share a Python knowledge point, hoping to help those students who are learning Python. Today, we share how to judge the integrity of jpeg images.
It's very easy to judge the file format by extension, but it may be wrong. The jpeg file has a fixed file header, whose format is as follows:
Start Marker | JFIF Marker | Header Length | Identifier
0xff, 0xd8 | 0xff, 0xe0 | 2-bytes | "JFIF\0"
So you can quickly determine the file format through the file header:
def is_jpg(filename): data = open(filename,'rb').read(11) if data[:4] != '\xff\xd8\xff\xe0' and data[:4]!='\xff\xd8\xff\xe1': return False if data[6:] != 'JFIF\0' and data[6:] != 'Exif\0': return False return True //You can also judge through the PIL class library: from PIL import Image def is_jpg(filename): try: i=Image.open(filename) return i.format =='JPEG' except IOError: return Fals //Application scenario: judge whether the jpeg file in the image folder is complete. The code is as follows: #coding=utf-8 #summary: judge the validity of pictures import io import os from PIL import Image #Judge whether the file is a valid (complete) picture #Input parameter is file path #There will be missed inspection def IsValidImage(pathfile): bValid = True try: Image.open(pathfile).verify() except: bValid = False return bValid def is_valid_jpg(jpg_file): """judge JPG Whether the file download is complete """ if jpg_file.split('.')[-1].lower() == 'jpg': with open(jpg_file, 'rb') as f: f.seek(-2, 2) return f.read() == '\xff\xd9' #Determine whether jpg contains an end field else: return True #Using the PIL library to determine the jpeg format, but some files without end fields cannot be detected. def is_jpg(filename): try: i=Image.open(filename) return i.format =='JPEG' except IOError: return False allfiles=os.listdir('image') log_file=open('img_lossinfo.txt','w') log = open('img_r.txt','w') log_w=open('img_w.txt','w') log1=open('img_jpeg.txt','w') log2=open('img_notjpg.txt','w') for i in allfiles: #if 1: if i[-4:]=='.jpg': f=os.path.join('image',i) value=IsValidImage(f) if not value: log_file.write(i+'\n') if is_valid_jpg(f): print f log.write(i+'\n') else: log_w.write(i+'\n') if is_jpg(f): log1.write(i+'\n') else: log2.write(i+'\n')
The above is all about how to judge the integrity of jpeg images in the python development of the little ape circle. I hope to give you a reference. Finally, I want to learn more about Python and artificial intelligence. You can pay attention to Little ape circle Every day, we will update our knowledge from time to time.