def togl_save_image(self): savefile = filedialog.asksaveasfile(parent=self.window, mode='wb', title='Save Image As PNG Image File', defaultextension='.png', filetypes=[("PNG image files", "*.png *.PNG", ""), ("All files", "")]) self.widget.redraw() if savefile: ppm_file = tempfile.mktemp() + ".ppm" PI = Tk_.PhotoImage() self.widget.tk.call(self.widget._w, 'takephoto', PI.name) PI.write(ppm_file, format='ppm') infile = open(ppm_file, 'rb') format, width, height, depth, maxval = \ png.read_pnm_header(infile, ('P5','P6','P7')) greyscale = depth <= 2 pamalpha = depth in (2, 4) supported = [2**x - 1 for x in range(1, 17)] mi = supported.index(maxval) bitdepth = mi + 1 writer = png.Writer(width, height, greyscale=greyscale, bitdepth=bitdepth, alpha=pamalpha) writer.convert_pnm(infile, savefile) savefile.close() infile.close() os.remove(ppm_file)
def read_pnm(filename): fd = open(filename,'rb') format, width, height, samples, maxval = png.read_pnm_header( fd ) pixels = np.fromfile( fd, dtype='u1' if maxval < 256 else '>u2' ) return pixels.reshape(height,width,samples)
from gaussian_2d_fit import Gaussian2DFit, Gaussian2DFitError import sys import scipy import png if __name__ == "__main__": fn = sys.argv[1] fd = open(fn, 'rb') format, width, height, samples, maxval = png.read_pnm_header(fd, supported=('P5', 'P6', 'P2') ) pixels = scipy.loadtxt(fd, dtype=scipy.uint16) img = pixels.reshape(height,width) fit = Gaussian2DFit(img) fit.save_fit_image("fig_fit.png") print(fit.get_fit_param())