from os import PathLike from PIL import Image path = "E:\\2025-云飞科技\\临时图片\\20250415_083503.jpg" def is_valid_image(file): bValid = True if isinstance(file, (str, PathLike)): fileObj = open(file, 'rb') else: fileObj = file buf = fileObj.read() if buf[6:10] in (b'JFIF', b'Exif'): #jpg图片 if not buf.rstrip(b'\0\r\n').endswith(b'\xff\xd9'): print(" "*10, "图片不完整") print(" "*35) bValid = False else: try: Image.open(fileObj).verify() except: print(" "*8, "图片打开失败", " "*10) print(" "*35) bValid = False return bValid if __name__ == "__main__": k = is_valid_image(path) print(k)