My intention is to use matplotlib to convert a coloured image into a grayscale image and use colormap to display it in the Viridis scale.
The code for that is as follows:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
IMG = mpimg.imread('dog_1.jpg')
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
gray = rgb2gray(IMG)
plt.imshow(gray, cmap='viridis')
plt.show()
The output displayed is proper and as follows:
Output Image
Now, I want to save the output image in a variable as a numpy array to carry out further processing. Can I do it in any way?
plt.imread() returns a 3-dimensional numpy array with RGB layers. Your rgb2gray() function returns a 2-dimensional numpy array with a grayscale image. There is no need to extract a numpy array from the object returned by plt.imshow() when you have two numpy arrays with the image data already. However, if you insist in doing it, you can try the following:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
IMG = mpimg.imread('img.jpg')
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
gray = rgb2gray(IMG)
aximg = plt.imshow(gray, cmap='viridis')
# an array with RGBA data of the image produced by plt.imshow
arr = aximg.make_image(renderer=None, unsampled=True)[0]
To understand the Pyton Interpreter,
MY QUESTION :
If i select the first 9 line and RUN Selected, the plt codes work…
But If i RUN all the codes, plts codes doesnt work, jump to second img.show() section…
WHY?
and
How all codes work sequential…
CODE :
from PIL import Image
import numpy as np
import matplotlib.image as gg
import matplotlib.pyplot as plt
img2 = gg.imread("C:/Users/John/Desktop/BOUTTEQA/data/1.jpg")
# print(type(img2))
# print(img2.shape)
plt.imshow(img2)
plt.colorbar()
img = Image.open("C:/Users/John/Desktop/BOUTTEQA/data/1.jpg")
print(type(img))
img.show()
print(img.format)
img1 = np.asarray(img)
I'm trying to convert a grayscale image's pixels into a numpy array.
Working on google colab.
it shows an error saying: TypeError: 'numpy.uint8' object is not iterable
enter code here
##load Library
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from google.colab import files
from scipy import misc #to see image
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from PIL import Image
pil_im = Image.open('papa.png')
pil_imgray = pil_im.convert('LA')
img = np.array(list(pil_imgray.getdata(band=0)), float)
img.shape = (pil_imgray.size[1], pil_imgray.size[0])
plt.imshow(img)
for eachRow in img:
for eachPixel in eachRow:
x_test.append(sum(eachPixel)/3.0)
You can directly load the image using matplotlib:
plt.imread('papa.png')
or you can convert your PIL image with:
img = np.asarray(pil_im)
Python: How to change a "penny" image color (copper) to different gray levels? Example given in image
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()
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()
How can I convert the blue color of of the water in an image into white, especially if there are different shades of blue. And then separate any other color and give it for instance black. An example would be this pollution in the sea and you want to detect the contaminated area and give it a color clearly distinguishable from the water:
import cv2
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv2.imread('11.jpg')
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
constant=
cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
import numpy as np
from PIL import Image
import colorsys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
img = mpimg.imread('0.jpg')
gray = rgb2gray(img)
plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.show()
cv2.waitKey()
cv2.destroyAllWindows()