Answers for "convert array to grayscale image python"

2

Image to grayscale using python

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.144])

img = mpimg.imread('img.png')

gray = rgb2gray(img)

plt.imshow(gray, cmap='gray')

plt.savefig('greyscale.png')
plt.show()
Posted by: Guest on July-22-2021
0

convert an image to grayscale python using numpy array

1
2
3
4
5
6
7
import numpy as np
from PIL import Image

im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line
print(type(im))

gr_im= Image.fromarray(im).save('gr_kolala.png')
Posted by: Guest on December-11-2020

Code answers related to "convert array to grayscale image python"

Python Answers by Framework

Browse Popular Code Answers by Language