#!/usr/bin/env python
from PIL import Image
import rethinkdb as r
from optparse import OptionParser
import sys
import os
import errno
import traceback


def datafile_dir(mcdir, datafile_id):
    pieces = datafile_id.split("-")
    return os.path.join(mcdir, pieces[1][0:2], pieces[1][2:4])


def mkdirp(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise


def main():
    parser = OptionParser()
    parser.add_option("-d", "--directory", dest="dir",
                      help="MCDIR location", type="string")
    parser.add_option("-P", "--port", dest="port", type="int",
                      help="rethinkdb port")
    (options, args) = parser.parse_args()

    if options.port is None:
        print "You must specify a port for RethinkDB."
        sys.exit(1)

    if options.dir is None:
        print "You must specify the location of the MC file repository."
        sys.exit(1)

    conn = r.connect('localhost', options.port, db='materialscommons')
    all_files = list(r.table('datafiles').run(conn))
    for datafile in all_files:
        #print "File: %s" % (datafile['name'])
        name, ext = os.path.splitext(datafile['name'])
        ext_lower = ext.lower()
        if ext_lower == ".tif" or ext_lower == ".tiff":
            if 'usesid' in datafile and datafile['usesid'] != "":
                continue
            if datafile['name'][0] == '.':
                continue
            filedir = datafile_dir(options.dir, datafile['id'])
            image_file = os.path.join(filedir, datafile['id'])
            print "  Opening: %s" % (image_file)
            try:
                im = Image.open(image_file)
            except:
                traceback.print_exc()
                continue
            conversion_dir = os.path.join(filedir, ".conversion")
            converted_file_path = os.path.join(conversion_dir,
                                               datafile['id'] + ".jpg")
            if os.path.isfile(converted_file_path):
                continue
            mkdirp(conversion_dir)
            if im.mode != 'RGB':
                im = im.convert('RGB')
            print "Converting file %s, id %s" % (datafile['name'],
                                                 datafile['id'])
            im.save(converted_file_path)

if __name__ == "__main__":
    main()

@@ -478,16 +478,211 @@
 raise%0A%0A%0A
+def convert_image(f):%0A    if f%5B%22mediatype%22%5D%5B%22mime%22%5D == %22image/tiff%22:%0A        return True%0A    elif f%5B%22mediatype%22%5D%5B%22mime%22%5D == %22image/x-ms-bmp%22:%0A        return True%0A    else:%0A        return False%0A%0A%0A
 def main
@@ -1318,16 +1318,114 @@
 afiles')
+%0A                     .get_all(%22image/tiff%22, %22image/x-ms-bmp%22, index=%22mime%22)%0A                     
 .run(con
@@ -1468,16 +1468,17 @@
        #
+ 
 print %22F
@@ -1519,37 +1519,44 @@
     
-name, ext = os.path.splitext(
+filedir = datafile_dir(options.dir, 
 data
@@ -1553,36 +1553,34 @@
 .dir, datafile%5B'
-name
+id
 '%5D)%0A        ext_
@@ -1579,153 +1579,179 @@
     
-ext_lower = ext.lower()%0A        if ext_lower == %22.tif%22 or ext_lower == %22.tiff%22:%0A            if 'usesid' in datafile and datafile%5B'usesid'%5D != %22%22:
+image_file = os.path.join(filedir, datafile%5B'id'%5D)%0A        conversion_dir = os.path.join(filedir, %22.conversion%22)%0A        converted_file_path = os.path.join(conversion_dir,
 %0A   
@@ -1759,33 +1759,38 @@
             
-continue%0A
+              
             
@@ -1789,18 +1789,16 @@
         
-if
  datafil
@@ -1804,182 +1804,106 @@
 le%5B'
-name'%5D%5B0%5D == '.':%0A                continue%0A            filedir = datafile_dir(options.dir, datafile%5B'id'%5D)%0A            image_file = os.path.join(filedir, datafile%5B'id'%5D)%0A    
+id'%5D + %22.jpg%22)%0A        if 'usesid' in datafile and datafile%5B'usesid'%5D != %22%22:%0A            continue%0A
     
@@ -1951,20 +1951,16 @@
         
-    
 try:%0A   
@@ -1964,28 +1964,24 @@
             
-  
-  
 im = Image.o
@@ -1992,28 +1992,24 @@
 image_file)%0A
-    
         exce
@@ -2024,20 +2024,16 @@
         
-    
 tracebac
@@ -2050,36 +2050,32 @@
 c()%0A            
- 
-   
 continue%0A       
@@ -2071,213 +2071,9 @@
 nue%0A
-            conversion_dir = os.path.join(filedir, %22.conversion%22)%0A            converted_file_path = os.path.join(conversion_dir,%0A                                               datafile%5B'id'%5D + %22.jpg%22)%0A    
+%0A
     
@@ -2120,36 +2120,32 @@
 h):%0A            
-    
 continue%0A       
@@ -2145,20 +2145,16 @@
         
- 
-   
 mkdirp(c
@@ -2176,20 +2176,16 @@
         
-   
- 
 if im.mo
@@ -2197,20 +2197,16 @@
  'RGB':%0A
-    
         
@@ -2236,28 +2236,24 @@
 B')%0A        
- 
-   
 print %22Conve
@@ -2332,36 +2332,32 @@
                 
-    
 datafile%5B'id'%5D)%0A
@@ -2352,28 +2352,24 @@
 file%5B'id'%5D)%0A
-    
         im.s

Add support for converting BMP files.