Python:如何将“便士”图像颜色(铜色)更改为不同的灰度级?

问题描述 投票:-2回答:1

Python:如何将“便士”图像颜色(铜色)更改为不同的灰度级?图中给出的例子

penny image in different gray levels

import numpy as np 
import scipy.io as sio 
import matplotlib.pyplot as plt 
import matplotlib.image as mpimg 

plt.clf()

p = plt.imread ('penny.jpg') 
plt.imshow(p) 

penny = p.copy() 
python image
1个回答
0
投票

转换为灰度代码来自https://stackoverflow.com/a/12201744/1092820

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Import the image
p = plt.imread('penny.png')

# Convert to grayscale
gray = np.dot(p[...,:3],[0.299, 0.587, 0.114])

# Round gray to nearest 1/n, where n is how many grays:
grayCount = 4
roundedGray = np.floor(gray * float(grayCount)) / float(grayCount)

# Display using matplotlib's copper color mapping
plt.imshow(roundedGray, cmap=plt.get_cmap('copper'))

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.