因此,我的程序比较图像并删除所有相同的图像,并将原始图像重命名为已删除的图像数。该程序可以在我的Linux(ubuntu)上正常工作,但是在Windows 8笔记本电脑上使用时,我在底部出现错误,任何建议都将不胜感激。
PIL.UnidentifiedImageError: cannot identify image file 'C:\\Users\\Public\\Pictures\\Sample Pictures/desktop.ini'
(如果需要,错误代码还有很多,我也导入了os,send2trash,pil,shutil和tkinter以便与脚本一起使用)
import os
from PIL import ImageChops, ImageDraw
import PIL.Image
import shutil
from send2trash import send2trash
from tkinter import *
root = Tk()
root.title("Cupcake 3.0")
paths = Entry(root, width = 50, borderwidth = 5)
paths.grid(row = 2, column =2, columnspan =3)
path = paths.get()
def add():
end_image = PIL.Image.new(mode = "RGB",size = (200, 70), color = "red")
end_image.save(paths.get() +"/zzz999.png")
files = os.listdir(paths.get())
files.sort()
image1 =files[0]
counter = 1
im1 = " "
for file in files:
original_file_name, file_ext = (os.path.splitext(file))
if delete == 0:
ofn, fe = (os.path.splitext(image1))
if image1 == file:
pass
elif image1 != file:
im1 = PIL.Image.open(paths.get() + "/" + image1).histogram() #the image to be compared too
im2 = PIL.Image.open(paths.get() + "/" + file).histogram()#file for comparison
if im1 == im2:
send2trash(paths.get() + "/" +file)
counter = counter +1
elif im1 != im2:
os.rename(paths.get() + "/" + image1, paths.get() + "/" + ofn + "_X" +str(counter)+ fe)
counter = 1
image1 = file
else:
print("something went wrong")
else:
addd = input("Enter in the text you wish to be added/removed (can not contain blank spaces): ").strip()
new_name = "{}{}{}".format(original_file_name, addd, file_ext).strip()
os.rename(path + "/" + file, path + "/" + new_name)
print("Job Complete")
send2trash(paths.get() + "/zzz999.png")
welcome = Label(root, text = "Welcome to Cupcake 3.0")
welcome.grid(row = 0, column =2, columnspan =3)
l1 = Label (root, text = "Please enter the path to your folder: ")
l1.grid(row = 1, column =2, columnspan =3)
paths = Entry(root, width = 50, borderwidth = 5)
paths.grid(row = 2, column =2, columnspan =3)
path = paths.get()
d = IntVar()
Radiobutton(root, text = "Add", variable = d, value = 1, anchor = W).grid(row = 3, column = 2, sticky = W)
Radiobutton(root, text = "Remove", variable = d, value = 2,anchor = W).grid(row = 4, column = 2, sticky = W)
def_question = d.get()
l2 = Label (root, text = "Enter text you want removed (leave blank if n/a): ").grid(row = 6, column = 2)
rem = Entry(root, width = 50, borderwidth = 5)
rem.grid(row = 7, column =2, columnspan =3)
remo=rem.get().strip()
de = IntVar()
c = Checkbutton(root, text = "Do you wish to delete duplicate files?", variable = de)
c.grid(row = 8, column =2, sticky = W)
delete = de.get()
def run():
#print(d.get())
#print(def_question)
if d.get() == 1:
add()
elif d.get() == 2:
remove()
else:
print("I don't understand your command")
def remove():
files = os.listdir(paths.get())
files.sort()
for file in files:
original_file_name, file_ext = (os.path.splitext(file))
#print(remo)
new_name = original_file_name.replace(rem.get(), "").strip()
os.rename(paths.get() + "/" + file, paths.get() + "/" + new_name)
print("Job Complete")
submit = Button(root, text = "Run", command =lambda: run())
submit.grid(row = 9, column =2)
root.mainloop()
"
Thats my entire program
答案 0 :(得分:0)
尝试过滤掉未被PIL识别为图像的文件。这样的事情可能对您有用
def image_filter(filename):
file_path = os.path.join(paths.get(), filename)
try:
im = PIL.Image.open(file_path)
im.verify()
im.close()
return True
except: # you can handle PIL.UnidentifiedImageError here
return False
def add():
...
files = os.listdir(path)
# filter out the files which are not images
files = list(filter(image_filter, files))
files.sort()
image1 = files[0]
...