Taking an image as input, how can I get the rgb matrix corresponding to it?

I checked out the numpy.asarray function. Does that give me the rgb matrix or some other matrix?

解决方案

The simplest answer is to use the NumPy and SciPy wrappers around PIL. There's a great tutorial, but the basic idea is:

from scipy import misc

arr = misc.imread('lena.png') # 640x480x3 array

arr[20, 30] # 3-vector for a pixel

arr[20, 30, 1] # green value for a pixel

For a 640x480 RGB image, this will give you a 640x480x3 array of uint8.

Or you can just open the file with PIL (or, rather, Pillow; if you're still using PIL, this may not work, or may be very slow) and pass it straight to NumPy:

import numpy as np

from PIL import Image

img = Image.open('lena.png')

arr = np.array(img) # 640x480x4 array

arr[20, 30] # 4-vector, just like above

This will give you a 640x480x4 array of type uint8 (the 4th is alpha; PIL always loads PNG files as RGBA, even if they have no transparency; see img.getbands() if you're every unsure).

If you don't want to use NumPy at all, PIL's own PixelArray type is a more limited array:

arr = img.load()

arr[20, 30] # tuple of 4 ints

This gives you a 640x480 PixelAccess array of RGBA 4-tuples.

Or you can just call getpixel on the image:

img.getpixel(20, 30) # tuple of 4 ints

python中rgb_python - 图像的RGB矩阵相关推荐

  1. Python中的图像调整和裁剪工具

    Python中的图像调整和裁剪工具 Python图像调整和裁剪工具 网站如何使那些图标随你的屏幕大小而放大或缩小?通过在不同的尺寸中拥有多个相同的图像.如果你想把你的标志以多种不同的尺寸添加到你的网站 ...

  2. 在Python中使用OpenCV将RGB格式的图像转换为HSV格式的图像

    An HSV is another type of color space in which H stands for Hue, S stands for Saturation and V stand ...

  3. Python图像处理介绍--Python中的图像表示

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 使用Python进行图像处理是理解如何通过数学和代码表示图片的有趣 ...

  4. Python中使用元组对ndarray矩阵的某个维度进行选取和调序的操作

    大家应该都知道,numpy库中的ndarray可以用所谓的三帽号规则进行切片操作(详情可见我之前写的博文,链接 https://blog.csdn.net/wenhao_ir/article/deta ...

  5. python黑色背景rbg_PIL图像转换为RGB,保存为纯黑色图像(python)

    我有图像,我想保存在jpeg格式,剪切和编辑后.在 这是我在python中的函数:import numpy as np from skimage import data, io, filter, co ...

  6. python提高照片分辨率_如何在python中提高图像的分辨率一个可能的解决方案?

    我有一个有趣的想法,通过在网格中复制每个像素9次来提高图像的分辨率.所以,我继续使用Pillow库在python中实现了它.在 这里的问题是图像没有比之前的图像更清晰或分辨率更高.它看起来仍然像先前图 ...

  7. 校验两张BMP图像的RGB矩阵有多少个像素点不同

    数据结构课设我选的的二值图像(要求是.bmp格式)数字水印,代码实现之后,我就萌生了校验一下水印信息是否编码成功的想法.经水印算法编码后的二值图像,有的被编码图与原图不存在显著的人眼上的差异.这份代码 ...

  8. python中哈达玛积与矩阵乘法的区别

    矩阵乘法是线性代数的基础运算,例如矩阵A是32,矩阵B是32 哈达玛积是两个m*n矩阵相乘 那么在python中,他们是怎样计算的呢? 当矩阵是 numpy.ndarray 时,A * B 表示哈达玛 ...

  9. python中rgb,python - 图像的RGB矩阵

    Taking an image as input, how can I get the rgb matrix corresponding to it? I checked out the numpy. ...

最新文章

  1. 作弊阴影罩棋盘,人工智能咋避嫌?
  2. tidb 企业_TiDB 在马上消费金融核心账务系统归档及跑批业务下的实践
  3. Team Foundation Server Beta3 安装指南
  4. 备份mysql数据库攻略_mysql备份实例攻略
  5. 微软出品 Kubernetes 最新学习指南 v3.0
  6. 18个不常见的C#关键字,您使用过几个?
  7. iOS 9之WatchKit for WatchOS 2
  8. 如何卸载非linux系统分区,如何卸载Linux系统分区?卸载Linux系统分区的方法-站长资讯中心...
  9. 第18章 Redis数据结构常用命令
  10. React 成为最吃香的前端技能,一纸技能图快速 Get!
  11. 如何正确获取安卓外置SD卡的路径
  12. 瑞芯微读取寄存器_瑞芯微Android软件中添加ALC5640 音频CODEC 的寄存器修改
  13. 【c++学习】int main(int argc, char** argv)命令行参数理解
  14. Unity实现换装功能
  15. 索尔维会议记录软件测试,科学史上的今天:10/30|索尔维会议创立,史上最强科学梦幻明星队...
  16. 中国 Google 面试 7 轮,结果...
  17. 【JavaScript】相关知识学习笔记
  18. 【Proteus仿真】Arduino UNO+uln2003驱动步进电机+按键启保停正反转控制
  19. 多普达 699之不完全新手须知
  20. python龙格库塔_龙格库塔积分法

热门文章

  1. 带有Python示例的math.cos()方法
  2. idea连接sqlserver及数据库操作
  3. 职业梦想是计算机的英语作文,理想职业英语作文2篇
  4. 类模板的分离式编译错误解决
  5. oracle truct,java向oracle 存储过程 传输数组
  6. 教师使用计算机职责,教师办公室计算机使用制度
  7. python解密_Python解密
  8. 机械臂中的四元素转为旋转矩阵_雅克比矩阵(上)雅克比推导
  9. mysql中数组转list,Arrays.asList(T... a) 不转换基本类型数组值为list
  10. 猎豹浏览器小号窗口怎么打开 小号窗口打开方法简述