把文件夹里,每张尺寸7200*6800的图,按每行72个、每列68个 256*256的图来抠出:
import os
from PIL import Image
import numpy as np
rootdir = r'C:\Users\admin\Desktop\U_data\11/'
savedir = r'C:\Users\admin\Desktop\U_data\22/'
list = os.listdir(rootdir)
j = 0
for i in list:
print(i)
img = Image.open(os.path.join(rootdir, i))
w = np.array(img).shape[0]
h = np.array(img).shape[1]
for x_idx in range(w // 100):
for y_idx in range(h // 100):
if x_idx * 100 + 256 < w and y_idx * 100 + 256 < h:
img1 = Image.fromarray(np.array(img)[x_idx * 100:x_idx * 100 + 256, y_idx * 100:y_idx * 100 + 256, :],mode='CMYK')
img1.save(os.path.join(savedir, "{}_{}_{}".format(str(x_idx), str(y_idx), i)))
j += 1
print(j)
原遥感图:
遥感图是4维的,如果不指定用CMYK模式的PIL读取,则截取后得到的图是这样的:
可见,颜色失真了。
而img1 = Image.fromarray(np.array(img)[x_idx * 100:x_idx * 100 + 256, y_idx * 100:y_idx * 100 + 256, :],mode='CMYK')后,就不会失真了: