def saveto(self, filepath, tiff16bit=False):
filepath = os.path.expanduser(filepath)
lowfile = filepath.lower()
# 16-bit TIFF file
if tiff16bit and lowfile.endswith((".tif",".tiff")):
# tiff header as uint16 words
lsNX,msNX = split16(self.shape[0])
lsNY,msNY = split16(self.shape[1])
lsBYTES,msBYTES = split16(self.shape[0]*self.shape[1]*6)
header = ( \
0x4949, 42, 8, 0, # 0: TIFF header (little endian)
12, # 8: number of directory entries
# (directory entry: tag,type,count,0,value/offset x 2)
256, 4, 1, 0, lsNX, msNX, # 10: ImageWidth, 1 LONG
257, 4, 1, 0, lsNY, msNY, # 22: ImageLength, 1 LONG
258, 3, 3, 0, 158, 0, # 34: BitsPerSample, 3 SHORT (-> offset!)
259, 3, 1, 0, 1, 0, # 46: Compression, 1 SHORT
262, 3, 1, 0, 2, 0, # 58: PhotometricInterpretation, 1 SHORT
273, 4, 1, 0, 180, 0, # 70: StripOffsets, 1 LONG
277, 3, 1, 0, 3, 0, # 82: SamplesPerPixel, 1 SHORT
278, 4, 1, 0, lsNY, msNY, # 94: RowsPerStrip, 1 LONG
279, 4, 1, 0, lsBYTES, msBYTES, # 106: StripByteCounts, 1 LONG
282, 5, 1, 0, 164, 0, # 118: XResolution, 1 RATIONAL (-> offset!)
283, 5, 1, 0, 172, 0, # 130: YResolution, 1 RATIONAL (-> offset!)
296, 3, 1, 0, 2, 0, # 142: ResolutionUnit, 1 SHORT
0, 0, # 154: IFD list terminator
16, 16, 16, # 158: BitsPerSample value
72, 0, 1, 0, # 164: XResolution value
72, 0, 1, 0 ) # 172: YResolution value
# 180: Image data
out = open(filepath, 'w')
out.write(np.array(header,dtype=np.uint16).tostring())
data = self.scaledpixelarray(0,65535.99)
out.write(np.flipud(np.rollaxis(data,1)).astype(np.uint16).tostring())
out.close()
# standard 8-bit image file
elif lowfile.endswith((".bmp",".gif",".jpg",".jpeg",".png",".tif",".tiff",".pdf")):
self.ensurepil(invalidate=False)
if lowfile.endswith((".jpg",".jpeg")): self.dpil.save(filepath, "JPEG", quality=80)
elif lowfile.endswith((".png")): self.dpil.save(filepath, "PNG")
elif lowfile.endswith((".tif",".tiff")): self.dpil.save(filepath, "TIFF")
elif lowfile.endswith((".pdf")): self.dpil.save(filepath, "PDF")
# FITS file
elif lowfile.endswith(".fits"):
self.ensurearr(invalidate=False)
data = np.dstack(( self.darr[:,:,2],self.darr[:,:,1],self.darr[:,:,0] ))
if os.path.exists(filepath): os.remove(filepath) # avoid message produced by fits library
pyfits.writeto(filepath, data.T, clobber=True)
# unsupported type
else:
raise ValueError("Filepath argument has unsupported filename extension")
## This function plots the image to the current axes in the current matplotlib figure. The image is
# resampled to fit the axes. If \em fill is True, the image is stretched to fit the axes in both directions
# changing the image aspect ratio if needed. If \em fill is False (the default), the axes aspect ratio
# is adjusted so that the image aspect ratio is preserved.
评论列表
文章目录