実現したいこと
AttributeError: 'NoneType' object has no attribute 'RasterCount'
を解決したい。AttributeError: 'NoneType' object has no attribute 'RasterCount'
の理由を知りたい
前提
JupyterNotebookにてPythonスクリプトで以下を実行しています。gdal.Open()は成功していますが、読み込まれた画像に対しての処理を実施する際に、AttributeError: 'NoneType' object has no attribute XXXXが発生しています。
いろいろと試行錯誤してみましたが解説せず、解決策を探しています。。。
以下のVersionを利用しています。
- Python 3.10.9
- GDAL 3.6.2
発生している問題・エラーメッセージ
AttributeError Traceback (most recent call last) Cell In[29], line 24 22 # IMG_1読込 23 ds_1 = gdal.Open(IMG_PATH_1_CUT, gdal.GA_ReadOnly) ---> 24 image_1 = np.array([ds_1.GetRasterBand(i + 1).ReadAsArray() for i in range(ds_1.RasterCount)]) 25 del ds_1 #不要メモリ解放 27 # IMG_2読込 AttributeError: 'NoneType' object has no attribute 'RasterCount'
該当のソースコード
python
1from osgeo import gdal 2import numpy as np 3import matplotlib.pyplot as plt 4from PIL import Image
python
1def cut4_geotiff(IMG_IN, IMG_OUT, minX, maxX, minY, maxY): 2 gdal.Translate(IMG_OUT, IMG_IN, projWin=[minX,maxY,maxX,minY]) 3 return print("Extraction process was completed! : ", IMG_IN)
python
1# ダウンロードしたファイルを指定して、切り出した上で、赤青合成を行い、PNGで表示 2if __name__ == '__main__': 3 # 処理対象ファイルの指定 4 IMG_PATH_1 = "AS200355208498-180908_webcog.tif" 5 IMG_PATH_2 = "AS201462808498-200905_webcog.tif" 6 7 # AOI抽出後(切り出し後)のGeotiffの保存先の指定 8 IMG_PATH_1_CUT = "AS200355208498-180908_webcog_cut.tif" 9 IMG_PATH_2_CUT = "AS201462808498-200905_webcog_cut.tif" 10 IMG_JPG = "out.jpeg" 11 12 # AOI(切り出し部分)の四隅緯度経度指定 13 minX = 141.93507323883878 14 minY = 42.75282111399301 15 maxX = 141.94639048725506 16 maxY = 42.75848804932892 17 18 # AOIの抽出処理(四隅): 切り出し処理 19 cut4_geotiff(IMG_PATH_1, IMG_PATH_1_CUT, minX, maxX, minY, maxY) 20 cut4_geotiff(IMG_PATH_2, IMG_PATH_2_CUT, minX, maxX, minY, maxY) 21 22 # IMG_1読込 23 ds_1 = gdal.Open(IMG_PATH_1_CUT, gdal.GA_ReadOnly) 24 image_1 = np.array([ds_1.GetRasterBand(i + 1).ReadAsArray() for i in range(ds_1.RasterCount)]) 25 del ds_1 #不要メモリ解放 26 27 # IMG_2読込 28 ds_2 = gdal.Open(IMG_PATH_2_CUT, gdal.GA_ReadOnly) 29 image_2 = np.array([ds_2.GetRasterBand(i + 1).ReadAsArray() for i in range(ds_2.RasterCount)]) 30 del ds_2 #不要メモリ解放 31 32 # 画像間のピクセル数を揃える処理(今回は必要なし) 33 x_min = min([image_1.shape[1], image_2.shape[1]]) 34 y_min = min([image_1.shape[2], image_2.shape[2]]) 35 # print("MinX : ", image_1.shape[1], image_2.shape[1]) 36 # print("MinY : ", image_1.shape[2], image_2.shape[2]) 37 38 # 出力用配列の準備 39 image = np.zeros((x_min,y_min,3)).astype(np.uint8) 40 image[:,:,0] = image_2[0, :x_min, :y_min] 41 image[:,:,1] = image_1[0, :x_min, :y_min] 42 image[:,:,2] = image_1[0, :x_min, :y_min] 43 # print(image_1.shape, image_2.shape, image.shape) 44 45 # 不要メモリの解放 46 del image_1 47 del image_2 48 49 # 出力画像の保存 50 pil_img = Image.fromarray(image) 51 # print(pil_img.mode) 52 pil_img.save(IMG_JPG) 53 54 print("DONE!")
試したこと
gdal.Open()
した画像を単純にprint()
すると、None
と表示されました。
python
1ds_1 = gdal.Open(IMG_PATH_1_CUT, gdal.GA_ReadOnly) 2print(de_1) 3 4>>結果: None
2.単純にもともとの画像をgdal.open()
した場合は、読み込んだ画像に対して正常に処理ができました。
python
1raster = gdal.Open("AS200355208498-180908_webcog.tif") 2print(raster.GetProjection()) 3 4>>結果:GEOGCS[....//詳細情報//]
補足情報(FW/ツールのバージョンなど)
cut4_geotiff
関数は成功している(エラーなし)のように思えますが、切り出された画像ファイルはNotebookのフォルダでは確認できませんでした。
こちらに記載されている内容を模倣しています。
https://sorabatake.jp/25931/
あなたの回答
tips
プレビュー