def test_add_shapefile_shapes(self): grid_img = Image.open(os.path.join(os.path.dirname(__file__), 'brazil_shapefiles.png')) grid_data = np.array(grid_img) img = Image.new('RGB', (425, 425)) proj4_string = '+proj=merc +lon_0=-60 +lat_ts=-30.0 +a=6371228.0 +units=m' area_extent = (-2000000.0, -5000000.0, 5000000.0, 2000000.0) area_def = (proj4_string, area_extent) cw = ContourWriter(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l', level=4) cw.add_shapefile_shapes(img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/Metareas.shp'), outline='red') cw.add_shapefile_shape(img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/divisao_politica/BR_Regioes.shp'), 3, outline='blue') cw.add_shapefile_shape(img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/divisao_politica/BR_Regioes.shp'), 4, outline='blue', fill='green') res = np.array(img) self.failUnless(fft_metric(grid_data, res), 'Writing of Brazil shapefiles failed')
def test_add_shapefile_shapes(self): grid_img = Image.open( os.path.join(os.path.dirname(__file__), 'brazil_shapefiles.png')) grid_data = np.array(grid_img) img = Image.new('RGB', (425, 425)) proj4_string = '+proj=merc +lon_0=-60 +lat_ts=-30.0 +a=6371228.0 +units=m' area_extent = (-2000000.0, -5000000.0, 5000000.0, 2000000.0) area_def = (proj4_string, area_extent) cw = ContourWriter(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l', level=4) cw.add_shapefile_shapes(img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/Metareas.shp'), outline='red') cw.add_shapefile_shape( img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/divisao_politica/BR_Regioes.shp'), 3, outline='blue') cw.add_shapefile_shape( img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/divisao_politica/BR_Regioes.shp'), 4, outline='blue', fill='green') res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of Brazil shapefiles failed')
def embellish(basDir, GSHHS_ROOT, imgStr, ii, dateSnap, timeSnap): """ What does this definition do? Embellishes the image with custom graphics :param basDir: Base directory path :param GSHHS_ROOT: GSHHS installation folder :param imgStr: Complete path of the output image data as string :param ii: Channel name as string :param dateSnap: Date (YYYYMMDD) name as string :param timeSnap: Time (HHMM) name as string :return: Image with all the decorations.. References: [1]. https://stackoverflow.com/questions/18522295/python-pil-change-greyscale-tif-to-rgb """ import os, sys import aggdraw, PIL from PIL import Image, ImageFont from pydecorate import DecoratorAGG as dag from pycoast import ContourWriter from satpy.resample import get_area_def img = Image.open(imgStr, mode = "r") # Convert the image into RGB if it's not if ("L" or "A" in img.getbands()): rgbimg = Image.new("RGBA", img.size) rgbimg.paste(img) img = rgbimg else: print("\n It was already an RGB image, so no need to convert!") # end if-confition # Add logos dc = dag(img) # dc.add_logo(basDir + 'logo/NCMRWF.png', height = 75.0) dc.add_logo(basDir + 'logo/NCMRWF.png', height = 75.0, bg='yellow') # Add basic text information capStr = ii textStr = "MSG-1: SEVIRI [" + capStr + "]\n" + dateSnap + '\n' + timeSnap + ' UTC' # fontClr = aggdraw.Font((255, 255, 255), "/usr/share/fonts/truetype/DejaVuSerif.ttf", size = 18) fontClr = aggdraw.Font('blue', "/usr/share/fonts/truetype/DejaVuSerif.ttf", size = 18) dc.add_text(textStr, font = fontClr) # Adding partner's logos dc.align_right() dc.add_logo(basDir + 'logo/imdlogo.jpg', height=75.0) dc.align_left() dc.align_bottom() dc.add_logo(basDir + 'logo/eumetsatLogo.jpg', height=50.0) dc.align_right() dc.add_logo(basDir + 'logo/pytroll-logo.png', height = 50.0) # Add coastline, grid to the image # Set up projection info for coastlines proj4_str = '+proj=merc +lon_0=75.0 + lat_0=17.5 + lat_ts=17.5 +ellps=WGS84' area_extent = (-3339584.72, -1111475.10, 3339584.72, 5591295.92) area_def = (proj4_str, area_extent) # Add the shape file directory or the GSHHS root path cw = ContourWriter(GSHHS_ROOT) cw.add_coastlines(img, area_def, resolution = 'h', level = 1) cw.add_shapefile_shapes(img, area_def, GSHHS_ROOT + 'India/Admin2.shp', outline = 'white') # Add gridlines fontClr2 = ImageFont.truetype("/usr/share/fonts/truetype/DejaVuSerif.ttf", 14) cw.add_grid(img, area_def, (10.0, 10.0), (2.0, 2.0), fontClr2, fill = "white", outline = 'lightblue', minor_outline = 'lightblue') # Save the image img.save(imgStr) # return the image object return img