def save_img(imgs, names, filename, min_size=(0, 0)): import f2n sys.path.append('./src/lib') nb = len(imgs) assert nb == len(names) l = int(round(np.sqrt(nb))) c = int(nb / l + (nb % l != 0.)) size_max = min_size for im in imgs: x, y = im.shape size_max = ((x > size_max[0]) * x or size_max[0], (y > size_max[1]) * y or size_max[1]) im_list = [] for i in xrange(nb): im = f2n.f2nimage(imgs[i], verbose=False) im.numpyarray = rebin(im.numpyarray, size_max) #TODO: use upsample instead im.setzscale(z2='ex') im.makepilimage() im.writetitle(names[i]) im_list += [im] im_list = [[im_list[i + c * j] for i in xrange(c) if i + c * j < nb] for j in xrange(l)] for i in xrange(nb, c * l): fim = f2n.f2nimage(np.zeros(size_max), verbose=False) fim.makepilimage() im_list[-1] += [fim] f2n.compose(im_list, filename)
myimage.crop(70, 170, 60, 160) myimage.setzscale("auto", "ex") linimage = copy.deepcopy(myimage) logimage = copy.deepcopy(myimage) linimage.makepilimage("lin", negative = False) logimage.makepilimage("log", negative = False) linimage.upsample(2) linimage.writetitle("lin") linimage.drawcircle(112, 101, r=15) logimage.upsample(2) logimage.writetitle("log") mylargeimage.crop(30, 230, 60, 160) mylargeimage.setzscale(2000, "auto") # We can set manual cutoffs too. mylargeimage.rebin(4) mylargeimage.makepilimage("clin", negative = True) mylargeimage.upsample(8) #mylargeimage.drawcircle(112, 101, r=15) # Despite the rebin and upsample, coordinates are the same ! mylargeimage.writetitle("clin") mylargeimage.writeinfo(["f2n.py can rebin and upsample", "your images ! But doing both for the same", "image doesn't make sense."]) f2n.compose([[linimage, logimage], [mylargeimage]], "3_compose.png") # Instead of the tonet() method, we call the compose() function.
objcosmicskey = objkey + "_cosmics" # objkey is redefined up there... ncosmics = image[objcosmicskey] pngpath = os.path.join(pngdir, image['imgname'] + ".png") if deconvonly: f2ndec = f2n.fromfits(os.path.join(decdir, "dec" + code + ".fits"), verbose=False) f2ndec.setzscale(-20, "auto") f2ndec.makepilimage(scale="exp", negative=False) f2ndec.upsample(8) #f2ndec.writeinfo(["Deconvolution"]) decpngpath = os.path.join(pngdir, image['imgname'] + "_deconly.png") f2n.compose([[f2ndec]], pngpath) continue #else... f2ndec = f2n.fromfits(os.path.join(decdir, "dec" + code + ".fits"), verbose=False) f2ndec.setzscale(-20, "auto") f2ndec.makepilimage(scale="log", negative=False) f2ndec.upsample(2) f2ndec.writeinfo(["Deconvolution"]) f2ng = f2n.fromfits(os.path.join(decdir, "g" + code + ".fits"), verbose=False) f2ng.setzscale(-20, "auto") f2ng.makepilimage(scale="log", negative=False) f2ng.upsample(4)
def pngstampgrid(img, cat, pngfilepath, xname="x", yname="y", stampsize=100, ncols=5, upsample=4, z1="auto", z2="auto"): """ For this it uses the (slightly outdated) f2n.py module. :param img: either a galsim image or the filepath to a FITS image :param cat: an astropy table :param pngfilepath: the png file path to be written :param xname: colname for the x position (in pixel) :param yname: colname for y :param stampsize: stamp size (in pixels) to be extracted :param ncols: number of postage-stamp columns :param upsample: postage-stamp upsample rate :param z1: "z" scale low :param z2: "z" scale high """ if type(img) is str: logger.debug("Filepath given, loading the image...") img = tools.image.loadimg(img) n = len(cat) nrows = int(np.ceil(float(n) / float(ncols))) logger.info("Preparing %i x %i stamps of %i x %i pixels each..." % (ncols, nrows, stampsize, stampsize)) stamprows = [] for nrow in range(nrows): stamprow = [] for ncol in range(ncols): index = ncol + ncols * nrow if index < n: # Then we have a galaxy to show gal = cat[index] (x, y) = (gal[xname], gal[yname]) (gps, flag) = tools.image.getstamp(x, y, img, stampsize) npstamp = gps.array f2nstamp = f2n.f2nimage(numpyarray=npstamp, verbose=False) f2nstamp.setzscale(z1, z2) f2nstamp.makepilimage("log", negative=False) f2nstamp.upsample(upsample) txt = [ "%i (%i, %i)" % (gal.index, x, y), ] f2nstamp.writeinfo(txt, colour=255) # Crosshair: s = stampsize f2nstamp.drawline(s / 2, s / 4, l=30, t=np.pi / 2.0) f2nstamp.drawline(s / 4, s / 2, l=30, t=0.0) # Just for reference, some other stuff from previous MomentsML versions: #f2nstamp.drawrectangle(1, s-1, 1, s-1) #f2nstamp.drawrectangle(140, s-140, 140, s-140, colour=(0,255,255)) # Showing the measured shape, in red #e = np.hypot(galaxy.gal_e1, galaxy.gal_e2) #t = 0.5*np.arctan2(galaxy.gal_e2, galaxy.gal_e1) #f2nstamp.drawline(x = s/4, y=s/4 , l=150*e, t=t, width=3, colour=(255,0,0)) else: # No more galaxies, we just fill the splot with a grey empty stamp. npstamp = np.zeros((stampsize, stampsize)) f2nstamp = f2n.f2nimage(numpyarray=npstamp, verbose=False) f2nstamp.setzscale(-1.0, 1.0) f2nstamp.makepilimage("lin", negative=False) f2nstamp.upsample(4) stamprow.append(f2nstamp) stamprows.append(stamprow) f2n.compose(stamprows, pngfilepath) logger.info("Wrote %s" % (pngfilepath))
"%s UTC" % image['datet'], image['telescopename'] + " - " + image['setname'], "Seeing : %4.2f [arcsec]" % image['seeing'], "Ellipticity : %4.2f" % image['ell'], "Airmass : %4.2f" % image['airmass'], "Sky level : %.1f" % image['skylevel'], "Sky stddev : %.1f" % image['prealistddev'], ] skysubimage.writeinfo(skysubinfo, colour = (255, 255, 255)) skyinfo = [ "%s : %i -> %i (span = %i) [ADU]" % ("Cuts", skyimage.z1, skyimage.z2, skyimage.z2 - skyimage.z1) ] skyimage.writeinfo(skyinfo, colour = (255, 0, 0)) f2n.compose([[skysubimage, skyimage]], pngpath) if not update: orderlink = os.path.join(pngdirpath, "%05i.png" % (i+1)) # a link to get the images sorted for the movies etc. os.symlink(pngpath, orderlink) except: errmsg += "%s \n" % image["imgname"] if errmsg != '': print "\n Problem with the following images:\n" + errmsg print "Check the fits !!" if update: # remove all the symlink and redo it again with the new images allimages = db.select(imgdb, ['gogogo','treatme'], [True, True], returnType='dict', sortFields=['setname','mjd']) for i, image in enumerate(allimages):
f2nimg.makepilimage(scale="lin", negative=False) f2nimg.upsample(2) #f2nimg.writeinfo([image['imgname']], (255, 0, 0)) #f2nimg.writeinfo(["","g001.fits"]) f2nimg.writetitle("dif num %02i" % (j + 1)) difnumlist.append(f2nimg) # We fill with blanks and cut at 4 images : #difnumlist.extend([blank256, blank256, blank256]) #difnumlist = difnumlist[:4] difnumlist.append(totpsfimg) #for a in difnumlist: # print a f2n.compose([psfstarimglist, sigmaimglist, difmlist, difnumlist], pngpath) if not update: orderlink = os.path.join( pngdir, "%05i.png" % (i + 1)) # a link to get the images sorted for the movies etc. os.symlink(pngpath, orderlink) if update: # remove all the symlink and redo it again with the new images allimages = db.select(imgdb, ['gogogo', 'treatme', psfkeyflag], [True, True, True], returnType='dict', sortFields=['setname', 'mjd']) for i, image in enumerate(allimages): pngpath = os.path.join(pngdir, image['imgname'] + ".png") orderlink = os.path.join(
myimage.setzscale("auto", "ex") linimage = copy.deepcopy(myimage) logimage = copy.deepcopy(myimage) linimage.makepilimage("lin", negative=False) logimage.makepilimage("log", negative=False) linimage.upsample(2) linimage.writetitle("lin") linimage.drawcircle(112, 101, r=15) logimage.upsample(2) logimage.writetitle("log") mylargeimage.crop(30, 230, 60, 160) mylargeimage.setzscale(2000, "auto") # We can set manual cutoffs too. mylargeimage.rebin(4) mylargeimage.makepilimage("clin", negative=True) mylargeimage.upsample(8) #mylargeimage.drawcircle(112, 101, r=15) # Despite the rebin and upsample, coordinates are the same ! mylargeimage.writetitle("clin") mylargeimage.writeinfo([ "f2n.py can rebin and upsample", "your images ! But doing both for the same", "image doesn't make sense." ]) f2n.compose([[linimage, logimage], [mylargeimage]], "3_compose.png" ) # Instead of the tonet() method, we call the compose() function.
else: infolist = [image['imgname']] g.writeinfo(infolist) # And now for sig : sig = f2n.fromfits(sigpath, verbose=False) sig.setzscale("ex", "ex") sig.makepilimage(scale = "log", negative = False) sig.upsample(4) sig.writetitle("sig.fits", colour=(0)) sig.writeinfo([ncosmics], colour=(0)) pngname = image['imgname'] + ".png" pngpath = os.path.join(pngdir, pngname) f2n.compose([[g, sig]], pngpath) orderlink = os.path.join(pngdir, "%05i.png" % (i+1)) # a link to get the images sorted for the movies etc. os.symlink(pngpath, orderlink) print "- "*40 notify(computer, withsound, "I made PNGs for %s." % objkey) if makejpgarchives : makejpgtgz(pngdir, workdir, askquestions = askquestions)