123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import geopandas as gpd
- import matplotlib.pyplot as plt
- from shapely.wkt import loads
- from PIL import Image
- import os
- import rasterio
- import numpy as np
- import matplotlib.pyplot as plt
- def resizeImage(dir, fileName,size0,size1):
-
- original_image = Image.open(dir+"/"+fileName)
-
- resized_image = original_image.resize((size0, size1))
-
- if not os.path.exists(dir):
- os.makedirs(dir)
-
- resized_image.save(dir+"/"+fileName)
- def savePng(fileName, ewkt, dir,size0,size1):
-
- geometry = loads(ewkt.split(';')[1])
- gdf = gpd.GeoDataFrame(geometry=[geometry])
-
- fig, ax = plt.subplots(figsize=(10, 10))
-
- if geometry.geom_type == 'Polygon':
- ax.fill(*geometry.exterior.xy, color='black')
- for interior in geometry.interiors:
- ax.fill(*interior.xy, color='white')
-
- elif geometry.geom_type == 'MultiPolygon':
- for poly in geometry.geoms:
- ax.fill(*poly.exterior.xy, color='black')
- for interior in poly.interiors:
- ax.fill(*interior.xy, color='white')
-
- ax.set_xlim(gdf.total_bounds[[0, 2]])
- ax.set_ylim(gdf.total_bounds[[1, 3]])
-
- ax.axis('off')
-
- if not os.path.exists(dir):
- os.makedirs(dir)
-
- temp_file_path = os.path.join(dir, fileName + '_temp.png')
- plt.savefig(temp_file_path, dpi=300, bbox_inches='tight', pad_inches=0)
- plt.close()
-
- img = Image.open(temp_file_path).convert('L')
- gray_file_path = os.path.join(dir, fileName + '.png')
- img.save(gray_file_path)
-
- os.remove(temp_file_path)
-
- resizeImage(dir, fileName + '.png',size0,size1)
- def saveImage(tifPath, pngPath):
-
- with rasterio.open(tifPath) as src:
-
- data = src.read()
-
- meta = src.meta
-
- with rasterio.open(pngPath, 'w', **meta) as dst:
- dst.write(data)
- def getImageSize(image_path):
-
- with Image.open(image_path) as img:
- return img.size
|