Browse Source

过滤尺寸不一样的图片

gushoubang 5 months ago
parent
commit
0aa2c4a922
3 changed files with 38 additions and 7 deletions
  1. BIN
      __pycache__/img_util.cpython-310.pyc
  2. 26 6
      img_util.py
  3. 12 1
      main.py

BIN
__pycache__/img_util.cpython-310.pyc


+ 26 - 6
img_util.py

@@ -3,15 +3,18 @@ import matplotlib.pyplot as plt
 from shapely.wkt import loads
 from PIL import Image
 import os
+import rasterio
+import numpy as np
+
 # 调整尺寸
 
 
-def resizeImage(dir,fileName):
+def resizeImage(dir, fileName):
     # 打开原始图片
     original_image = Image.open(dir+"/"+fileName)
     # 调整图片尺寸到 256x256
     resized_image = original_image.resize((256, 256))
-    dir = dir.replace('tb', 'tb_256')
+    # dir = dir.replace('tb', 'tb_256')
     if not os.path.exists(dir):
         os.makedirs(dir)
     # 保存调整后的图片
@@ -21,7 +24,7 @@ def resizeImage(dir,fileName):
 # resizeImage('img/tb/6401812024070108270001change.png')
 
 # 保存为 PNG 图片
-def savePng(fileName, ewkt,dir):
+def savePng(fileName, ewkt, dir):
     # 解析 WKT 数据字符串
     geometry = loads(ewkt.split(';')[1])  # 解析 WKT
     gdf = gpd.GeoDataFrame(geometry=[geometry])
@@ -49,7 +52,6 @@ def savePng(fileName, ewkt,dir):
     ax.axis('off')
 
     # 保存为 PNG 图片
-    dir=dir+'/tb'
     if not os.path.exists(dir):
         os.makedirs(dir)
 
@@ -58,9 +60,27 @@ def savePng(fileName, ewkt,dir):
     plt.close()
 
     # 调用 resizeImage 函数来调整图片大小
-    resizeImage(dir,fileName)
-
+    resizeImage(dir, fileName)
 
 
 # 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))"
 # savePng('111',ewkt)
+
+# tif转png
+
+def saveImage(tifPath, pngPath):
+    # 将tifPath保存到pngPath
+    with rasterio.open(tifPath) as src:
+        # 读取tif文件的数据
+        data = src.read()
+        # 读取tif文件的元数据
+        meta = src.meta
+        # 将数据保存到pngPath
+        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

+ 12 - 1
main.py

@@ -2,6 +2,8 @@
 from file_util import *
 from db_util import getDiffEwkt
 from img_util import savePng
+from img_util import saveImage
+from img_util import getImageSize
 import re
 
 # 1.获取图斑的根目录
@@ -13,6 +15,11 @@ for dir in dirs:
     for filePath in allPathList:
 
         print('tif路径:'+filePath)
+        imageSize=getImageSize(filePath)
+        if not(imageSize[0] ==256 and imageSize[1] == 256):
+            print('图片尺寸不是256:',imageSize)
+            continue
+
 
         # 3.获取tif的边界矢量范围
         ewkt = getTifEwkt(filePath)
@@ -22,4 +29,8 @@ for dir in dirs:
         tbName=dir.replace('img/','')
         ewkt = getDiffEwkt(ewkt, tbName)
         # 5.生成变化的图片
-        savePng(code+"_CHANGE", ewkt, dir)
+        savePng(code, ewkt, 'img/lable')
+        # 6.tif转png
+        saveImage(filePath,'img/A/'+code+'.tif')
+        filePath=filePath.replace('DDOM','QDOM')
+        saveImage(filePath,'img/B/'+code+'.tif')