将RGB转换为黑色或白色

时间:2021-11-01 05:52:14

How would I take an RGB image in Python and convert it to black OR white? Not grayscale, I want each pixel to be either fully black (0, 0, 0) or fully white (255, 255, 255).

如何在Python中拍摄RGB图像并将其转换为黑色或白色?不是灰度,我希望每个像素都是全黑(0,0,0)或全白(255,255,255)。

Is there any built-in functionality for this in the popular Python image processing libraries? If not, would the best way be just to loop through each pixel, if it's closer to white set it to white, if it's closer to black set it to black?

在流行的Python图像处理库中是否有任何内置功能?如果不是,那么最好的方法就是循环每个像素,如果它接近白色则将其设置为白色,如果它接近黑色则将其设置为黑色?

5 个解决方案

#1


72  

Scaling to Black and White

Convert to grayscale and then scale to white or black (whichever is closest).

转换为灰度,然后缩放为白色或黑色(以最接近的为准)。

Original:

原版的:

将RGB转换为黑色或白色

Result:

结果:

将RGB转换为黑色或白色

Pure Pillow implementation

Install pillow if you haven't already:

如果你还没有安装枕头:

$ pip install pillow

Pillow (or PIL) can help you work with images effectively.

枕头(或PIL)可以帮助您有效地处理图像。

from PIL import Image

col = Image.open("cat-tied-icon.png")
gray = col.convert('L')
bw = gray.point(lambda x: 0 if x<128 else 255, '1')
bw.save("result_bw.png")

Alternatively, you can use Pillow with numpy.

或者,你可以使用枕头和numpy。

Pillow + Numpy Bitmasks Approach

You'll need to install numpy:

你需要安装numpy:

$ pip install numpy

Numpy needs a copy of the array to operate on, but the result is the same.

Numpy需要一个数组的副本来操作,但结果是一样的。

from PIL import Image
import numpy as np

col = Image.open("cat-tied-icon.png")
gray = col.convert('L')

# Let numpy do the heavy lifting for converting pixels to pure black or white
bw = np.asarray(gray).copy()

# Pixel range is 0...255, 256/2 = 128
bw[bw < 128] = 0    # Black
bw[bw >= 128] = 255 # White

# Now we put it back in Pillow/PIL land
imfile = Image.fromarray(bw)
imfile.save("result_bw.png")

Black and White using Pillow, with dithering

Using pillow you can convert it directly to black and white. It will look like it has shades of grey but your brain is tricking you! (Black and white near each other look like grey)

使用枕头,您可以直接将其转换为黑色和白色。看起来它有灰色阴影但你的大脑却在欺骗你! (彼此附近的黑色和白色看起来像灰色)

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('/tmp/result.png')

Original:

原版的:

将RGB转换为黑色或白色

Converted:

转化:

将RGB转换为黑色或白色

Black and White using Pillow, without dithering

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open color image
image_file = image_file.convert('1', dither=Image.NONE) # convert image to black and white
image_file.save('/tmp/result.png')

#2


4  

I would suggest converting to grayscale, then simply applying a threshold (halfway, or mean or meadian, if you so choose) to it.

我建议转换为灰度,然后简单地应用一个阈值(中途,或平均或meadian,如果你这样选择)。

from PIL import Image

col = Image.open('myimage.jpg')
gry = col.convert('L')
grarray = np.asarray(gry)
bw = (grarray > grarray.mean())*255
imshow(bw)

#3


1  

Pillow, with dithering

Using pillow you can convert it directly to black and white. It will look like it has shades of grey but your brain is tricking you! (Black and white near each other look like grey)

使用枕头,您可以直接将其转换为黑色和白色。看起来它有灰色阴影但你的大脑却在欺骗你! (彼此附近的黑色和白色看起来像灰色)

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('/tmp/result.png')

Original:

原版的:

将RGB转换为黑色或白色

Converted:

转化:

将RGB转换为黑色或白色

#4


1  

And you can use colorsys (in the standard library) to convert rgb to hls and use the lightness value to determine black/white:

您可以使用colorsys(在标准库中)将rgb转换为hls并使用亮度值来确定黑/白:

import colorsys
# convert rgb values from 0-255 to %
r = 120/255.0
g = 29/255.0
b = 200/255.0
h, l, s = colorsys.rgb_to_hls(r, g, b)
if l >= .5:
    # color is lighter
    result_rgb = (255, 255, 255)
elif l < .5:
    # color is darker
    result_rgb = (0,0,0)

#5


0  

Using opencv You can easily convert rgb to binary image

使用opencv您可以轻松地将rgb转换为二进制图像

import cv2
%matplotlib inline 
import matplotlib.pyplot as plt
from skimage import io
from PIL import Image
import numpy as np

img = io.imread('http://www.bogotobogo.com/Matlab/images/MATLAB_DEMO_IMAGES/football.jpg')
img = cv2.cvtColor(img, cv2.IMREAD_COLOR)
imR=img[:,:,0] #only taking gray channel
print(img.shape)
plt.imshow(imR, cmap=plt.get_cmap('gray'))

#Gray Image
plt.imshow(imR)
plt.title('my picture')
plt.show()

#Histogram Analyze

