变形 RGB 黑色 OR 白色的

我怎么拍照片 RGB 在 Python 并将其转化为黑色 OR 白色的? 我希望每个像素完全是黑色的 /0, 0, 0/, 无论是完全的白色 /255, 255, 255/.

在流行的图像处理库中有此功能是否有嵌入式功能 Python? 如果没有,如果它更靠近白色,将是刚刚通过每个像素的最佳方式,将其安装在白色,如果它更接近黑色,请将其安装在黑色?
已邀请:

八刀丁二

赞同来自:

缩放到黑色和白色

转换为灰色的阴影,然后缩放到白色或黑色 /取决于更近的事实/.

原版的:


结果:


枕头销售

安装
pillow

, 如果你还没有这样做:


$ pip install pillow


https://github.com/python-imaging/Pillow
/或者 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"/


另外,你可以使用
https://github.com/python-imaging/Pillow

http://www.numpy.org/
.

枕头 + Numpy 位面具方法

您需要安装 numpy:


$ pip install numpy


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"/


黑色和白色使用枕头,有振荡

使用
https://github.com/python-imaging/Pillow
, 您可以将其直接转换为黑色和白色。 看起来它似乎有灰色的色调,但你的大脑正在欺骗你! /彼此相邻的黑白看起来像灰色/


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'/


原版的:


转换:


黑色和白色与枕头,毫不犹豫


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'/

八刀丁二

赞同来自:

我建议将其转换为灰色的阴影,然后只需将阈值应用于它。 /一半,中等或 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/

董宝中

赞同来自:

img_rgb = cv2.imread/'image.jpg'/
img_gray = cv2.cvtColor/img_rgb, cv2.COLOR_BGR2GRAY/
/threshi, img_bw/ = cv2.threshold/img_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU/

江南孤鹜

赞同来自:

枕头,有振荡

使用
https://github.com/python-imaging/Pillow
, 您可以将其直接转换为黑色和白色。 看起来它似乎有灰色的色调,但你的大脑正在欺骗你! /彼此相邻的黑白看起来像灰色/


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'/


原版的:


转换:

二哥

赞同来自:

你可以使用
colorsys

/在标准库中/ 转换 rgb 在
http://en.wikipedia.org/wiki/HSL_and_HSV
并使用亮度值来确定 black/white:


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/

冰洋

赞同来自:

使用 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/

小姐请别说爱

赞同来自:

以下是用于创建二进制图像的代码 opencv-python :


img = cv2.imread/'in.jpg',2/

ret, bw_img = cv2.threshold/img,127,255,cv2.THRESH_BINARY/

cv2.imshow/"Output - Binary Image",bw_img/

小明明

赞同来自:

如果您不想使用方法 cv 对于细分并理解您在做什么,请考虑图像 RGB 作为矩阵。


image = mpimg.imread/'image_example.png'/ # your image
R,G,B = image[:,:,0], image[:,:,1], image[:,:,2] # the 3 RGB channels
thresh = [100, 200, 50] # example of triple threshold

# First, create an array of 0's as default value
binary_output = np.zeros_like/R/
# then screen all pixels and change the array based on RGB threshold.
binary_output[/R < thresh[0]/ & /G > thresh[1]/ & /B < thresh[2]/] = 255


结果,获得了数组 0 和 255, 基于三重条件。

要回复问题请先登录注册