首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python打开TIF失败多个库

Python打开TIF失败多个库
EN

Stack Overflow用户
提问于 2021-02-23 21:31:25
回答 1查看 125关注 0票数 1

我在打开我有的.tif文件时遇到了一些问题。我试过使用pillowgdalcv2skimage。我更喜欢使用pillow,因为我确实在其他脚本中使用它。我不能使用rasterio,因为当我设置一个批处理文件时,rasterio抛出了一个尚未解决的gdal._version 错误

这个打开的图像错误对我来说很奇怪,因为我有另一个代码来分割光栅图像(有一个丢失的数据问题,所以我切换到了这个),它使用gdal,打开它没有问题。我读过几篇关于pillow如何不支持某些数据类型的文章,但我还没有任何解决办法。我已经附上了我的图像属性,并将继续麻烦拍摄。

有什么我需要修理的东西吗?同样,我更喜欢使用我发布的第一个代码块(使用枕头)。

枕头

代码语言:javascript
运行
复制
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)
代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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)
代码语言:javascript
运行
复制
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的其他拆分代码-这一个工作

代码语言:javascript
运行
复制
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)

Tif特性图像

编辑--我运行了另一张图像,它是大小的一小部分,具有相同的属性。相同的CRS,单位,数据类型等。我不认为这将是一个大小问题,因为我通过了Image.MAX_IMAGE_PIXELS = None。但是要注意的一点是,每个分割的图像都没有分配CRS,这确实给我带来了一个问题。

EN

回答 1

Stack Overflow用户

发布于 2021-02-25 17:38:31

经过深入研究,我发现来自pillowpillow不支持BigTiffs。它还只支持特定的字节大小tifffile可用于打开BigTiffs。最后,我改变了代码中的一些内容。首先,使用tifffile而不是pillow。第二,将img.size更改为img.shape。第三,tifffile将图像打开为numpy数组,因此使用clip方法代替。虽然代码没有被剪裁,但它确实有效。这回答了我最初的问题。

代码语言:javascript
运行
复制
import os
import tifffile
from itertools import product

def tile(filename, dir_in, dir_out, d):
    name, ext = os.path.splitext(filename)
    img = tifffile.imread(os.path.join(dir_in, filename))
    w = img.shape[0]
    h = img.shape[1]

    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 = img.clip(box)
        img = img.astype('uint8')
        tifffile.imsave(out, img)

tile('name.tif',
     r'D:\image',
     r'D:\images_split',
     1000)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66341397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档