我是编程新手并尝试制作图像调整器。我希望能够在文件名之前由用户编写自定义前缀。尺寸也是可定制的。
cv2.imshow() 工作正常,但 cv2.resize() 不能。如果我用 imshow 检查它,尽管有 for 循环,它只显示一张图片,然后 cv2.imwrite 只保存一张图片,其中包含所有选定图片的名称。列表似乎没问题。
我希望我清楚,代码:
def openfiles():
global picture_original
global file_path
global f
# Valid filetypes
file_types=[("Jpeg files","*.jpg"),("PNG files","*.png"),("BMP files","*.bmp"),("all files","*.*")]
window.filenames = filedialog.askopenfilenames(initialdir = "/",title = "Select file",filetypes = (file_types)) # TUPLE of filepath/filenames
#f = []
# Creating a list from the tuple
for pics in window.filenames:
f.append(pics)
f = [picture.replace('/', "\\") for picture in f]
try:
for picture in f:
picture_original = cv2.imread(picture, 1)
#cv2.imshow("Preview", picture_original)
#cv2.waitKey(1)
except:
msgbox_openerror() # Error opening the file
# Getting the filepath only from the list "f":
file_path = f[0].rsplit("\\",1)[0]
view_list()
def save_command():
"""
function to save the resized pictures
"""
global picture_resized
global prefix
prefix = "Resized_"
lst_filename = []
lst_renamed = []
for i in f:
#print(f)
path,filename = os.path.split(i) # path - only filepath | filename - only filename with extension
lst_filename.append(prefix+filename)
for i in range(len(f)):
lst_renamed.append(os.path.join(path, lst_filename[i]))
for i in range(len(f)):
picture_resized = cv2.resize(picture_original, ((int(width.get())), int(height.get())))
cv2.imshow("Preview", picture_resized)
cv2.waitKey(1)
for i in lst_renamed:
cv2.imwrite(i, picture_resized)
我希望有人有一些想法。先感谢您!