#!/usr/bin/env python # tif2spi.py : convert a tif file to a Spider file. # Works on small black and white images - don't use this on a micrograph! # Usage: tif2spi image.tif ext, where 'ext' is the spider data extension. # Uses SGI's imgcopy and tifhdr.c (and spider's CP FROM RAW) import string, sys, os from commands import getoutput if __name__ == '__main__': spider = '/net/bali/usr1/spider/bin/spider' nargs = len(sys.argv[1:]) if nargs == 0: print "Usage: tif2spi.py file.tif [spider_extension]" sys.exit(0) else: tif = sys.argv[1] if nargs > 1: ext = sys.argv[2] else: ext = "spi" tifname, tifext = os.path.splitext(tif) rawname = tifname + ".raw" cmd = "imgcopy %s %s" % (tif,rawname) err = getoutput(cmd) #if err == "" or err == None: #print err #sys.exit(0) # run tifhdr to get image dimensions tmpfile = "tmp99383" cmd = "tifhdr %s > %s" % (tif, tmpfile) os.system(cmd) f = open(tmpfile) B = f.readlines() f.close() os.remove(tmpfile) width = 0 height = 0 bits = 0 for line in B: if string.find(line, 'ImageWidth') > -1: a = string.split(line, ':') width = string.strip(a[-1]) if string.find(line, 'ImageLength') > -1: a = string.split(line, ':') height = string.strip(a[-1]) if string.find(line, 'BitsPerSample') > -1: a = string.split(line, ':') bits = string.strip(a[-1]) if bits == '8' or bits == '16' or bits == '32': pass else: print "I don't know how to process this image: %s bits/pixel" % (bits) sys.exit(0) batch = "CP FROM RAW\n" batch = batch + rawname + "\n" # raw filename batch = batch + bits + "\n" # bits per pixel batch = batch + width + "," + height + "\n" # columns & rows batch = batch + "0 \n" # header bytes to skip if bits == 16: batch = batch + "1 \n" # most significant byte batch = batch + "N \n" # don't fold negatives batch = batch + tifname + "\n" # output spider file batch = batch + "EN D \n" # f = open(tmpfile+'.bat', 'w') f.write(batch) f.close() cmd = spider + " bat/" + ext + " @" + tmpfile os.system(cmd) os.remove(tmpfile+'.bat')