rgbimage.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:CAAPR 作者: Stargrazer82301 项目源码 文件源码
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.
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号