|
@@ -5,6 +5,7 @@ from PIL import Image
|
|
|
import os
|
|
|
import rasterio
|
|
|
import numpy as np
|
|
|
+import matplotlib.pyplot as plt
|
|
|
|
|
|
# 调整尺寸
|
|
|
|
|
@@ -39,7 +40,7 @@ def savePng(fileName, ewkt, dir):
|
|
|
ax.fill(*interior.xy, color='white') # 填充内部(洞)为白色
|
|
|
# 如果是 MultiPolygon,遍历每个 Polygon
|
|
|
elif geometry.geom_type == 'MultiPolygon':
|
|
|
- for poly in geometry.geoms: # 使用 geometry.geoms 进行迭代
|
|
|
+ for poly in geometry.geoms:
|
|
|
ax.fill(*poly.exterior.xy, color='black') # 填充外部为黑色
|
|
|
for interior in poly.interiors:
|
|
|
ax.fill(*interior.xy, color='white') # 填充内部(洞)为白色
|
|
@@ -51,16 +52,25 @@ def savePng(fileName, ewkt, dir):
|
|
|
# 去掉坐标轴
|
|
|
ax.axis('off')
|
|
|
|
|
|
- # 保存为 PNG 图片
|
|
|
+ # 检查目录是否存在,如果不存在则创建
|
|
|
if not os.path.exists(dir):
|
|
|
os.makedirs(dir)
|
|
|
|
|
|
- fileName = fileName + '.png'
|
|
|
- plt.savefig(dir+"/"+fileName, dpi=300, bbox_inches='tight', pad_inches=0)
|
|
|
+ # 保存为 PNG 图片(暂存为 RGB 模式)
|
|
|
+ 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()
|
|
|
|
|
|
+ # 使用 Pillow 将暂存的 RGB PNG 转换为灰度
|
|
|
+ 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 函数来调整图片大小
|
|
|
- resizeImage(dir, fileName)
|
|
|
+ resizeImage(dir, fileName + '.png')
|
|
|
|
|
|
|
|
|
# ewkt = "SRID=3857;POLYGON((13522050.0 3666550.0, 13522050.0 3657500.0, 13512100.0 3657500.0, 13512100.0 3666550.0, 13522050.0 3666550.0), (13516300.0 3662300.0, 13517350.0 3662300.0, 13517350.0 3663150.0, 13516300.0 3663150.0, 13516300.0 3662300.0))"
|