01-GDAL image simple processing operation

1. Read, display and save

1. Obtain basic information

from osgeo import gdal
import numpy as np

dataset = gdal.Open("G:\mypython3\gdal\data\can_tmr.img")

cols  = dataset.RasterXSize
rows = dataset.RasterYSize
bands = dataset.RasterCount

2. Get band information and combine

band = dataset.GetRasterBand(3)
r = band.ReadAsArray(0,0,cols,rows)

band = dataset.GetRasterBand(2)
g = band.ReadAsArray(0,0,cols,rows)

band = dataset.GetRasterBand(1)
b = band.ReadAsArray(0,0,cols,rows)

3. Display and save the image

Combine through opencv:

import cv2
from matplotlib import pyplot as plt

img = cv2.merge([r,g,b])
plt.imshow(img)
plt.show()

Or use plt to draw:

datas =  dataset.ReadAsArray(0,0,cols,rows)
dimen = np.array(datas).shape
print(dimen)
a_datas = np.array(datas)
print(a_datas.shape)
#旋转,旋转为(400,640,6)
t_datas = a_datas.transpose((1,2,0))

#拉伸显示
from skimage   import  exposure
img2 = exposure.rescale_intensity(img2)
from PIL import Image
image = Image.fromarray(img2)

plt.imshow(image)
plt.show()
image.save('G:\mypython3\gdal\data\out.jpg')

2. Other operations

# 旋转
image = image.rotate(-90)
# 对称
image = image.transpose(Image.FLIP_LEFT_RIGHT)
# 转置
array.transpose((1,2,0))
# 调整大小
image = image.resize((cols,rows))
# 拉伸,原图像为8bit,这里为0-255显示拉伸
# 可以通过in_range, out_range来控制拉伸输入和输出的像元值
from skimage import exposure
img2 = exposure.rescale_intensity(img2)

Guess you like

Origin blog.csdn.net/suntongxue100/article/details/108448544