About Me

Monday, August 2, 2010

Sorting Photos by Filename on the iPad #fail


For a company that is known for designing great user interfaces that are pleasant and simple to use, Apple definitely made it hard for me to do something trivial. So I have a folder of image files with numerical names on my Windows PC, e.g:

00.png
01.png
02.png
...

I want to transfer them to my iPad so I can enjoy them on the go. I pull out my trusty Apple 30 pin to USB cable and connect my PC to my iPad and wait for iTunes to launch. At this point I'm thinking to myself, on my Android phone I could do all this over the air... Sigh. But let's continue... I sync my folder to the iPad and disconnect.

At this point I expected to have my photos in the logical order defined by my clever numeric nomenclature, but nay! I seem to have an arbitrary ordering of my photos. WTF?

So how do I solve this problem? ...Google... According to the plethora of Apple fanboys out there on the Internets, I should switch to a Mac and use iPhoto which has an option to sort by filename. That's great! But I'm a PC :( --Any other bright ideas?

Well, apparently, according to Apple Support (http://support.apple.com/kb/HT4221):


If your iPhone, iPad, or iPod touch is using the latest software, it sorts photos by the Date Taken tag of the photos. Specifically, your device uses the pictures' EXIF tags to determine the Date Taken information. Your device will look for the following EXIF tags within your pictures:
  1. Capture Date
  2. Date Time Digitized
  3. Date Time Original
  4. Last Modified
Okay! I'll work around the iPad's UI deficiency and modify the Date Taken of all my photos. So I fire up Windows Live Photo Gallery, select all files in that folder, change the Date taken to Aug 2nd 2010 at 11:00am. Awesome! Now I reconnect my iPad to the PC, delete the incorrectly sorted folder and resync with the newly updated folder... No dice. Same ordering as before.

Maybe it's because I changed all the modification times to the same date? Maybe iTunes caches the photos? I don't know what the issue is. I'll assume it's the former. Okay I'll resort to python now.

...code...code...code...

Here we go: sort_ipad_photos.py

#!/usr/bin/python

import datetime, time, sys, os

# iPad sorts photos by timestamp rather than by name. A solution to this is to
# change the timestamps for files so that they are sorted in the same order as
# the file names.

def get_next_time(count):
future = datetime.datetime(2010, 1, 1, 0, 0)
future += datetime.timedelta(minutes = count)
timestamp = time.mktime(future.timetuple())
return timestamp

def main():
folder = sys.argv[1]

list = os.listdir(folder)
list.sort(reverse=True)

count = 0
for file in list:
if file.lower().endswith('jpg') or file.lower().endswith('png'):
count += 1
time = get_next_time(count)
print('Changing timestamp of %s to %d' % (file, time))
os.utime(file, (time, time))

USAGE = 'Usage: python sort_ipad_photos.py folder'

if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit(USAGE)

main()

So this script will essentially change the modification and access timestamps of the files so that the first image will be the most recent one. E.g. if there are 20 images, #20 has a timestamp of Jan 01 2010 00:01 and #19 has a timestamp of Jan 01 2010 00:02. One minute increments.

This should solve my problem. I run the script and test out the ordering with `ls -t`. Looks good. I repeat the delete and sync folder to my iPad process again. No dice. Same ordering! WTF?!

The issue must be the creation date. I modified the access and modification times but not the creation date. New solution: Create new files sequentially so that their timestamps have the same order as their filenames.

...code...code...code...

#!/usr/bin/python

import time, sys, os, shutil

# iPad sorts photos by timestamp rather than by name. A solution is to
# create new files with creation dates that match the ordering of the filenames.

def create_new_files():
folder = sys.argv[1]

list = os.listdir(folder)
list.sort(reverse=True)
sorted_folder = folder + '/sorted'
if not os.path.exists(sorted_folder):
os.mkdir(sorted_folder)

count = 0
for file in list:
if file.lower().endswith('jpg') or file.lower().endswith('png'):
shutil.copyfile(file, sorted_folder + '/' + file)
time.sleep(1)

def main():
create_new_files()

USAGE = 'Usage: python sort_ipad_photos.py folder'

if __name__ == '__main__':
if len(sys.argv) == 1:
sys.exit(USAGE)

main()

Done! Okay this one actually worked! Thank you Apple for wasting 2 hours of my time!

1 comment:

  1. hey!!There is an iPad app named sortshot which can be helpful to you as it is Quicker an easy way of image retrieval through unique photo and video tagging. Tagging options include personalized keywords, ratings, favourites, or type of media – photo or video. Tapping on a tag provides instant results so that we can further refine our search with additional tags and keywords.

    ReplyDelete