imgg=imR
hist = cv2.calcHist([imgg],[0],None,[256],[0,256])
plt.hist(imgg.ravel(),256,[0,256])

# show the plotting graph of an image

plt.show()

#Black And White
height,width=imgg.shape
for i in range(0,height):
  for j in range(0,width):
     if(imgg[i][j]>60):
        imgg[i][j]=255
     else:
        imgg[i][j]=0

plt.imshow(imgg)

#1


72  

Scaling to Black and White

Convert to grayscale and then scale to white or black (whichever is closest).

转换为灰度,然后缩放为白色或黑色(以最接近的为准)。

Original:

原版的:

将RGB转换为黑色或白色

Result:

结果:

将RGB转换为黑色或白色

Pure Pillow implementation

Install pillow if you haven't already:

如果你还没有安装枕头:

$ pip install pillow

Pillow (or PIL) can help you work with images effectively.

枕头(或PIL)可以帮助您有效地处理图像。

from PIL import Image

col = Image.open("cat-tied-icon.png")
gray = col.convert('L')
bw = gray.point(lambda x: 0 if x<128 else 255, '1')
bw.save("result_bw.png")

Alternatively, you can use Pillow with numpy.

或者,你可以使用枕头和numpy。

Pillow + Numpy Bitmasks Approach

You'll need to install numpy:

你需要安装numpy:

$ pip install numpy

Numpy needs a copy of the array to operate on, but the result is the same.

Numpy需要一个数组的副本来操作,但结果是一样的。

from PIL import Image
import numpy as np

col = Image.open("cat-tied-icon.png")
gray = col.convert('L')

# Let numpy do the heavy lifting for converting pixels to pure black or white
bw = np.asarray(gray).copy()

# Pixel range is 0...255, 256/2 = 128
bw[bw < 128] = 0    # Black
bw[bw >= 128] = 255 # White

# Now we put it back in Pillow/PIL land
imfile = Image.fromarray(bw)
imfile.save("result_bw.png")

Black and White using Pillow, with dithering

Using pillow you can convert it directly to black and white. It will look like it has shades of grey but your brain is tricking you! (Black and white near each other look like grey)

使用枕头,您可以直接将其转换为黑色和白色。看起来它有灰色阴影但你的大脑却在欺骗你! (彼此附近的黑色和白色看起来像灰色)

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('/tmp/result.png')

Original:

原版的:

将RGB转换为黑色或白色

Converted:

转化:

将RGB转换为黑色或白色

Black and White using Pillow, without dithering

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open color image
image_file = image_file.convert('1', dither=Image.NONE) # convert image to black and white
image_file.save('/tmp/result.png')

#2


4  

I would suggest converting to grayscale, then simply applying a threshold (halfway, or mean or meadian, if you so choose) to it.

我建议转换为灰度,然后简单地应用一个阈值(中途,或平均或meadian,如果你这样选择)。

from PIL import Image

col = Image.open('myimage.jpg')
gry = col.convert('L')
grarray = np.asarray(gry)
bw = (grarray > grarray.mean())*255
imshow(bw)

#3


1  

Pillow, with dithering

Using pillow you can convert it directly to black and white. It will look like it has shades of grey but your brain is tricking you! (Black and white near each other look like grey)

使用枕头,您可以直接将其转换为黑色和白色。看起来它有灰色阴影但你的大脑却在欺骗你! (彼此附近的黑色和白色看起来像灰色)

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('/tmp/result.png')

Original:

原版的:

将RGB转换为黑色或白色

Converted:

转化:

将RGB转换为黑色或白色

#4


1  

And you can use colorsys (in the standard library) to convert rgb to hls and use the lightness value to determine black/white:

您可以使用colorsys(在标准库中)将rgb转换为hls并使用亮度值来确定黑/白:

import colorsys
# convert rgb values from 0-255 to %
r = 120/255.0
g = 29/255.0
b = 200/255.0
h, l, s = colorsys.rgb_to_hls(r, g, b)
if l >= .5:
    # color is lighter
    result_rgb = (255, 255, 255)
elif l < .5:
    # color is darker
    result_rgb = (0,0,0)

#5


0  

Using opencv You can easily convert rgb to binary image

使用opencv您可以轻松地将rgb转换为二进制图像

import cv2
%matplotlib inline 
import matplotlib.pyplot as plt
from skimage import io
from PIL import Image
import numpy as np

img = io.imread('http://www.bogotobogo.com/Matlab/images/MATLAB_DEMO_IMAGES/football.jpg')
img = cv2.cvtColor(img, cv2.IMREAD_COLOR)
imR=img[:,:,0] #only taking gray channel
print(img.shape)
plt.imshow(imR, cmap=plt.get_cmap('gray'))

#Gray Image
plt.imshow(imR)
plt.title('my picture')
plt.show()

#Histogram Analyze

imgg=imR
hist = cv2.calcHist([imgg],[0],None,[256],[0,256])
plt.hist(imgg.ravel(),256,[0,256])

# show the plotting graph of an image

plt.show()

#Black And White
height,width=imgg.shape
for i in range(0,height):
  for j in range(0,width):
     if(imgg[i][j]>60):
        imgg[i][j]=255
     else:
        imgg[i][j]=0

plt.imshow(imgg)