python黑色的_python – 将RGB转换为黑色或白色
縮小到黑白
轉(zhuǎn)換為灰度,然后縮放為白色或黑色(以最接近的方式)。
原版的:
結(jié)果:
純枕實(shí)施
如果您還沒有安裝枕頭:
$ pip install pillow
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")
枕頭Numpy Bitmasks方法
你需要安裝numpy:
$ pip install numpy
Numpy需要一個(gè)數(shù)組的副本來操作,但結(jié)果是一樣的。
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")
黑白使用枕頭,抖動(dòng)
使用pillow可以將其直接轉(zhuǎn)換為黑白。它會(huì)看起來像灰色陰影,但你的大腦欺騙你! (黑色和白色附近看起來像灰色)
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')
原版的:
轉(zhuǎn)換:
黑白采用枕頭,無抖動(dòng)
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')
總結(jié)
以上是生活随笔為你收集整理的python黑色的_python – 将RGB转换为黑色或白色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Visual C++实现远程线程嵌入技
- 下一篇: C/C++中的多线程入门