2011-07-10 22 views
7

bir resim olup olmadığını (... jpg, bmp, png, vs) bir dosya resim olup olmadığını algılamak için herhangi bir genel yolu var mıbir dosya Python

algılama Veya dosya uzantılarının listesini yapıyor ve tek tek karşılaştırmayı tek yolla mı yapıyorsunuz?

+1

Standart python dosya türlerine göre http://docs.python.org/c-api/concrete.html görüntü dosyası standart değildir, bu nedenle bazı dış modüllerin gerekeceğini varsayalım. – timonti

+2

'imghdr modülünü kullanın. Bkz. [Bir dosyanın geçerli bir resim dosyası olup olmadığı nasıl kontrol edilir?] (Http://stackoverflow.com/questions/889333/how-to-check-if-a-file-is-a-valid-image-file) –

cevap

1

Bunun için bir kitaplık kullanmalısınız. Bu uzantı! = Dosya tipini not edin, çünkü .jpg dosyasına uzantıyı değiştirebilir, boya ile açabilir ve boya bir jpeg gibi yorumlayacaktır (örneğin). How to find the mime type of a file in python?'u kontrol etmelisiniz.

>>> files = {"a_movie.mkv", "an_image.png", "a_movie_without_extension", "an_image_without_extension"} 

Ve onlar senaryo klasöründe uygun film ve resim dosyaları şunlardır:

+2

Bu daha önce belirtilmişti - bu bir yorum olmalı, bir cevap olmamalı –

18

varsayarsak.

Yerleşik mimetek modülünü kullanabilirsiniz, ancak uzantı olmadan çalışmayacaktır.

>>> import mimetypes 
>>> {file: mimetypes.guess_type(file) for file in files} 
{'a_movie_without_extension': (None, None), 'an_image.png': ('image/png', None), 'an_image_without_extension': (None, None), 'a_movie.mkv': (None, None)} 

Veya unix komutunu file diyoruz. senin gibi, basitlik için,

>>> from PIL import Image 
>>> def check_image_with_pil(path): 
...  try: 
...   Image.open(path) 
...  except IOError: 
...   return False 
...  return True 
... 
>>> {file: check_image_with_pil(file) for file in files} 
{'a_movie_without_extension': False, 'an_image.png': True, 'an_image_without_extension': True, 'a_movie.mkv': False} 

Veya: Bu uzantılar olmadan çalışır, ancak Windows:

>>> import subprocess 
>>> def find_mime_with_file(path): 
...  command = "/usr/bin/file -i {0}".format(path) 
...  return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).communicate()[0].split()[1] 
... 
>>> {file: find_mime_with_file(file) for file in files} 
{'a_movie_without_extension': 'application/octet-stream;', 'an_image.png': 'image/png;', 'an_image_without_extension': 'image/png;', 'a_movie.mkv': 'application/octet-stream;'} 

Yoksa PIL ile açın ve hataları denetlemek için çalışıyorum, ama yüklü PIL ihtiyacı Sadece uzantıları kontrol et, düşündüğüm en iyi yol.

>>> extensions = {".jpg", ".png", ".gif"} #etc 
>>> {file: any(file.endswith(ext) for ext in extensions) for file in files} 
{'a_movie_without_extension': False, 'an_image.png': True, 'an_image_without_extension': False, 'a_movie.mkv': False} 
+0

+1 başkalarının “dosya” ya da seçenek iki kullanımının en iyi çalıştığım kullanım durumum için en iyi çalıştığımı belirttim. uzantı ve .jpg/.png olarak kaydetmeniz gerekiyor. – matchew

+0

Bunun için de kolay bir yol var .... "if 'file" request.files: "eğer dosya varsa bunu deneyin. . –

İlgili konular