Hi,
My requirement is to search for jpeg images files in a directory using python script and list the file names. Can anyone help me on how to identify jpeg images files.
Thanks in advance...
Hi,
My requirement is to search for jpeg images files in a directory using python script and list the file names. Can anyone help me on how to identify jpeg images files.
Thanks in advance...
Use the magic
module to get the MIME type, and look for image/jpeg
.
import os
path=os.path.join("/home","mypath","to_search")
for r,d,f in os.walk(path):
for files in f:
if files[-3:].lower()=='jpg' of files[-4:].lower() =="jpeg":
print "found: ",os.path.join(r,files)
if you need to search a single folder non-recursively you can simply do
>>> import glob
>>> glob.glob("D:\\bluetooth\*.jpg")
['D:\\bluetooth\\Image1475.jpg', 'D:\\bluetooth\\Image1514.jpg']
Read more about glob here, you can do unix like wildcard searches e.g.
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
If you want to scan subfolders:
import os
for root, subdirs, files in os.walk(DIRECTORY):
for file in files:
if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg'):
print os.path.join(root, file)
Otherwise, using one of the other glob functions in the other answers, or this:
import os
for f in os.listdir(DIRECTORY):
if os.path.splitext(f)[1].lower() in ('.jpg', '.jpeg'):
print os.path.join(DIRECTORY, f)
should work OK.
If you want to determine the image format by file contents, you can use the Python Imaging Library:
import Image
try:
img = Image.open('maybe_jpeg_file')
print img.format # Will return 'JPEG' for JPEG files.
except IOError:
print "Not an image file or unreadable."