gpt4 book ai didi

python - cv2.resize() 错误 : saves the same picture with different names

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:44 25 4
gpt4 key购买 nike

我是编程新手,正在尝试制作图像缩放器。我想让用户在文件名之前编写自定义前缀。尺寸也可定制。

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)

我希望有人有一些想法。提前致谢!

最佳答案

这是一个按比例调整大小并保存图像的示例

    def ResizeImage(filepath, newfileName,ratio):
image = cv2.imread(filepath) #Open image
height, width = image.shape[:2] #Shapes
image2 = cv2.resize(image, (width/ratio, height/ratio), interpolation = cv2.INTER_AREA)
cv2.imwrite(newfileName,image2) #saves, True if OK
return filepath

所以如果你打电话

image = "originalpath"
resized = ResizeImage(image,"Resized_"+image,2)
newImage = cv2.imread(resized)
cv2.imshow("new",newImage)
cv2.waitKey(1)

你应该把你的图片调整一半

关于python - cv2.resize() 错误 : saves the same picture with different names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42144742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com