简体   繁体   English

打开过程并将特定图像保存在相关文件夹中

[英]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). 我正在寻找一种打开并裁剪多个tiff图像,然后将创建的新裁剪图像保存在同一文件夹(与我的脚本文件夹相关)中的方法。

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) 文件“ C:\\ Users ... \\ Desktop \\ Python \\ script.py”,第22行,位于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' 打开文件“ C:\\ Python34 \\ lib \\ site-packages \\ PIL \\ Image.py”,行2219 fp = builtins.open(fp,“ rb”)FileNotFoundError:[Errno 2]没有这样的文件或目录:' Image1Name.tif'

'Image1Name.tif' is the first tif image I'm trying to process in the folder. “ Image1Name.tif”是我尝试在文件夹中处理的第一个tif图像。 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. PS:我有2天的python和一般代码经验。 Sorry if the answer is obvious 对不起,如果答案很明显

[EDIT/Update] After modifying my initial code thanks to vttran and ChrisGuest answers, turning then into this: [编辑/更新]借助vttran和ChrisGuest的答案修改了我的初始代码后,将其转换为:

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) 在im.crop((3000,6600,3700,6750))中的文件“ C:/ Users /.../ Desktop / Python / script.py”,第11行,.save(“ crop” + file)
File "C:\\Python34\\lib\\site-packages\\PIL\\Image.py", line 986, in crop self.load() 作物self.load()中的文件“ C:\\ Python34 \\ lib \\ site-packages \\ PIL \\ Image.py”,行986
File "C:\\Python34\\lib\\site-packages\\PIL\\ImageFile.py", line 166, in load self.load_prepare() 加载self.load_prepare()中的文件“ C:\\ Python34 \\ lib \\ site-packages \\ PIL \\ ImageFile.py”,行166
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 文件“ C:\\ Python34 \\ lib \\ site-packages \\ PIL \\ ImageFile.py”,行250,处于load_prepare self.im = Image.core.new(self.mode,self.size)ValueError:无法识别的模式

A maybe-useful information, it's a Landsat8 image in GeoTiff format. 可能有用的信息,它是GeoTiff格式的Landsat8图像。 The TIFF file therefore include geoposition, projection... informations. 因此,TIFF文件包含地理,投影...信息。 The script works perfectly fine if I first open and re-save them with a software like Photoshop (16int tiff format). 如果我首先打开并使用Photoshop之类的软件(16int tiff格式)重新保存脚本,则脚本可以正常工作。

When you are search for the file names you use filespath to specify the directory. 搜索文件名时,可以使用filespath指定目录。

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) . 还可以考虑使用glob模块,就像可以做glob.glob(r'path\\*.tif) It is also good practice to avoid using builtin functions like file as variable names. 避免将诸如file类的内置函数用作变量名也是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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