日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python黑色的_python – 将RGB转换为黑色或白色

發布時間:2025/3/15 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python黑色的_python – 将RGB转换为黑色或白色 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

縮小到黑白

轉換為灰度,然后縮放為白色或黑色(以最接近的方式)。

原版的:

結果:

純枕實施

如果您還沒有安裝枕頭:

$ 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需要一個數組的副本來操作,但結果是一樣的。

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

黑白使用枕頭,抖動

使用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')

總結

以上是生活随笔為你收集整理的python黑色的_python – 将RGB转换为黑色或白色的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。