发布时间:2019-09-18 07:23:02编辑:auto阅读(1551)
原图:
转换成:
python代码
# -*- coding: utf-8 -*-
import os
from PIL import *
curdir="E:\\py\\WinPython-32bit-2.7.6.4\\study"
os.chdir(curdir)
def RGB2BlackWhite(filename):
im=Image.open(filename)
print "image info,",im.format,im.mode,im.size
(w,h)=im.size
R=0
G=0
B=0
for x in xrange(w):
for y in xrange(h):
pos=(x,y)
rgb=im.getpixel( pos )
(r,g,b)=rgb
R=R+r
G=G+g
B=B+b
rate1=R*1000/(R+G+B)
rate2=G*1000/(R+G+B)
rate3=B*1000/(R+G+B)
print "rate:",rate1,rate2,rate3
for x in xrange(w):
for y in xrange(h):
pos=(x,y)
rgb=im.getpixel( pos )
(r,g,b)=rgb
n= r*rate1/1000 + g*rate2/1000 + b*rate3/1000
#print "n:",n
if n>=60:
im.putpixel( pos,(255,255,255))
else:
im.putpixel( pos,(0,0,0))
im.save("blackwhite.bmp")
def saveAsBmp(fname):
pos1=fname.rfind('.')
fname1=fname[0:pos1]
fname1=fname1+'_2.bmp'
im = Image.open(fname)
new_im = Image.new("RGB", im.size)
new_im.paste(im)
new_im.save(fname1)
return fname1
if __name__=="__main__":
filename=saveAsBmp("2ujb.bmp")
RGB2BlackWhite(filename)
上一篇: Python绘制爱心
下一篇: python整小时 整天时间戳获取算法
47840
46387
37279
34731
29312
25970
24909
19949
19541
18026
5790°
6411°
5926°
5960°
7063°
5911°
5942°
6437°
6404°
7776°