Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a loop to read several images in a python script?

I use python to work with image processing. I'm used to cut, draw and other things, but for one image. Like in the script below, how can i aply a loop in the script to do for several images?

import PIL

import Image

im=Image.open('test.tif')

box=(50, 50, 200, 200)

im_crop=im.crop(box)

im_crop.show()
like image 453
Carlos Pinto da Silva Neto Avatar asked Sep 16 '25 11:09

Carlos Pinto da Silva Neto


1 Answers

You need to wrap it in a for loop, and give that loop a list of files.

One very easy way to get a list of all the TIF files in the current directory is with glob, like this:

import PIL
import Image
import glob

for filename in glob.glob("*.tif"):
    im=Image.open(filename)
    box=(50, 50, 200, 200)
    im_crop=im.crop(box)
    im_crop.show()
like image 123
RichieHindle Avatar answered Sep 18 '25 08:09

RichieHindle