<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: exif code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 07:36:46 GMT</pubDate>
    <description>DZone Snippets: exif code</description>
    <item>
      <title>Change system date of photos to EXIF date</title>
      <link>http://snippets.dzone.com/posts/show/4089</link>
      <description>This script takes a list of files as arguments.&lt;br /&gt;It set the system datetime (both modifitation and access) to the date &lt;br /&gt;the photo was taken.&lt;br /&gt;&lt;br /&gt;It use the EXIF module for that, that needs to be in the same folder as the script :&lt;br /&gt;You can find it here :&lt;br /&gt;http://home.cfl.rr.com/genecash/digital_camera/EXIF.py &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/python&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;import EXIF&lt;br /&gt;from datetime import *&lt;br /&gt;import time&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;# Loop on arguments (files)&lt;br /&gt;for arg in sys.argv[1:] :&lt;br /&gt;    &lt;br /&gt;    # Do nothing of dirs&lt;br /&gt;    if os.path.isdir(arg) :&lt;br /&gt;        continue&lt;br /&gt;&lt;br /&gt;    # Open the file&lt;br /&gt;    f=open(arg, 'rb')&lt;br /&gt; &lt;br /&gt;    # Read exif data&lt;br /&gt;    tags = EXIF.process_file(f)&lt;br /&gt;    &lt;br /&gt;    # Ensure date is present &lt;br /&gt;    if not tags.has_key("Image DateTime") :&lt;br /&gt;        continue &lt;br /&gt;        &lt;br /&gt;    # Get date of photo&lt;br /&gt;    date = tags["Image DateTime"]&lt;br /&gt;    date = datetime(*(time.strptime(date.values, "%Y:%m:%d %H:%M:%S")[0:6]))&lt;br /&gt;    timestamp = int(time.mktime(date.timetuple()))&lt;br /&gt;    &lt;br /&gt;    # Some traces&lt;br /&gt;    print "File:%s - Time:%s " % (arg, timestamp)&lt;br /&gt;&lt;br /&gt;    # Change the date&lt;br /&gt;    os.utime(arg, (timestamp, timestamp))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jun 2007 00:10:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4089</guid>
      <author>cdvddt (Raphael Jolivet)</author>
    </item>
    <item>
      <title>Read &amp; Write JPEG COM and EXIF metadata</title>
      <link>http://snippets.dzone.com/posts/show/1021</link>
      <description>This snippet is similar to my &lt;a href=http://bigbold.com/snippets/posts/show/768&gt;previous one&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import jpeg&lt;br /&gt;&lt;br /&gt;#read/write JPEG comments (aka the COM area)&lt;br /&gt;jpeg.getComments(file)&lt;br /&gt;jpeg.setComments(txt, file)&lt;br /&gt;&lt;br /&gt;#read/write some EXIF data&lt;br /&gt;e = jpeg.getExif(file)   #an Exif object&lt;br /&gt;e2 = jpeg.getExif2(file) #an Exif extension segment as Exif instance too&lt;br /&gt;&lt;br /&gt;#quick getter&lt;br /&gt;d = e.dict()&lt;br /&gt;print d['image description']&lt;br /&gt;e.display()  #function to print all tags and their value, type, etc.&lt;br /&gt;&lt;br /&gt;#some tags with an explicit interface, some are writable&lt;br /&gt;print e.description&lt;br /&gt;e.description = "my nice picture"&lt;br /&gt;&lt;br /&gt;#generic read function&lt;br /&gt;print e.get(0x010e)  #value for a tag number you know&lt;br /&gt;print e.get(0x010e, value=False) #get the tag as a Tag instance&lt;br /&gt;&lt;br /&gt;#generic write function&lt;br /&gt;e.set(0x010e, "my nice picture", e.ifd0, 2)&lt;br /&gt;    #e.ifd0, e.exif, e.gps or e.interop, represents the IFD the tag belongs to&lt;br /&gt;    #and 2 represents the fact that the tag is of ASCII type&lt;br /&gt;    #see www.exif.org specifications for details&lt;br /&gt;&lt;br /&gt;#iterate&lt;br /&gt;for tag in e:&lt;br /&gt;   print tag.getValue() #value parsed&lt;br /&gt;   print tag.value      #value raw&lt;br /&gt;   tag.setValue(100)    #change it&lt;br /&gt;&lt;br /&gt;#save the changed Exif segment back into the image file&lt;br /&gt;jpeg.setExif(e, file) &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;See more details, and download the module &lt;a href=http://www.emilas.com/jpeg/&gt;here&lt;/a&gt;.</description>
      <pubDate>Tue, 27 Dec 2005 20:07:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1021</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Reading and writing image metadata</title>
      <link>http://snippets.dzone.com/posts/show/768</link>
      <description>The IPTCInfo module allows you to add/edit metadata &lt;br /&gt;to your image. You can download it from &lt;a href=http://cheeseshop.python.org/pypi/IPTCInfo/&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Reading&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from iptcinfo import IPTCInfo&lt;br /&gt;info = IPTCInfo('test.jpg')&lt;br /&gt;print info.keywords, info.supplementalCategories, info.contacts&lt;br /&gt;caption = info.data['caption/abstract']&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Add/Edit&lt;br /&gt;&lt;code&gt;&lt;br /&gt;info = IPTCInfo('test.jpg')&lt;br /&gt;info.data['caption/abstract'] = 'Witty caption here'&lt;br /&gt;info.data['supplemental category'] = ['portrait']&lt;br /&gt;info.save()&lt;br /&gt;info.saveAs('test_out.jpg')  # keep original safe&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 29 Sep 2005 22:09:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/768</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
