首先,介绍第一种方法, 使用  PIL  库,   PIL库是一种python语言常用的一个图形处理库。

关于   PIL  库的安装本文就不介绍了。

from PIL import Image
I = Image.open('C:\\Users\\Administrator\\Desktop\\照片\\timg.jpg')
I.show()
L = I.convert('L')
L.show()
L.save('C:\\Users\\Administrator\\Desktop\\照片\\timg2.jpg')

timg.jpg  文件:

灰度图:

第二种, 便是使用 numpy库  来编写自己的转化函数:

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
def rgb2gray(rgb):
r, g, b=rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
gray=0.2989*r + 0.5870*g + 0.1140*b
return gray
img=mpimg.imread('C:\\Users\\Administrator\\Desktop\\照片\\timg.jpg')
plt.imshow(img)
plt.show()
img2=rgb2gray(img)
plt.imshow(img2)
plt.show()

结果展示:

采用第二种方法展示的结果图十分的怪异,应该是matplotlib.plot 展示一个通道图片时候需要额外设置的缘故吧,下面找到了一个修正的代码,如下:

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140]) img=mpimg.imread('C:\\Users\\Administrator\\Desktop\\照片\\timg.jpg') plt.imshow(img)
plt.show() img2=rgb2gray(img) plt.imshow(img2, cmap=plt.get_cmap('gray'))
plt.show()

结果图:

发现这次的效果还是可以的。

如何使用 python3 将RGB 图片转换为 灰度图的更多相关文章

  1. 机器学习进阶-图像基本处理-视频的读取与处理 1.cv2.VideoCapture(视频的载入) 2.vc.isOpened(载入的视频是否可以打开) 3.vc.read(视频中一张图片的读取) 4.cv2.cvtColor(将图片转换为灰度图)

    1.vc = cv2.VideoCapture('test.mp4') #进行视频的载入 2.vc.isOpened() # 判断载入的视频是否可以打开 3.ret, frame = vc.read( ...

  2. 【python】RGB图片到灰度图的转换

    在做立体匹配求深度图的时候遇到这个问题,用惯了matlab的rgb2gray,倒是不熟悉python的实现,在网上找到了相关方案,记下来已作备用 RGB到灰度图转换公式: Y' = 0.299 R + ...

  3. Android 将ARGB图片转换为灰度图

    思路如下: 1.读取or照相,得到一张ARGB图片. 2.转化为bitmap类,并对其数据做如下操作: A通道保持不变,然后逐像素计算:X = 0.3×R+0.59×G+0.11×B,并使这个像素的值 ...

  4. C# (灰度)加权平均法将图片转换为灰度图

    private Bitmap ToG(string file) { using (Bitmap o = new Bitmap(file)) { Bitmap g = new Bitmap(o.Widt ...

  5. [Xcode 实际操作]六、媒体与动画-(2)使用图形上下文转换图片为灰度图

    目录:[Swift]Xcode实际操作 本文将演示如何将图片转换为灰度图. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class V ...

  6. c语言实现BMP图像转换为灰度图

    当初是自己要装X,非要用c来写信息隐藏作业,装了X,就得付出实践.查了好久资料,到期末才把作业交了,这里总结一下. 这道题是将真彩图转换为灰度图. 关于BMP文件结构,这是困扰了我好久的问题,上网查了 ...

  7. 从视频文件中读入数据-->将数据转换为灰度图-->对图像做canny边缘检测-->将这三个结构显示在一个图像中

    //从视频文件中读入数据-->将数据转换为灰度图-->对图像做canny边缘检测-->将这三个结构显示在一个图像中 //作者:sandy //时间:2015-10-10 #inclu ...

  8. Android-将RGB彩色图转换为灰度图

    package com.example.yanlei.wifi; import android.graphics.Bitmap; import android.graphics.BitmapFacto ...

  9. Qt 中彩色图像转换为灰度图

    近期在做几个图像处理相关的项目.里面有一个操作就是须要先将彩色图像转换为灰度图像. QImage 有一个convertToFormat方法.最開始一直用这个函数来实现. 可是今天细致看了看,发现这个函 ...

随机推荐

  1. python json 文件读写

    import json test_dict = {,,,} print(test_dict) print(type(test_dict))#字典 #dumps 将数据转换成字符串 json_str = ...

  2. windows服务部署

    1.新建windows服务项目 2.编辑业务代码 我这里只写2句记录文本的测试代码 using System; using System.IO; using System.ServiceProcess ...

  3. 更新自带pip

    想安装docker-compose发现居然找不到pip curl https://bootstrap.pypa.io/get-pip.py | python 直接sudo不行.还是提示权限不够. su ...

  4. Caffe 学习系列

    学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...

  5. C#中简单的文件操作实例

    using System; using System.IO; namespace Demo { class Program { static string tmpPath = @"D:/Lg ...

  6. oralce表空间使用情况查询

    SELECT UPPER(F.TABLESPACE_NAME) TABLESPACE_NAME, -- 表空间名, D.TOT_GROOTTE_MB TOT_GROOTTE_MB, -- 表空间大小( ...

  7. dns未设置 PHP Warning: file_get_contents():php_network_getaddresses: getaddrinfo failed:

    php通过去访问外部网站时,出现以下提示: PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed ...

  8. English trip -- Phonics 2 元音字母a

    xu言: 欲速则不达,如果这是你生命中最后一天.你还愿意花这么多精力继续坚持你现在做的事吗?如果答案是否定的,那么你需要改变了! What makes a word? 单词构成 Word 单词:    ...

  9. python模块——hashlib模块(简单文件摘要算法实现)

    #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" # Usage: hashlib模块 import ...

  10. TCP文件发送

    发送端(客户端) #include <iostream> #include <winsock2.h> #include <Ws2tcpip.h> #include ...