Skip to content

"Too many open files" when trying to slice a very large image using crop #6671

Closed
@Marchiuzzz

Description

What did you do?

Tried slicing satellite image of 10 000 x 10 000 px (150mb) image to 100x100px images using crop.

What did you expect to happen?

Images to get sliced without errors

What actually happened?

After exactly 8187 correctly sliced and saved images I get a "Too many open files" error

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.10.5
  • Pillow: 9.1.1
from PIL import Image
from itertools import product
import os
def tile(filename, dir_in, dir_out, d):
    name, ext = os.path.splitext(filename)
    with Image.open(os.path.join(dir_in, filename)) as img:
        w, h = img.size

        grid = 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}')
            temp = img.crop(box)
            temp.save(out)
            temp.close()
        img.close()

tile('image.tif', './orig', './sliced', 100)

I cannot upload the image used because it is protected by a signed agreement that I would not share the image. Any guidance to the right direction would be much appreciated.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions