【问题标题】:Looping through Images Using For Loop(#Tkinter)使用 For 循环(#Tkinter)循环图像
【发布时间】:2019-09-10 14:35:32
【问题描述】:

当我运行此代码时,我收到错误.. FileNotFound。我需要一些帮助才能知道我做错了什么。 (只是提高我的python技能

  def resizemultiple():

    extensions = ('.jpeg','.jpg','.png','.svg')
    x = filedialog.askdirectory()
    Newwidth = int(width.get())
    Newheight = int(height.get())
    for f in os.listdir(x):
        if f.endswith(extensions):
            #Opening the image
            res = Image.open(f)
            #Splitting the word content
            splitimage = f.split('\\')[-1]
            fn, fext = splitimage.split('.')
            #resizing the image
            #messagebox.askyesnocancel(title=x, message="Do you want yo Resize the images")
            Newimage = res.resize((int(Newwidth), int(Newheight)))
            #messagebox._show(title="Resized", message="The Image Is Resized to " + str(Newwidth) + ' x ' + str(Newheight)+ " Press Ok To save it")
            Newimage.save('()res'.format(fext))

   messagebox._show(title="Resized", message="The Images are saved")

这就是我得到的

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Emt_Inc\AppData\Local\Programs\Python\Python37- 
32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "c:/Users/Emt_Inc/Desktop/All Desktop/Random 
Coding/RatesConveter/ImageResizer.py", line 59, in resizemultiple
res = Image.open(f)
File "C:\Users\Emt_Inc\AppData\Local\Programs\Python\Python37- 
32\lib\site-packages\PIL\Image.py", line 2652, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'deo.jpg'

【问题讨论】:

  • 该错误自行解释。运行目录中没有 deo.jpg 文件。尝试更改为res = Image.open(os.join(x,f)) 我猜这就是你的意思

标签: python loops for-loop tkinter


【解决方案1】:

listdir 不返回包含整个路径的文件。 (见https://www.tutorialspoint.com/python/os_listdir.htm

您需要前面的整个路径。喜欢

pth = os.path.join(x, f)
Image.open(pth)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多