You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
803B

  1. import imghdr
  2. import time
  3. import magic
  4. from PIL import Image
  5. """
  6. 判断图片是否是jpg格式
  7. """
  8. def is_jpeg_image(image_path):
  9. image_format = imghdr.what(image_path)
  10. return image_format in ['jpeg', 'png', 'webp']
  11. def is_jpeg_image1(image_path):
  12. try:
  13. with Image.open(image_path) as image:
  14. return image.format == 'JPEG'
  15. except IOError:
  16. return False
  17. # python-magic-bin
  18. def is_jpeg_image2(image_path):
  19. mime_type = magic.from_file(image_path, mime=True)
  20. return mime_type == 'image/jpeg'
  21. # if __name__ == '__main__':
  22. # start_time = time.time()
  23. # # print(is_jpeg_image(r"D:\test\image\111.txt"))
  24. # # print(is_jpeg(r"D:\test\image\111.txt"))
  25. # print(is_jpeg_image1(r"D:\test\image\111.jpg"))
  26. # print(time.time() - start_time)