python image black and white

#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PIL import Image

im = Image.open(r " C:\Users\wangshaowei6\Desktop\wm.gif " )
 # (convert the image to 8-bit pixel mode) similar to RGB mode 
im.convert( " P " )
his = im.histogram()
vslues ={}

#Each pixel can represent 256 colors, and you will find that the white point is at most 
# (the position of the white serial number 255, which is the last digit, as you can see, there are 625 white pixels). The number of red pixels is around 200, and we can get useful colors by sorting them. 
for i in range(256 ):
    vslues[i] = his[i]

for j,k in sorted(vslues.items(),key = lambda x:x[1],reverse= True)[:10]:    # [:10] represents the first 10 
    passes # print(j , k)

#Construct a grayscale image 
im2 = Image.new( " P " ,im.size,255 )
 for y in range(im.size[1 ]):
     for x in range(im.size[0]):
        pix = im.getpixel((x,y))
        if pix == 220 or pix ==227 :
            im2.putpixel((x,y),0)
im2.show()
#Convert image to vector 
def buildvector(im):
    d1 = {}
    count = 0
     for i in im.getdata(): #Get               the value of each pixel 
        d1[count] = i
        count += 1
    return d1

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325298127&siteId=291194637