import imghdr import time import magic from PIL import Image """ 判断图片是否是jpg格式 """ def is_jpeg_image(image_path): image_format = imghdr.what(image_path) return image_format in ['jpeg', 'png', 'webp'] def is_jpeg_image1(image_path): try: with Image.open(image_path) as image: return image.format == 'JPEG' except IOError: return False # python-magic-bin def is_jpeg_image2(image_path): mime_type = magic.from_file(image_path, mime=True) return mime_type == 'image/jpeg' # if __name__ == '__main__': # start_time = time.time() # # print(is_jpeg_image(r"D:\test\image\111.txt")) # # print(is_jpeg(r"D:\test\image\111.txt")) # print(is_jpeg_image1(r"D:\test\image\111.jpg")) # print(time.time() - start_time)