【问题标题】:Python Program Doesn't Recognize InputPython 程序无法识别输入
【发布时间】:2021-04-23 21:29:25
【问题描述】:

我有一个程序应该将图像转换为具有透明边框的方形 png(如果图像为 1000x1500,则 png 为 1500x1500,然后图像将放置在 png 的中间,创建透明边框250x1500 两侧)。该程序应该从我可以选择要从中导入的文件夹中导入图像,然后将其导出到您也可以选择的另一个文件夹中。我使用递归从文件夹中抛出所有图像,但是它说输入无法识别。谁能帮我解决这个问题?在我现在发送的代码中,你有你需要解释的一切:

from PIL import Image
from os import listdir
from os.path import isfile, join,splitext

Folder_Input = 'D:\\0Pi_\Downloads\ScpServer\Originais'                         #directory where we will upload images
Folder_Output ='D:\\0Pi_\Downloads\ScpServer\PNGs'                              #directory where we will save images

typelist = ['.png','.jpg','.jpeg']
images_list = [f for f in listdir(Folder_Input) if isfile(join(Folder_Input, f)) and splitext(f)[1].lower() in typelist]

for im in images_list:
    try:
        image = Image.open(im)                                                  #this is supposed to open the image, however it doesn't. what's missing
    except:
        print(f'Problem opening image{im}!\n')
        image.close()
        continue
    width, height = image.size
    
    if height >= width:
        new_width = height
        new_height = height
    else:
        new_width = width
        new_height = width

    result = Image.new(mode = 'RGBA', size = (new_width, new_height) ,color = (0,0,0,0))   #creates a new transparent squared png
    offset = ((new_width - width) // 2, (new_height - height) // 2)             #centers the image in relation to the squared png
    
    result.paste(image,offset)                                                  #unites both the image and the squared layer
    image.close()                                                               #closes the file now finished
    
    output_file = join(Folder_Output,splitext(im)[0] + '.png')
    if not isfile(output_file):
        result.save(output_file)                                                #saves file on the output folder
    else:
        print(f"The file {splitext(im)[0] + '.png'} was not saved because it already exists on {Folder_Output}!\n")

这是 Tracebak:

Traceback (most recent call last):
  File "D:/0Pi_/Documents/add_borders.py", line 16, in <module>
    image.close()
builtins.NameError: name 'image' is not defined

【问题讨论】:

标签: python python-imaging-library


【解决方案1】:

如果try/except 块失败,它可能没有将对象分配给image 变量。

try:
    image = Image.open(im)  # this is supposed to open the image, however it doesn't. what's missing
except:
    print(f'Problem opening image{im}!\n')
    image.close() # will have problem here

因为,如果成功,您只查看Image.open(im),只需从except 中删除image.close(),或者使用另一个try/except 关闭对象。可能im 不可读或路径错误,会导致失败。

【讨论】:

  • 我已经制作了一个与这些类似的程序,我可以读取文件,甚至在此,如果我使用image.close().,我会收到消息“问题打开图像 test.png!”,它测试是我在该文件夹中的图像的名称。这意味着im 是可读的并且路径是正确的。我认为该功能甚至不应该转到except: 因为这意味着首先存在问题
  • @Pedro 重点是,仅仅从image.close()这一行被执行,就意味着image没有被赋值。现在您需要首先检查错误发生的原因。一种方法是暂时删除 except 以实际查看完整错误
  • @Pedro 提示:你已经在某个地方做了join(Folder_Input, f)。想想你在哪里需要它
  • 您甚至不需要关闭图像。当您将名称重新分配给其他对象时,该 Image 对象将没有绑定到它的引用,并且很快就会被垃圾回收。
猜你喜欢
  • 2020-06-04
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 2014-10-05
  • 2013-05-28
相关资源
最近更新 更多