-2

Python: How to change a "penny" image color (copper) to different gray levels? Example given in image

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() 
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    Please provide more details about what you want to achieve, what you have tried. including everything in a linked image is not helpful to people trying to help you. – LmW. Sep 18 '18 at 01:20
  • plt.clf() import numpy as np import scipy.io as sio import matplotlib.pyplot as plt import matplotlib.image as mpimg p = plt.imread ('penny.jpg') plt.imshow(p) penny = p.copy() Then what should I do to change the gray levels of a regular penny color? – student Sep 18 '18 at 01:21
  • 1
    edit your post and add code in code block instead of add them in comment – Circle Hsiao Sep 18 '18 at 02:12

1 Answers1

0

The conversion to grayscale code comes from 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()
Ruzihm
  • 19,749
  • 5
  • 36
  • 48