python - Open process and save specific images in related folder -


i'm looking way open , crop several tiff images , save new croped images created in same folder (related script folder).

my current code looks this:

from pil import image import os,platform  filespath = os.path.join(os.environ['userprofile'],"desktop\python\originalimagesfolder")  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 returning me 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' first tif image i'm trying process in folder. don't how script can give file's name without being able find it. help?

ps: have 2 days experience in python , codes generaly speaking. sorry if answer obvious

[edit/update] after modifying initial code vttran , chrisguest answers, turning this:

from pil import image import os,platform  filespath = os.path.join(os.environ['userprofile'],"desktop\python\originalimagesfolder")  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 returning me 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 landsat8 image in geotiff format. tiff file therefore include geoposition, projection... informations. script works fine if first open , re-save them software photoshop (16int tiff format).

when search file names use filespath specify directory.

but when open file, using base filename.

so replace

im = image.open(file) 

with

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

also consider using glob module, can glob.glob(r'path\*.tif) . practice avoid using builtin functions file variable names.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -