python扩展图片时报错怎么解决?


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import cv2
import numpy as np
import os.path
import copy

# 椒盐噪声
def SaltAndPepper(src,percetage):
    SP_NoiseImg=src.copy()
    SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
    for i in range(SP_NoiseNum):
        randR=np.random.randint(0,src.shape[0]-1)
        randG=np.random.randint(0,src.shape[1]-1)
        randB=np.random.randint(0,3)
        if np.random.randint(0,1)==0:
            SP_NoiseImg[randR,randG,randB]=0
        else:
            SP_NoiseImg[randR,randG,randB]=255
    return SP_NoiseImg

# 高斯噪声
def addGaussianNoise(image,percetage):
    G_Noiseimg = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
    for i in range(G_NoiseNum):
        temp_x = np.random.randint(0,h)
        temp_y = np.random.randint(0,w)
        G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
    return G_Noiseimg

# 昏暗
def darker(image,percetage=0.9):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get darker
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
            image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
            image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
    return image_copy

# 亮度
def brighter(image, percetage=1.5):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get brighter
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
    return image_copy

# 旋转
def rotate(image, angle, center=None, scale=1.0):
    (h, w) = image.shape[:2]
    # If no rotation center is specified, the center of the image is set as the rotation center
    if center is None:
        center = (w / 2, h / 2)
    m = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, m, (w, h))
    return rotated

# 翻转
def flip(image):
    flipped_image = np.fliplr(image)
    return flipped_image
    
# 图片文件夹路径
file_dir = r"F:/垃圾分类/图片集/其他垃圾/其他垃圾_鹅卵石/" 
for img_name in os.listdir(file_dir):
    img_path = file_dir + img_name
    img = cv2.imread(img_path)
    # cv2.imshow("1",img)
    # cv2.waitKey(5000)
    # 旋转
    rotated_90 = rotate(img, 90)
    cv2.imwrite(file_dir + img_name[0:-4] + '_r90.jpg', rotated_90)
    rotated_180 = rotate(img, 180)
    cv2.imwrite(file_dir + img_name[0:-4] + '_r180.jpg', rotated_180)

for img_name in os.listdir(file_dir):
    img_path = file_dir + img_name
    img = cv2.imread(img_path)
    # 镜像
    flipped_img = flip(img)
    cv2.imwrite(file_dir +img_name[0:-4] + '_fli.jpg', flipped_img)

    # 增加噪声
    # img_salt = SaltAndPepper(img, 0.3)
    # cv2.imwrite(file_dir + img_name[0:7] + '_salt.jpg', img_salt)
    img_gauss = addGaussianNoise(img, 0.3)
    cv2.imwrite(file_dir + img_name[0:-4] + '_noise.jpg',img_gauss)

    #变亮、变暗
    img_darker = darker(img)
    cv2.imwrite(file_dir + img_name[0:-4] + '_darker.jpg', img_darker)
    img_brighter = brighter(img)
    cv2.imwrite(file_dir + img_name[0:-4] + '_brighter.jpg', img_brighter)

    blur = cv2.GaussianBlur(img, (7, 7), 1.5)
    #      cv2.GaussianBlur(图像,卷积核,标准差)
    cv2.imwrite(file_dir + img_name[0:-4] + '_blur.jpg',blur)

```bash
[Running] /usr/bin/python "f:\垃圾分类\图片集\图片扩展.py"
The system cannot find the path specified.

[Done] exited with code=1 in 0.02 seconds

```
The system cannot find the path specified.

我就奇怪了,你到底是windows下运行还是linux下运行,怎么前面还有f盘盘符,下面又是bash又是usr/bin的
还有你的代码怎么一看就是GPT生成的
你别一窍不通吧,建议找一个懂的人帮你

【以下回答由 GPT 生成】

根据您提供的代码片段,您在使用Python的opencv库进行图片扩展时遇到了问题。但是,根据您所提供的代码,没有明确的图片扩展的函数或代码。如果您能够提供更多关于您的问题的详细信息或提供相关的错误信息,我将能够更好地帮助您解决问题。同时,我将给出一些可能导致问题的潜在原因和解决方案。

潜在原因: 1. 缺少必要的库或模块。 2. 图片路径不正确。 3. 函数命名错误或缺失。

解决方案: 1. 检查您的代码是否有导入cv2和numpy库的语句,确保这些库已正确安装。 2. 检查图像文件的路径是否正确。可以尝试将图像文件放在与脚本文件相同的目录下,并使用相对路径来加载图像。 3. 确保您在代码中调用了正确的函数,或者您可能需要自己编写一个图片扩展的函数。 4. 如果遇到任何错误提示,可以将错误信息提供给我,以便进行进一步的分析和解决方案的提供。

总之,根据您提供的信息,无法确定您具体遇到的问题是什么。如果您能提供更多的信息,我将能够给出更加具体的解决方案。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^