0

I am having trouble splitting a BigTiff. I am orginally using @Ivan code from this question. I had to modify it a bit since I am using a .tif that was larger than 4GB. Pillow does not support BigTiffs. So I ended up using tifffile. The code is running, though the image is not being clipped to the box parameter. I am thinking it has to do that tifffile reads the image in as a numpy array and is not actually being clipped by anything???

I've also noticed that when this code is running my memory is just about maxing out. I tried assigning the data type to a unassigned 8-bit and that drastically helped. Should I compress it as well? I don't want to change the data too much since I will be classifying them and do not want to lose/change data.

import os
from itertools import product
import tifffile

def tile(filename, dir_in, dir_out, d):
    name, ext = os.path.splitext(filename)
    img = tifffile.imread(os.path.join(dir_in, filename))
    print(type(img))
    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('Orthomosaic_export_MonFeb01193821460106.tif',
     r'D:\ortho',
     r'D:\model_images',
     1000)
  • [`img.clip`](https://numpy.org/doc/stable/reference/generated/numpy.clip.html) clips the values in the array. It doesn't change the shape. Use [slicing](https://numpy.org/doc/stable/reference/arrays.indexing.html) instead. – cgohlke Feb 25 '21 at 18:46
  • @cgohlke so `img=img[box]`? –  Feb 25 '21 at 18:54

1 Answers1

0

Ended up merging this answer into my code and finally got it to run.

import os
import tifffile

def tile(filename, dir_in, dir_out):
    name, ext = os.path.splitext(filename)
    img = tifffile.imread(os.path.join(dir_in, filename))

    windowsize_r = 10000
    windowsize_c = 10000

    i = 0
    for r in range(0, img.shape[0] - windowsize_r, windowsize_r):
        for c in range(0, img.shape[1] - windowsize_c, windowsize_c):
            window = img[r:r + windowsize_r, c:c + windowsize_c]
            out = os.path.join(dir_out, f'{name}_{i}_{ext}')
            tifffile.imsave(out, window)
            i = i + 1

tile('Shopfield_Orthomosaic_export_MonFeb01193821460106.tif',
     r'D:\ortho',
     r'D:\model_images')