https://pymolwiki.org/index.php?title=Tiff2ccp4&feed=atom&action=history
Tiff2ccp4 - Revision history
2024-03-28T09:01:58Z
Revision history for this page on the wiki
MediaWiki 1.35.7
https://pymolwiki.org/index.php?title=Tiff2ccp4&diff=12428&oldid=prev
Speleo3: see also load_img_stack
2016-08-17T14:23:49Z
<p>see also load_img_stack</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 14:23, 17 August 2016</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l2" >Line 2:</td>
<td colspan="2" class="diff-lineno">Line 2:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This script will convert tiff stacks to CCP4 maps for reading in PyMOL.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This script will convert tiff stacks to CCP4 maps for reading in PyMOL.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">''See also the [[load_img_stack]] script for loading image stacks directly into PyMOL''.</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>If someone knows how to determine the file's dimensions from the file itself (not the image size, but the physical dimension size) please let me know.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>If someone knows how to determine the file's dimensions from the file itself (not the image size, but the physical dimension size) please let me know.</div></td></tr>
</table>
Speleo3
https://pymolwiki.org/index.php?title=Tiff2ccp4&diff=9451&oldid=prev
Inchoate at 16:05, 16 September 2011
2011-09-16T16:05:36Z
<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 16:05, 16 September 2011</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l452" >Line 452:</td>
<td colspan="2" class="diff-lineno">Line 452:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> s.asCCP4(args.output, args.x, args.y, args.z)</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> s.asCCP4(args.output, args.x, args.y, args.z)</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></source></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></source></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Script_Library]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:System_Scripts]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:ThirdParty_Scripts]]</ins></div></td></tr>
</table>
Inchoate
https://pymolwiki.org/index.php?title=Tiff2ccp4&diff=9450&oldid=prev
Inchoate: /* Overview */
2011-09-16T16:04:09Z
<p><span dir="auto"><span class="autocomment">Overview</span></span></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 16:04, 16 September 2011</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l4" >Line 4:</td>
<td colspan="2" class="diff-lineno">Line 4:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>If someone knows how to determine the file's dimensions from the file itself (not the image size, but the physical dimension size) please let me know.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>If someone knows how to determine the file's dimensions from the file itself (not the image size, but the physical dimension size) please let me know.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">Note: requires Python 2.7 or later (for argparse).</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">= Usage =</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"><source lang="python"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"># convert input.tiff to output.ccp4 which has cell dimensions</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"># x=40 A, y=80.5 A, Z=125.2 A</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">python tiff2ccp4.py input.tiff output.ccp4 -x 40 -y 80.5 -z 125.2</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"># print help</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">#</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">python tiff2ccp4.py -h</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></source></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>= The Code =</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>= The Code =</div></td></tr>
</table>
Inchoate
https://pymolwiki.org/index.php?title=Tiff2ccp4&diff=9449&oldid=prev
Inchoate: Created page with "= Overview = This script will convert tiff stacks to CCP4 maps for reading in PyMOL. If someone knows how to determine the file's dimensions from the file itself (not the image..."
2011-09-16T16:02:18Z
<p>Created page with "= Overview = This script will convert tiff stacks to CCP4 maps for reading in PyMOL. If someone knows how to determine the file's dimensions from the file itself (not the image..."</p>
<p><b>New page</b></p><div>= Overview =<br />
<br />
This script will convert tiff stacks to CCP4 maps for reading in PyMOL.<br />
<br />
If someone knows how to determine the file's dimensions from the file itself (not the image size, but the physical dimension size) please let me know.<br />
<br />
= The Code =<br />
<source lang="python"><br />
#<br />
# tiff2ccp4.py -- convert a TIFF stack to a CCP4 map<br />
#<br />
# Author: Jason Vertrees<br />
# Date : 2011-09-16<br />
#<br />
# Notes : To get help, type "python tiff2ccp4.py -h"<br />
#<br />
import struct<br />
import sys<br />
import string<br />
<br />
# key = field_type<br />
# value = # bytes for that field<br />
field_types = { <br />
1 : 1, <br />
2 : 1, <br />
3 : 2, <br />
4 : 4, <br />
5 : 8, <br />
6 : 1, <br />
7 : 1, <br />
8 : 2, <br />
9 : 4, <br />
10: 8, <br />
11: 4, <br />
12: 8 } <br />
<br />
field_fmt = { <br />
1 : "B", <br />
2 : "c", <br />
3 : "H", <br />
4 : "I", <br />
5 : "II", <br />
6 : "i", <br />
7 : "B", <br />
8 : "h", <br />
9 : "i", <br />
10: "ii", <br />
11: "f", <br />
12: "d" } <br />
<br />
tag_types = { <br />
"NewSubfileType" : 254, <br />
"ImageWidth" : 256, # # cols (number of pixels per scanline) <br />
"ImageLength" : 257, # # rows (number of scanlines) <br />
"BitsPerSample" : 258, <br />
"PhotometricInterpretation" : 262, <br />
"ImageDescription" : 270, <br />
"StripOffsets" : 273, <br />
"SamplesPerPixel" : 277, <br />
"RowsPerStrip" : 278, <br />
"StripByteCounts" : 279, <br />
"XResolution" : 282, <br />
"YResolution" : 283, <br />
"ZResolution" : 284, <br />
"ResolutionUnit" : 296, <br />
}<br />
<br />
<br />
class TIFFStack:<br />
<br />
def __init__(self,filename):<br />
<br />
self._filename = filename<br />
self._f = open(self._filename, 'rb')<br />
<br />
self._file_size = self.get_file_size()<br />
<br />
# read and store the file header<br />
<br />
self._header = self.get_header()<br />
<br />
# read all IFDs and store<br />
<br />
self._IFDs = self.get_IFDs()<br />
<br />
<br />
def __str__(self):<br />
s = ""<br />
s += "[TIFFStack]\n"<br />
s += "Filename : %s\n" % self._filename<br />
s += "File size : %s\n" % self._file_size<br />
s += "Header : %s\n" % self._header<br />
s += "IFDs : %s\n" % self._IFDs<br />
return s<br />
<br />
<br />
def get_file_size(self):<br />
"""<br />
file size in bytes<br />
"""<br />
if not self._f:<br />
print "Invalid: Must have open file handle."<br />
return -1<br />
<br />
# less typing<br />
<br />
f = self._f<br />
<br />
# get file size<br />
<br />
curP = f.tell()<br />
f.seek(0,2)<br />
sz = f.tell()<br />
<br />
# return to previous location<br />
<br />
f.seek(curP)<br />
<br />
return sz<br />
<br />
<br />
def get_header(self):<br />
return TIFFHeader(self._f)<br />
<br />
<br />
def get_IFDs(self):<br />
return IFD_Store(self._f, self._header)<br />
<br />
def get_data(self):<br />
<br />
f = self._f<br />
<br />
# bits per sample<br />
<br />
bps = 258<br />
<br />
# StripOffsets<br />
<br />
offset = 273<br />
<br />
# byte counts per strip<br />
<br />
byte_count = 279<br />
<br />
sample_format = 339<br />
<br />
data = []<br />
<br />
ifds = self._IFDs._store<br />
<br />
for curIFD in ifds:<br />
<br />
curOffset = curIFD[offset]._val<br />
curLength = curIFD[byte_count]._val<br />
curBPS = curIFD[bps]._val<br />
bytesPerSample = curBPS / 8.0<br />
<br />
fld = self.get_field_fmt(curIFD)<br />
<br />
# use "B" for now; support 16-bit later using tag 339 (SampleFormat)<br />
unpackStr = self._header._e_flg + fld * int(curLength/bytesPerSample)<br />
<br />
f.seek(curOffset)<br />
<br />
data.extend(struct.unpack(unpackStr, f.read(curLength)))<br />
<br />
return data<br />
<br />
def get_field_fmt(self,ifd):<br />
"""<br />
Determines the Python struct code given<br />
BitsPerSample and SampleFormat from the IFD<br />
"""<br />
bits_per_sample, sample_fmt = 258, 339<br />
<br />
bps = ifd[bits_per_sample]._val<br />
<br />
fmt = None<br />
<br />
if sample_fmt in ifd.keys():<br />
fmt = ifd[sample_fmt]._val<br />
else:<br />
fmt = 1<br />
<br />
if bps==8:<br />
# 1-byte unsigned int<br />
if fmt==1:<br />
return "B"<br />
# 1-byte signed<br />
elif fmt==2:<br />
return "b"<br />
elif bps==16:<br />
# 2-byte unsigned<br />
if fmt==1:<br />
return "H"<br />
elif fmt==2:<br />
return "h"<br />
<br />
def get_size(self):<br />
fX, fY = 256, 257<br />
x = self._IFDs._store[0][fX]._val<br />
y = self._IFDs._store[0][fY]._val<br />
z = len(self._IFDs._store)<br />
<br />
return x, y, z<br />
<br />
def get_axes_scale(self):<br />
# for now<br />
return 1,1,1<br />
<br />
<br />
def asCCP4(self,filename,dimX=-1,dimY=-1,dimZ=-1):<br />
data = self.get_data()<br />
<br />
# pixels size x,y,z<br />
<br />
nX, nY, nZ = self.get_size()<br />
<br />
# dimension scaling<br />
<br />
if -1 in (dimX,dimY,dimZ):<br />
dimX, dimY, dimZ = self.get_axes_scale()<br />
dimX *= nX<br />
dimY *= nY<br />
dimZ *= nZ<br />
<br />
m, avg, M = min(data), sum(data)/len(data), max(data)<br />
<br />
outFile = open(filename, 'wb')<br />
<br />
outFile.write(struct.pack('i',nX)) # col<br />
outFile.write(struct.pack('i',nY)) # row<br />
outFile.write(struct.pack('i',nZ)) # section<br />
outFile.write(struct.pack('i',2)) # mode = 2<br />
<br />
outFile.write(struct.pack('i',1)) # number of first col <br />
outFile.write(struct.pack('i',1)) # '' row<br />
outFile.write(struct.pack('i',1)) # '' section<br />
<br />
outFile.write(struct.pack('i',nX)) # nIntervals<br />
outFile.write(struct.pack('i',nY)) #<br />
outFile.write(struct.pack('i',nZ)) #<br />
<br />
outFile.write(struct.pack('f',dimX)) # length in X<br />
outFile.write(struct.pack('f',dimY)) # length in Y<br />
outFile.write(struct.pack('f',dimZ)) # length in Z<br />
<br />
outFile.write(struct.pack('f',90.)) # alpha <br />
outFile.write(struct.pack('f',90.)) # beta<br />
outFile.write(struct.pack('f',90.)) # gamma<br />
<br />
outFile.write(struct.pack('i',1)) # axis cols<br />
outFile.write(struct.pack('i',2)) # axis rows<br />
outFile.write(struct.pack('i',3)) # axis section<br />
<br />
outFile.write(struct.pack('f', m)) # min density<br />
outFile.write(struct.pack('f', avg)) # max density<br />
outFile.write(struct.pack('f', M)) # mean density<br />
<br />
outFile.write(struct.pack('i',0)) # space gp ?<br />
<br />
# header info; blank for us<br />
for x in range(24,257): outFile.write(struct.pack('i',0))<br />
<br />
# assume constant data in file<br />
norm = 255.<br />
bps = tag_types["BitsPerSample"]<br />
max_bits = self._IFDs._store[0][bps]._val<br />
norm = float(2**max_bits-1.)<br />
<br />
# read/write data<br />
for x in data:<br />
outFile.write(struct.pack('f', x/norm))<br />
<br />
outFile.close() <br />
<br />
<br />
class TIFFHeader:<br />
def __init__(self,fileHandle):<br />
self._endian, self._e_flg = self.get_endian(fileHandle)<br />
self._magic_number = self.get_magic_number(fileHandle)<br />
self._first_IFD = self.get_first_IFDOffset(fileHandle)<br />
<br />
def __str__(self):<br />
s = "\n"<br />
s += " [TIFF Header]\n"<br />
s += " Endian : %s\n" % self._endian<br />
s += " Endian Flag : %s\n" % self._e_flg<br />
s += " Magic Number : %s\n" % self._magic_number<br />
s += " IFD[0] Offset : %s" % self._first_IFD<br />
return s<br />
<br />
# for struct.unpackx<br />
def _1byte(self,n=1):<br />
return self._e_flg + "C"*n<br />
def _2byte(self,n=1):<br />
return self._e_flg + "H"*n<br />
def _4byte(self,n=1):<br />
return self._e_flg + "I"*n<br />
def _IFDbyte(self,n=1):<br />
return self._e_flg + "HHII"*n<br />
<br />
def get_endian(self,fileHandle):<br />
f = fileHandle<br />
f.seek(0)<br />
code = struct.unpack("cc", f.read(2))<br />
code = "".join(code)<br />
flg = ""<br />
if code=="II":<br />
flg = "<"<br />
elif code=="MM":<br />
flg = ">"<br />
else:<br />
print "This file is not a valid TIFF (bad endian tag)."<br />
flg = "?"<br />
return code,flg<br />
<br />
def get_magic_number(self,fileHandle):<br />
f = fileHandle<br />
f.seek(2)<br />
# read the magic number<br />
idx = 0<br />
if self._endian == "II": idx = 1<br />
_42 = struct.unpack(self._2byte(), f.read(2))[0]<br />
if _42!=42:<br />
print "Error: Improperly formatted TIFF file (bad magic number)."<br />
return None<br />
return _42<br />
<br />
def get_first_IFDOffset(self,fileHandle):<br />
f=fileHandle<br />
f.seek(4)<br />
off = struct.unpack(self._4byte(), f.read(4))[0]<br />
return off<br />
<br />
<br />
class IFD_Store:<br />
def __init__(self,fileHandle,fileHeader):<br />
self._f = fileHandle<br />
self._header = fileHeader<br />
self._first_offset = self._header._first_IFD<br />
# array of IFDs<br />
self._store = self.read_ifds()<br />
<br />
def __str__(self):<br />
s = "\n"<br />
s += " [IFD_Store]\n"<br />
s += " First Offset : %d\n" % self._first_offset<br />
s += " Store :\n"<br />
for st in self._store:<br />
s += "\n\n IFD Store =>\n"<br />
for k in st:<br />
s += " %s => %s" % (k,st[k])<br />
return s<br />
<br />
def read_ifds(self):<br />
<br />
f = self._f<br />
<br />
pos = self._first_offset<br />
<br />
ifds = []<br />
<br />
while pos!=0:<br />
<br />
ifds.append({})<br />
<br />
# get number of IFD_Entries<br />
<br />
f.seek(pos)<br />
<br />
# read number of IFDs<br />
num_entries = struct.unpack(self._header._2byte(), f.read(2))<br />
num_entries = num_entries[0]<br />
<br />
# read all into IFD[x]<br />
for x in range(num_entries):<br />
# pull the current record from file<br />
curParams = struct.unpack(self._header._IFDbyte(), f.read(12))<br />
<br />
# format the data if necessary<br />
if (self._header._e_flg==">" and sys.byteorder=="little") and \<br />
curParams[0] not in (270,50838,50839):<br />
scale = 32 - (8 * field_types[curParams[1]])<br />
scaledData = curParams[3] >> scale<br />
else:<br />
scaledData = curParams[3]<br />
<br />
<br />
ifds[-1][curParams[0]] = IFDEntry(curParams[0], curParams[1], curParams[2], scaledData)<br />
<br />
# read next offset<br />
pos = struct.unpack(self._header._4byte(), f.read(4))[0]<br />
<br />
return ifds<br />
<br />
class IFDEntry:<br />
def __init__(self,theTag=None,theType=None,theCount=None,theValue=None):<br />
self._tag = theTag<br />
self._type = theType<br />
self._count = theCount<br />
self._val = theValue<br />
<br />
def __str__(self):<br />
s = "\n"<br />
s += " [IFD_Entry]\n"<br />
s += " Tag : %s\n" % self._tag<br />
s += " Type : %s\n" % self._type<br />
s += " Count : %s\n" % self._count<br />
s += " Val/Off: %s\n" % self._val<br />
return s<br />
<br />
if __name__=="__main__":<br />
<br />
"""<br />
Running,<br />
<br />
python tiff2ccp4.py<br />
<br />
will convert all TIFF stacks in the current directory to <br />
CCP4 maps.<br />
"""<br />
import argparse<br />
from string import split<br />
<br />
parser = argparse.ArgumentParser(description="Convert a TIFF Stack to a CCP4 Map")<br />
parser.add_argument("input", type=str, help="input file name (usually .tif, .tiff)")<br />
parser.add_argument("output", type=str, help="output file name (usually .ccp4)")<br />
parser.add_argument('-x',"--x", help="length of x-dimension",default=-1.,type=float)<br />
parser.add_argument('-y',"--y", help="length of y-dimension",default=-1.,type=float)<br />
parser.add_argument('-z',"--z", help="length of z-dimension",default=-1.,type=float)<br />
<br />
args = parser.parse_args()<br />
<br />
s = TIFFStack(args.input)<br />
s.asCCP4(args.output, args.x, args.y, args.z)<br />
</source></div>
Inchoate