10 examples of 'rgb to grayscale python' in Python

Every line of 'rgb to grayscale python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
16def hsv_to_rgb(a, b, c):
17 return (1.0,)
56def rgb_to_gray(image):
57 return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
1239def hsv_to_rgb(hsv):
1240 """Input HSV image [0~1] return RGB image [0~255].
1241
1242 Parameters
1243 -------------
1244 hsv : numpy.array
1245 An image with values between 0.0 and 1.0
1246
1247 Returns
1248 -------
1249 numpy.array
1250 A processed image.
1251 """
1252 # Translated from source of colorsys.hsv_to_rgb
1253 # h,s should be a numpy arrays with values between 0.0 and 1.0
1254 # v should be a numpy array with values between 0.0 and 255.0
1255 # hsv_to_rgb returns an array of uints between 0 and 255.
1256 rgb = np.empty_like(hsv)
1257 rgb[..., 3:] = hsv[..., 3:]
1258 h, s, v = hsv[..., 0], hsv[..., 1], hsv[..., 2]
1259 i = (h * 6.0).astype('uint8')
1260 f = (h * 6.0) - i
1261 p = v * (1.0 - s)
1262 q = v * (1.0 - s * f)
1263 t = v * (1.0 - s * (1.0 - f))
1264 i = i % 6
1265 conditions = [s == 0.0, i == 1, i == 2, i == 3, i == 4, i == 5]
1266 rgb[..., 0] = np.select(conditions, [v, q, p, p, t, v], default=v)
1267 rgb[..., 1] = np.select(conditions, [v, v, v, q, p, p], default=t)
1268 rgb[..., 2] = np.select(conditions, [v, p, t, v, v, q], default=p)
1269 return rgb.astype('uint8')
15def colour2grayscale(R, grayscaleImgList):
16
17 # grayscaleImgList = []
18
19 images = R['data']
20
21 for i in xrange(10000):
22
23 img = images[i, ]
24
25 imRed = img[0:1024].reshape(32, 32)
26 imGreen = img[1024:2048].reshape(32, 32)
27 imBlue = img[2048:3072].reshape(32, 32)
28
29 grayscaleImg = (imRed*0.3 + imGreen*0.59 + imBlue*0.11).astype(int)
30
31 grayscaleImgList.append(grayscaleImg)
32
33 #grayscaleImgList = np.array(grayscaleImgList)
34
35 return grayscaleImgList
16def to_rgb(h,s,l):
17 return hsv_to_rgb(h,s,l)
509def convert_to_grayscale(image):
510 if image.n_channels == 3:
511 image = image.as_greyscale()
512 return image
18def grayscale(img):
19 """
20 Applies the Grayscale transform
21 This will return an image with only one color channel
22 """
23 return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
140def convert2grayscale(self):
141 ''' when importing an rgb image is necessary to calculate the
142 luminosity and reduce the array from mxnx3 to mxn
143 '''
144 self.data = np.dot(self.data[...,:3], [0.299, 0.587, 0.114])
68@staticmethod
69def hex_to_rgb(l):
70 return [(int(s[1:3], 16), int(s[3:5], 16), int(s[5:7], 16))
71 for s in l]
60def convert_img_color_to_rgb(image, color_space):
61 """ convert image colour space to RGB to xxx
62
63 :param ndarray image: rgb image
64 :param str color_space:
65 :return ndarray: image
66
67 >>> convert_img_color_to_rgb(np.ones((50, 75, 3)), 'hsv').shape
68 (50, 75, 3)
69 """
70 if image.ndim == 3 and image.shape[-1] == 3 \
71 and color_space in DICT_CONVERT_COLOR_TO_RGB:
72 image = DICT_CONVERT_COLOR_TO_RGB[color_space](image)
73 return image

Related snippets