【发布时间】:2021-02-23 21:31:25
【问题描述】:
我在打开我拥有的一些 .tif 文件时遇到了一些问题。我尝试过使用pillow、gdal、cv2 和skimage。我更喜欢使用pillow,因为我确实在其他脚本中使用过它。我不能使用rasterio,因为当我设置批处理文件rasterio 时会抛出我尚未解决的gdal._version error。
这个打开图像错误对我来说很奇怪,因为我有另一个使用 gdal 并且打开它没有问题的分割光栅图像的代码(有一个丢失数据的问题,所以我切换到这个)。我读了几篇关于pillow 如何不支持某些数据类型的帖子,但我还没有任何解决方法。我已附加图像的属性,并将继续解决问题。
有什么问题需要我修复吗?同样,我更喜欢使用我发布的第一个代码块(使用枕头)。
枕头
import os
from PIL import Image
from itertools import product
def tile(filename, dir_in, dir_out, d):
Image.MAX_IMAGE_PIXELS = None
name, ext = os.path.splitext(filename)
img = Image.open(os.path.join(dir_in, filename))
w, h = img.size
grid = list(product(range(0, h - h % d, d), range(0, w - w % d, d)))
for i, j in grid:
box = (j, i, j + d, i + d)
out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')
img.crop(box).save(out)
tile('name.tif',
r'D:\image',
r'D:\images_split',
1000)
Traceback (most recent call last):
File "C:/Users/delete_NA/split.py", line 22, in <module>
1000)
File "C:/Users/delete_NA/split.py", line 9, in tile
img = Image.open(os.path.join(dir_in, filename))
File "C:\Users\anaconda3\envs\split\lib\site-packages\PIL\Image.py", line 2959, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file 'D:\\image\\image_to_split.tif'
GDAL
import os
import gdal
from itertools import product
def tile(filename, dir_in, dir_out, d):
Image.MAX_IMAGE_PIXELS = None
name, ext = os.path.splitext(filename)
img = gdal.Open(os.path.join(dir_in, filename))
w, h = img.size
grid = list(product(range(0, h - h % d, d), range(0, w - w % d, d)))
for i, j in grid:
box = (j, i, j + d, i + d)
out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')
img.crop(box).save(out)
tile('name.tif',
r'D:\image',
r'D:\images_split',
1000)
Traceback (most recent call last):
File "C:/Users/delete_NA/split.py", line 22, in <module>
1000)
File "C:/Users/delete_NA/split.py", line 10, in tile
w, h = img.size
File "C:\Users\anaconda3\envs\split\lib\site-packages\osgeo\gdal.py", line 2184, in <lambda>
__getattr__ = lambda self, name: _swig_getattr(self, Dataset, name)
File "C:\Users\anaconda3\envs\split\lib\site-packages\osgeo\gdal.py", line 80, in _swig_getattr
raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name))
AttributeError: 'Dataset' object has no attribute 'size'
使用 gdal 的其他拆分代码 - 这个有效
import os
from osgeo import gdal
# import variables
# Setting the directory
os.chdir(r"D:\ortho")
# Loading in the image
rimg = gdal.Open("image.tif")
# Upper left corner of the minX and maxY
gt = rimg.GetGeoTransform()
xmin = gt[0]
ymax = gt[3]
res = gt[1]
xlen = res * rimg.RasterXSize # units is important UTM or WGS
ylen = res * rimg.RasterYSize
# how many tiles you want to have in each row and column
xdiv = 150
ydiv = 150
# Determining the size of each new image
xsize = xlen/xdiv
ysize = ylen/ydiv
print(xsize)
print(ysize)
xsteps = [xmin + xsize * i for i in range(xdiv+1)] # plut because we start in the left top corner where X is at its lowest
ysteps = [ymax - ysize * i for i in range(ydiv+1)] # minus because we start in the left top corner where Y is at its highest
for i in range(xdiv):
for j in range(ydiv):
xmin = xsteps[i]
xmax = xsteps[i+1]
ymax = ysteps[j]
ymin = ysteps[j+1]
# Splices the image up into the set divs and saves them.
gdal.Warp("D:/split_images/item" + str(i)+str(j) + ".tif",rimg,
outputBounds = (xmin,ymin,xmax,ymax),dstNodata = -9999)
修改
我运行了另一个图像,它的大小只有一小部分,具有相同的属性。相同的 CRS、单位、数据类型等。我认为这不是尺寸问题,因为我通过了Image.MAX_IMAGE_PIXELS = None。不过需要注意的一点是,每个分割的图像都没有分配 CRS,这确实给我以后带来了问题。
【问题讨论】:
-
您确实看到了,
tile('name.tif', r'D:\image, r'D:\images_split, 1000)(第二个和第三个参数)中缺少'!? -
@HansHirse 是的,我现在确实看到了。一定是不小心把它删了。让我更新一下。但是我在 IDLE 中的代码确实有
'。所以这不应该是问题。
标签: python python-imaging-library tiff gdal