img_util.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. from shapely.wkt import loads
  4. from PIL import Image
  5. import os
  6. import rasterio
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. # 调整尺寸
  10. def resizeImage(dir, fileName,size0,size1):
  11. # 打开原始图片
  12. original_image = Image.open(dir+"/"+fileName)
  13. # 调整图片尺寸到 256x256
  14. resized_image = original_image.resize((size0, size1))
  15. # dir = dir.replace('tb', 'tb_256')
  16. if not os.path.exists(dir):
  17. os.makedirs(dir)
  18. # 保存调整后的图片
  19. resized_image.save(dir+"/"+fileName)
  20. # resizeImage('img/tb/6401812024070108270001change.png')
  21. # 保存为 PNG 图片
  22. def savePng(fileName, ewkt, dir,size0,size1):
  23. # 解析 WKT 数据字符串
  24. geometry = loads(ewkt.split(';')[1]) # 解析 WKT
  25. gdf = gpd.GeoDataFrame(geometry=[geometry])
  26. # 创建绘图
  27. fig, ax = plt.subplots(figsize=(10, 10))
  28. # 如果是 Polygon,处理其外部和内部
  29. if geometry.geom_type == 'Polygon':
  30. ax.fill(*geometry.exterior.xy, color='black') # 填充外部为黑色
  31. for interior in geometry.interiors:
  32. ax.fill(*interior.xy, color='white') # 填充内部(洞)为白色
  33. # 如果是 MultiPolygon,遍历每个 Polygon
  34. elif geometry.geom_type == 'MultiPolygon':
  35. for poly in geometry.geoms:
  36. ax.fill(*poly.exterior.xy, color='black') # 填充外部为黑色
  37. for interior in poly.interiors:
  38. ax.fill(*interior.xy, color='white') # 填充内部(洞)为白色
  39. # 设置坐标轴范围
  40. ax.set_xlim(gdf.total_bounds[[0, 2]])
  41. ax.set_ylim(gdf.total_bounds[[1, 3]])
  42. # 去掉坐标轴
  43. ax.axis('off')
  44. # 检查目录是否存在,如果不存在则创建
  45. if not os.path.exists(dir):
  46. os.makedirs(dir)
  47. # 保存为 PNG 图片(暂存为 RGB 模式)
  48. temp_file_path = os.path.join(dir, fileName + '_temp.png')
  49. plt.savefig(temp_file_path, dpi=300, bbox_inches='tight', pad_inches=0)
  50. plt.close()
  51. # 使用 Pillow 将暂存的 RGB PNG 转换为灰度
  52. img = Image.open(temp_file_path).convert('L')
  53. gray_file_path = os.path.join(dir, fileName + '.png')
  54. img.save(gray_file_path)
  55. # 删除暂存文件
  56. os.remove(temp_file_path)
  57. # 调用 resizeImage 函数来调整图片大小
  58. resizeImage(dir, fileName + '.png',size0,size1)
  59. # 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))"
  60. # savePng('111',ewkt)
  61. # tif转png
  62. def saveImage(tifPath, pngPath):
  63. # 将tifPath保存到pngPath
  64. with rasterio.open(tifPath) as src:
  65. # 读取tif文件的数据
  66. data = src.read()
  67. # 读取tif文件的元数据
  68. meta = src.meta
  69. # 将数据保存到pngPath
  70. with rasterio.open(pngPath, 'w', **meta) as dst:
  71. dst.write(data)
  72. # 获取图片的尺寸
  73. def getImageSize(image_path):
  74. # 获取图片的尺寸
  75. with Image.open(image_path) as img:
  76. return img.size