python1. 读入一张彩色图片,对该图片中每个像素的RGB分量按照如下公式进行赋值,并将修改后的图片显示保存。 R’=0.393*R+0.769*G+0.189*B G’=0.349*R+0.686*G+0.168*B B’=0.272*R+0.534*G+0.131*B
时间: 2023-08-03 14:08:58 浏览: 146
你可以使用Python中的OpenCV库来读取和处理图片,然后使用matplotlib库来显示图片。下面是一份示例代码:
```python
import cv2
from matplotlib import pyplot as plt
# 读取输入图片
img = cv2.imread('input.jpg')
# 将每个像素的RGB分量按照公式进行赋值
b, g, r = cv2.split(img)
r_new = 0.393 * r + 0.769 * g + 0.189 * b
g_new = 0.349 * r + 0.686 * g + 0.168 * b
b_new = 0.272 * r + 0.534 * g + 0.131 * b
# 将修改后的像素值合并成新的图片
img_new = cv2.merge((b_new, g_new, r_new))
# 显示并保存图片
plt.imshow(cv2.cvtColor(img_new, cv2.COLOR_BGR2RGB))
plt.savefig('output.jpg')
plt.show()
```
注意:在使用matplotlib库显示图片时,需要使用cv2.cvtColor()函数将BGR格式的图片转换为RGB格式。
相关问题
python1. 读入一张彩色图片,对该图片中每个像素的RGB分量按照如下公式进行赋值,并将修改后的图片显示保存。 R’=0.393R+0.769G+0.189B G’=0.349R+0.686G+0.168B B’=0.272R+0.534G+0.131*B
这个问题已经被我回答过了,以下是代码示例:
```python
from PIL import Image
# 读入图片
img = Image.open('input.jpg')
# 获取图片宽高
width, height = img.size
# 对每个像素进行修改
for x in range(width):
for y in range(height):
r, g, b = img.getpixel((x, y))
r_new = int(0.393*r + 0.769*g + 0.189*b)
g_new = int(0.349*r + 0.686*g + 0.168*b)
b_new = int(0.272*r + 0.534*g + 0.131*b)
img.putpixel((x, y), (r_new, g_new, b_new))
# 显示并保存修改后的图片
img.show()
img.save('output.jpg')
```
其中,`input.jpg`是输入图片的文件名,`output.jpg`是输出图片的文件名。你可以将其替换成你需要处理的图片文件名。
python 读入一张彩色图片,对该图片中每个像素的RGB分量按照如下公式进行赋值,并将修改后的图片显示保存。 R’=0.393*R+0.769*G+0.189*B G’=0.349*R+0.686*G+0.168*B B’=0.272*R+0.534*G+0.131*B
好的,下面是代码实现:
```python
from PIL import Image
# 读入图片
img = Image.open('input.jpg')
# 获取图片宽高
width, height = img.size
# 对每个像素进行修改
for x in range(width):
for y in range(height):
r, g, b = img.getpixel((x, y))
r_new = int(0.393 * r + 0.769 * g + 0.189 * b)
g_new = int(0.349 * r + 0.686 * g + 0.168 * b)
b_new = int(0.272 * r + 0.534 * g + 0.131 * b)
img.putpixel((x, y), (r_new, g_new, b_new))
# 显示并保存修改后的图片
img.show()
img.save('output.jpg')
```
其中,`input.jpg`是输入图片的文件名,`output.jpg`是输出图片的文件名。你可以将其替换成你需要处理的图片文件名。
阅读全文
相关推荐











