简体   繁体   中英

Open process and save specific images in related folder

I'm looking for a way to open and crop several tiff images and then save the new croped images created in the same folder (related to my script folder).

My current code looks like this:

from PIL import Image
import os,platform

filespath = os.path.join(os.environ['USERPROFILE'],"Desktop\Python\originalImagesfolder")

for file in os.listdir(filespath):
    if file.endswith(".tif"):
        im = Image.open(file)
        im.crop((3000, 6600, 3700, 6750)).save(file+"_crop.tif")

This script is returning me the error:

Traceback (most recent call last):

File "C:\\Users...\\Desktop\\Python\\script.py", line 22, in im = Image.open(file)
File "C:\\Python34\\lib\\site-packages\\PIL\\Image.py", line 2219, in open fp = builtins.open(fp, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'Image1Name.tif'

'Image1Name.tif' is the first tif image I'm trying to process in the folder. I don't get how the script can give the file's name without being able to find it. Any Help?

PS: I have 2 days experience in python and codes generaly speaking. Sorry if the answer is obvious

[EDIT/Update] After modifying my initial code thanks to vttran and ChrisGuest answers, turning then into this:

from PIL import Image
import os,platform

filespath = os.path.join(os.environ['USERPROFILE'],"Desktop\Python\originalImagesfolder")

for file in os.listdir(filespath):
    if file.endswith(".tif"):
        filepath = os.path.join(filespath, file)
        im = Image.open(filepath)
        im.crop((3000, 6600, 3700, 6750)).save("crop"+file)

the script is returning me a new error message:

Traceback (most recent call last):
File "C:/Users/.../Desktop/Python/script.py", line 11, in im.crop((3000, 6600, 3700, 6750)).save("crop"+file)
File "C:\\Python34\\lib\\site-packages\\PIL\\Image.py", line 986, in crop self.load()
File "C:\\Python34\\lib\\site-packages\\PIL\\ImageFile.py", line 166, in load self.load_prepare()
File "C:\\Python34\\lib\\site-packages\\PIL\\ImageFile.py", line 250, in load_prepare self.im = Image.core.new(self.mode, self.size) ValueError: unrecognized mode

A maybe-useful information, it's a Landsat8 image in GeoTiff format. The TIFF file therefore include geoposition, projection... informations. The script works perfectly fine if I first open and re-save them with a software like Photoshop (16int tiff format).

When you are search for the file names you use filespath to specify the directory.

But then when you open the file, you are only using the base filename.

So you could replace

im = Image.open(file)

with

filepath = os.path.join(filespath, file)
im = Image.open(filepath)

Also consider using the glob module, as you can do glob.glob(r'path\\*.tif) . It is also good practice to avoid using builtin functions like file as variable names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM