Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在引入高斯模糊后使用原始名称保存多幅图像_Python_Opencv - Fatal编程技术网

Python 在引入高斯模糊后使用原始名称保存多幅图像

Python 在引入高斯模糊后使用原始名称保存多幅图像,python,opencv,Python,Opencv,我有Python代码,它从名为Distance的文件夹中读取54幅图像,并对这些图像应用高斯模糊,然后将它们写入一个位置。代码如下: path = "Disparity/*.png" for bb,file in enumerate (glob.glob(path)): a = cv2.imread(file) blur = cv2.GaussianBlur(a, (5, 5), random.randint(3, 10)) cv2.imwrite('GaussianB

我有Python代码,它从名为Distance的文件夹中读取54幅图像,并对这些图像应用高斯模糊,然后将它们写入一个位置。代码如下:

path = "Disparity/*.png"

for bb,file in enumerate (glob.glob(path)):
    a = cv2.imread(file)
    blur = cv2.GaussianBlur(a, (5, 5), random.randint(3, 10))
    cv2.imwrite('GaussianBlur{}.jpg'.format(bb), blur)
运行这段代码后,我希望它将第一个图像读取为a.png,第二个图像读取为b.png

这两幅图像应保存为a_GaussianBlur.jpg和b_GaussianBlur.jpg


请告诉我如何获得原始名称和编辑的名称?

要仅从给定文件路径提取文件名,可以使用Python函数。这也会给你一个扩展。然后可以使用返回的仅返回基本文件名。然后可以使用该文件来构造新的输出文件名:

import os

path = "Disparity/*.png"

for index, filename in enumerate(glob.glob(path)):
    a = cv2.imread(filename)
    blur = cv2.GaussianBlur(a, (5, 5), random.randint(3, 10))
    basename = os.path.splitext(os.path.basename(filename))[0]
    cv2.imwrite(f'{basename}_Gaussian{index}.png')
因此,如果您有三个文件['abc.png','def.png','ghi.png',],您的输出文件名将是:

abc_Gussian0.png
def_Gussian1.png
ghi_Gussian2.png

我想回答我自己的问题。。。 我必须对代码进行以下更改:

最后一行代码是:-> cv2.imwrite'GaussianBlur{}.jpg'.formatbb,blur

这应该是这样的:-> cv2.imwritefile+'GaussianBlur{}.jpg'.formatbb,blur


我在这里添加了文件,因为从Distance文件夹中读取的所有文件名都在file变量中。通过添加文件,我实际上是在将原始名称添加到单个图像中

先生,我不明白您的意思,但图像的数量并不重要,我想要的是,如果我对一批图像应用高斯模糊,这些图像应使用其原始名称写入,您是否要将图像保存在同一文件夹中,但在预处理之后?这段代码运行时是否没有任何错误?是的,这段代码运行时没有任何错误我没有将它们写入同一位置目录结构类似于-文件夹“A”,其中包含一个文件夹Disparitythis包含所有图像和一个Gaussian.py文件我正在从Disparity文件夹读取并将它们写入文件夹谢谢先生,谢谢您的帮助