Changeset 4917 for trunk/imports/G2img_1TIF.py
- Timestamp:
- May 28, 2021 10:20:46 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/imports/G2img_1TIF.py
r4763 r4917 13 13 Routine to read an image in Tagged-image file (TIF) format as well as a variety 14 14 of slightly incorrect pseudo-TIF formats used at instruments around the world. 15 This uses a custom reader that attempts to determine the instrument and detector 16 parameters from various aspects of the file. 17 15 18 Note that the name ``G2img_1TIF`` is used so that this file will 16 19 sort to the top of the image formats and thus show up first in the menu. … … 31 34 '''Reads TIF files using a routine (:func:`GetTifData`) that looks 32 35 for files that can be identified from known instruments and will 33 correct for slightly incorrect TIF usage. If that routine fails, 34 it will be read with a standard TIF reader, which can handle compression 35 and other things less commonly used at beamlines. 36 correct for slightly incorrect TIF usage. 36 37 ''' 37 38 def __init__(self): … … 39 40 extensionlist=('.tif','.tiff'), 40 41 strictExtension=False, 41 formatName = ' TIF image',42 longFormatName = 'Various .tif and pseudo-TIF formats '42 formatName = 'GSAS-II known TIF image', 43 longFormatName = 'Various .tif and pseudo-TIF formats using GSAS-II reader' 43 44 ) 44 45 self.scriptable = True … … 47 48 '''Does the header match the required TIF header? 48 49 ''' 49 fp = open(filename,'rb') 50 tag = fp.read(2) 51 if 'bytes' in str(type(tag)): 52 tag = tag.decode('latin-1') 53 if tag == 'II' and int(st.unpack('<h',fp.read(2))[0]) == 42: #little endian 54 pass 55 elif tag == 'MM' and int(st.unpack('>h',fp.read(2))[0]) == 42: #big endian 56 pass 57 else: 58 return False # header not found; not valid TIF 59 fp.close() 60 fp.close() 61 return True 50 return TIFValidator(filename) 62 51 63 52 def Reader(self,filename, ParentFrame=None, **unused): 64 '''Read the TIF file using :func:`GetTifData`. If that fails, 65 use :func:`scipy.misc.imread` and give the user a chance to 66 edit the likely wrong default image parameters. 53 '''Read the TIF file using :func:`GetTifData` which attempts to 54 recognize the detector type and set various parameters 67 55 ''' 56 self.Npix = 0 68 57 self.Comments,self.Data,self.Npix,self.Image = GetTifData(filename) 69 if self.Npix == 0:70 G2fil.G2Print("GetTifData failed to read "+str(filename)+" Trying PIL")71 import PIL.Image as PI72 self.Image = PI.open(filename,mode='r')73 self.Npix = self.Image.size74 if ParentFrame:75 self.SciPy = True76 self.Comments = ['no metadata']77 self.Data = {'wavelength': 0.1, 'pixelSize': [200., 200.], 'distance': 100.0}78 self.Data['size'] = list(self.Image.size)79 self.Data['center'] = [int(i/2) for i in self.Image.size]80 58 if self.Npix == 0: 81 59 return False 82 60 self.LoadImage(ParentFrame,filename) 83 61 return True 84 62 85 63 def GetTifData(filename): 86 64 '''Read an image in a pseudo-tif format, … … 379 357 File.close() 380 358 return head,data,Npix,image 359 360 def TIFValidator(filename): 361 '''Does the header match the required TIF header? 362 ''' 363 fp = open(filename,'rb') 364 tag = fp.read(2) 365 if 'bytes' in str(type(tag)): 366 tag = tag.decode('latin-1') 367 if tag == 'II' and int(st.unpack('<h',fp.read(2))[0]) == 42: #little endian 368 pass 369 elif tag == 'MM' and int(st.unpack('>h',fp.read(2))[0]) == 42: #big endian 370 pass 371 else: 372 return False # header not found; not valid TIF 373 fp.close() 374 fp.close() 375 return True
Note: See TracChangeset
for help on using the changeset viewer.