def __init__(self, source, frames=None):
# matplotlib Figure
if isinstance(source,Figure):
self.setbuf(source.canvas.buffer_rgba())
self.shape = source.canvas.get_width_height()
# numpy array
elif isinstance(source,np.ndarray) and source.ndim == 3 and source.shape[2] == 3:
self.setarr(source)
self.shape = self.darr.shape[0:2]
# standard image file
elif isinstance(source,types.StringTypes) and \
source.lower().endswith((".jpg",".jpeg",".png",".tif",".tiff")):
self.setpil(Image.open(os.path.expanduser(source)))
self.shape = self.dpil.size
# FITS file
elif isinstance(source,types.StringTypes) and source.lower().endswith(".fits"):
try: data = pyfits.getdata(arch.openbinary(os.path.expanduser(source))).T
except TypeError: raise ValueError("Something is wrong with the FITS file (the file may have been truncated)")
# the above gives us an array with shape (nx, ny) or (nx, ny, nlambda)
if data.ndim == 2:
self.setarr(np.dstack(( data,data,data )))
elif data.ndim == 3:
if frames is None:
n = data.shape[2]
frames = (n-1, n//2, 0)
self.setarr(np.dstack(( data[:,:,frames[0]],data[:,:,frames[1]],data[:,:,frames[2]] )))
else:
raise ValueError("Data in FITS file has unsupported shape")
self.shape = self.darr.shape[0:2]
# unsupported type
else:
raise ValueError("Source argument has unsupported type")
# ---------- Basic attributes -------------------------------------
## This function returns the image's shape as a tuple (nx, ny).
# You can also directly access the \c shape property.
评论列表
文章目录