Python lets a picture sort by the creation time in EXIF information _python

Source: Internet
Author: User

This example describes how Python lets a picture sort by the creation time in EXIF information. Share to everyone for your reference. The specific analysis is as follows:

We often take photos from different devices, such as cameras, cell phones, iphones, and so on, the creation date of the operating system record is often changed because of the copy, and so on, the following code can give the picture according to the creation time in Exif to sort, very useful.

Copy Code code as follows:

Import OS
Import Shutil
Import Image
From PiL. Exiftags Import TAGS
Def print_all_known_exif_tags ():
For k in Sorted (TAGS):
Print K, tags[k]
def print_all_exif_tags (image):
Try
img = Image.open (Image)
Except Exception, E:
Print image, "skipping due to", E
Else
XF = Img._getexif ()
For tag in XF:
Print Tags.get (tag), Xf[tag]
Finally
print ' Done '
def get_minimum_creation_time (Exif_data):
Mtime = "?"
If 306 in Exif_data and exif_data[306] < mtime: # 306 = DateTime
Mtime = exif_data[306]
If 36867 in Exif_data and exif_data[36867] < mtime: # 36867 = datetimeoriginal
Mtime = exif_data[36867]
If 36868 in Exif_data and exif_data[36868] < mtime: # 36868 = datetimedigitized
Mtime = exif_data[36868]
Return Mtime
def get_creationdate_with_filename_as_dict (list_of_folders):
Print "Processing all image files in:"
result = {}
For folder in List_of_folders:
Print "-" + folder
Counter = 0
For f in Os.listdir (folder):
Counter + 1
Fullfilename = folder + "\" + F
Try
img = Image.open (fullfilename)
Except Exception, E:
Print "Skipping '%s ' due to exception:%s"% (f, E)
Continue
Mtime = Get_minimum_creation_time (Img._getexif ())
i = 0
While mtime+ "_" *i in Result:
i + 1
Mtime = mtime+ "_" *i
Result[mtime] = Fullfilename
Print "Found%s orignal files in%s."% ( Counter, folder)
Print "Added total of%s to dictionary." %len (Result)
return result
def copy_from_image_dict_to_directory (Image_dict, Output_dir):
Assert Os.path.exists (Output_dir)
For I,key in enumerate (sorted (image_dict)):
Dummy, extension = Os.path.splitext (Image_dict[key])
New_file_name = Key.replace (":", "-") + extension
Output_file = Output_dir + new_file_name
Shutil.copy2 (Image_dict[key], output_file)
Print "Copied%s files to%s"% (i+1, Output_dir)
If __name__== "__main__":
Source_dir = "/var/tmp/images"
Output_dir = "/var/tmp/output"
# Obtain/var/tmp/images/iphone,/var/tmp/images/canonpowershot,/var/tmp/images/nikon1
List_of_folders = [Source_dir + subfolder for subfolder in Os.listdir (Source_dir)]
All_files = Get_creationdate_with_filename_as_dict (list_of_folders)
Copy_from_image_dict_to_directory (All_files, Output_dir)

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.