学习记录1:用python—opencv批量实现图片黑白颜色互换

import os.path
import cv2

# rootdir = r'D:\用户目录\我的图片\From Yun\背景图\背景图'  # 指明被遍历的文件夹
rootdir = r'D:\yuanma\deeplabv3-plus-pytorch-2.0\VOCdevkit\VOC2007\SegmentationClass0'

for parent, dirnames, filenames in os.walk(rootdir):  # 遍历图片
    for filename in filenames:
        print('parent is :' + parent)
        print('filename is :' + filename)
        currentPath = os.path.join(parent, filename)
        print('the full name of the file is :' + currentPath)

        im=cv2.imread(currentPath)
        #彩色图像转为灰度图像(3通道变为1通道)
        gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
        #最大图像灰度值减去原图像,即可得到反转的图像
        out=255-gray
        # newname=r"D:\用户目录\我的图片\From Yun\背景图\背景图反转"+'\\'+filename+"(1).jpg"
        newname = r"D:\yuanma\deeplabv3-plus-pytorch-2.0\VOCdevkit\VOC2007\SegmentationClass" + '\\' + filename   # 重新命名
        cv2.imwrite(newname,out,) #保存结束
# im = Image.open(r'C:\Users\Administrator\Desktop\新建文件夹 (2)\1.jpg')
# out = im.transpose(Image.FLIP_LEFT_RIGHT)
# out.save(r'C:\Users\Administrator\Desktop\新建文件夹 (2)\test2.jpg')

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值