def makeImage(ImageHeaderInstance,fname,clipSize,ra,dec,units='arcseconds', **kwargs): d = pyfits.open(ImageHeaderInstance.PATH)[0].data cutout = {'data':d} scale=zscale.zscale(d) if clipSize: wcs = astWCS.WCS(ImageHeaderInstance.PATH) clipSizeDeg = clipSize * constants.convert_arcmin_or_arcsec_to_degrees[units] cutout = astImages.clipImageSectionWCS(d, wcs, ra, dec, clipSizeDeg, returnWCS = False) else: clipSizeDeg = ImageHeaderInstance.TOPRIGHT_DEC-ImageHeaderInstance.BOTTOMLEFT_DEC caption = ImageHeaderInstance.FILTER ImageHeaderInstance.fname = fname try: astImages.saveBitmap( os.path.join(MEDIA_ROOT,fname), cutout['data'], cutLevels=["smart", 99.5], size=200, colorMapName='gray', caption=caption, clipSizeDeg=clipSizeDeg, scale=scale, ) except: pass #Fail silently; rendering will recover return ImageHeaderInstance
def saveBitmap(outputFileName, inputFileName, imageData, size, colorMapName, caption): """Makes a bitmap image from an image array; the image format is specified by the filename extension. (e.g. ".jpg" =JPEG, ".png"=PNG). @type outputFileName: string @param outputFileName: filename of output bitmap image @type imageData: numpy array @param imageData: image data array @type cutLevels: list @param cutLevels: sets the image scaling - available options: - pixel values: cutLevels=[low value, high value]. - histogram equalisation: cutLevels=["histEq", number of bins ( e.g. 1024)] - relative: cutLevels=["relative", cut per cent level (e.g. 99.5)] - smart: cutLevels=["smart", cut per cent level (e.g. 99.5)] ["smart", 99.5] seems to provide good scaling over a range of different images. @type size: int @param size: size of output image in pixels @type colorMapName: string @param colorMapName: name of a standard matplotlib colormap, e.g. "hot", "cool", "gray" etc. (do "help(pylab.colormaps)" in the Python interpreter to see available options) """ import time start = time.time() scale=zscale.zscale(imageData) anorm = matplotlib.colors.Normalize(scale[0],scale[1]) cut = {'image': imageData, 'norm': anorm} # Make plot aspectR=float(cut['image'].shape[0])/float(cut['image'].shape[1]) fig = pyplot.figure(figsize=(10,10*aspectR)) xPix = size yPix = size dpi = 80 xSizeInches = size/dpi ySizeInches = xSizeInches fig.set_size_inches(xSizeInches,ySizeInches) pyplot.axes([0,0,1,1]) pyplot.minorticks_off() try: colorMap=matplotlib.cm.get_cmap(colorMapName) except AssertionError: raise Exception, colorMapName+" is not a defined matplotlib colormap." pyplot.imshow(cut['image'], interpolation="bilinear", norm=cut['norm'], origin='lower', cmap=colorMap) pyplot.axis("off") xmin,xmax = fig.gca().get_xlim() ymin,ymax = fig.gca().get_ylim() pyplot.text(xmin+1,ymin+35,caption,color="red",fontsize=20,fontweight=500,backgroundcolor='white') pyplot.savefig(outputFileName,format="png",dpi=dpi) pyplot.close() return inputFileName,outputFileName