日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python中rgb_python - 图像的RGB矩阵

發(fā)布時(shí)間:2025/3/12 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中rgb_python - 图像的RGB矩阵 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

總結(jié)

以上是生活随笔為你收集整理的python中rgb_python - 图像的RGB矩阵的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。