views:

151

answers:

5

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...

+2  A: 

Use the magic module to get the MIME type, and look for image/jpeg.

Ignacio Vazquez-Abrams
+1  A: 
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)
ghostdog74
It is working....Thank u...:)
Nims
I find that the `str.endswith()` is easier to read.@Nims: consider validating this answer, or at least upvoting it.
Bastien Léonard
I suggest `if os.path.splitext(files)[1] in ('.jpg', '.jpeg'):`, otherwise a file named `imnotajpeg` would be processed.
nosklo
+7  A: 

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']
Anurag Uniyal
+2  A: 

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.

David Morrissey
`splitext` returns the extension with a dot, so you want `in ('.jpg', '.jpeg')`.
nosklo
Yeah, you're right. I've updated it, thanks for correcting me :-)
David Morrissey
But I don't want to find files in the subdirectories.
Nims
OK, I've updated the answer to include only scanning a single folder.
David Morrissey
+1  A: 

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."
Frederik