Picture processing whims

Actual demand

Some time ago, the processing of the crawled data requires the implementation of a personalized word cloud, but the effect achieved due to the problem of the background image has not been ideal. There is no way to process the background image (note that the path is involved)
The original idea was to achieve the following effect (generating a word cloud of the outline of the young lady),
Insert picture description here
Insert picture description here
so the picture must be processed (processed picture)Insert picture description here

Binarization

At the beginning I thought of binarizing it, because the outline of the picture is not very obvious, so there is no way to generate the word cloud I want. Then obviously we can extract the general outline through binarization, which is also done Common methods used by crawlers to extract verification codes.
Below is the processed picture. The
Insert picture description here
code is as follows

def change_L(path,save_path,show_yes=1):

    image=Image.open(path)
    # image = image.convert('1')

    image=image.convert('L')

    t=[]

    for i in range(256):
        #杂质越多,值越大(轮廓越黑越明显)
        if i<160:
            t.append(0)
        else:
            t.append(1)

    image=image.point(t,'1')
    image.save(save_path)
    if show_yes:
        image.show()

However, the result is still stretched,
Insert picture description here
so I have to think about it at this time. At this time, I referred to other people's background pictures and guessed that it is the problem of the color difference of the picture. Relatively speaking, it is still not obvious. So here is considered to recolor it, just right first The picture is binarized to filter out some impurities, so that it is convenient to color.

Black and white coloring

The specific idea is to modify the
black (0,0,0), white (255,255,255) by the pixel RGB value .
Then it is simple. To replace the black rgb value I changed it to green here, and then re-draw the new image.

from PIL import Image


def change_L(path,save_path,show_yes=1):

    image=Image.open(path)
    # image = image.convert('1')灰度处理

    image=image.convert('L')

    t=[]

    for i in range(256):
        #杂质越多,值越大(轮廓越黑越明显)
        if i<160:
            t.append(0)
        else:
            t.append(1)

    image=image.point(t,'1')
    image.save(save_path)
    if show_yes:
        image.show()


def chang_rgb(path,save_path,rgb_change=(50,205,50),show_yes=1):

    im = Image.open(path)
    width = im.size[0]
    height = im.size[1]

    new_image = Image.new("RGB",(width,height))

    im = im.convert('RGB')
    # array = []
    for x in range(width):
        for y in range(height):
            r, g, b = im.getpixel((x,y))
            rgb = (r, g, b)
            if rgb == (0,0,0):
                rgb=rgb_change
            new_image.putpixel((x, y), (int(rgb[0]), int(rgb[1]), int(rgb[2])))
            # array.append(rgb)
    # print(array)

    new_image.save(save_path)
    if show_yes:
        new_image.show()

if __name__=='__main__':
    star_path=r'E:\projects\python_projects\worldcloudtext\pic\01.jpg'
    save_path = r'E:\projects\python_projects\worldcloudtext\pic_L.png'
    change_L(star_path,save_path,1)

    save_path_rgb=r'E:\projects\python_projects\worldcloudtext\pic_c_rgb.png'
    chang_rgb(save_path,save_path_rgb,(50,205,50),1)
    print('ok')

Insert picture description here
Then test other pictures
Insert picture description here
Insert picture description here

Picture comparison

Through the case just now, I will make a derivative of the previous approach to judge the similarity of the two pictures.

from PIL import Image

def compare(path1,path2,show_yes=1):

    def pic_change(path):
        image1 = Image.open(path)
        image1 = image1.resize((500, 500))
        image1 = image1.convert("RGB")
        return image1


    image1=pic_change(path1)
    image2=pic_change(path2)
    toutlo = 500*500

    same_p=0


    for x in range(500):
        for y in range(500):
            r1,g1,b1=image1.getpixel((x,y))
            r2, g2, b2 = image2.getpixel((x, y))
            if r1==r2 and g1 ==g2 and b2 == b1:
                same_p+=1

    present = (same_p / toutlo)*100
    if show_yes:
        print("%f%s"%(present,"%"))

if __name__=='__main__':
    path1='1.png'
    path2 = '2.png'
    compare(path1,path2)

A preliminary study on face recognition

In view of the previous content,
we might as well write a simple face recognition comparison
by ourselves. First take our Andy Lau's picture.
Insert picture description here
We first process the picture as before, and
then extract the outline of the human head. The
specific idea is to first judge the RGB value of the pixel. If the pixel above is It is white, the bottom is green, or the top is green and the bottom is white, then obviously this is an outline. If the previous and next pixels are both green, then it is not the edge of the outline, modify the rgb value to white

from PIL import Image


def pic_change(path):
    image1 = Image.open(path)
    image1 = image1.resize((500, 500))
    image1 = image1.convert("RGB")
    return image1

image = pic_change(r'E:\projects\python_projects\pic_compare\see_rgb.png')

new_img=Image.new("RGB",(500,500))

for x in range(500):
    for y in range(500):

        r,g,b=image.getpixel((x,y))
        rgb = (r, g, b)
        if rgb!=(255,255,255):
            if y>1 and y<499:
                r1,g1,b1=image.getpixel((x,y-1))
                rgb1=(r1,g1,b1)
                r2, g2, b2 = image.getpixel((x,y+1))
                rgb2 = (r2, g2, b2)
                if rgb1 ==(255,255,255) and rgb ==(50,205,50) and rgb2 ==(20,205,50):
                    rgb=(50,205,50)
                elif rgb1 ==(20,205,50) and rgb ==(50,205,50) and rgb2 ==(255,255,255):
                    rgb = (50, 205, 50)
                if rgb1 ==(50,205,50) and rgb ==(50,205,50) and rgb2 ==(50,205,50):
                    rgb=(255,255,255)

        new_img.putpixel((x,y),(int(rgb[0]), int(rgb[1]), int(rgb[2])))

new_img.show()

The effect is as follows.
Insert picture description here
However, the next picture is stretched (we found that Huazi on the right has a shadow),
Insert picture description here
which is embarrassing. Even the human face extracted is not
so how to handle it. Okay, I haven't thought about it for the time being, but I It is certain that this is a picture of the same picture from different angles.So
, if we can, we can build a database to simulate the effect of different angles of the same face and the processed picture, and then take out the positive picture data comparison. (Of course this is just my guess)

Comparison in an ideal state.
In this case, let's use the picture in the ideal state for preliminary identification. The
specific idea is to record the ordinate of the green pixel of the picture after the image is marginalized, because here is a vertical scan, and then a feature After the list (not the mainstream algorithm proposed here, but an idea that I have extended myself)
, we can directly use the variance to compare the deviation of the two lists. The
algorithm is as follows: (Note that the picture is processed first, With the code above)

from PIL import Image


def pic_change(path):
    image1 = Image.open(path)
    image1 = image1.resize((500, 500))
    image1 = image1.convert("RGB")
    return image1

def Dxmath(list_name):
    sums = 0
    sum_ = sum(list_name)
    long = len(list_name)
    agve = sum_ / long
    for i in range(long):
        x = list_name[i] - agve
        x = x * x
        sums += x
    agves = sums / long
    return agves


def outface(path,save_path,yes_show=1):

    image = pic_change(path)

    new_img=Image.new("RGB",(500,500))

    for x in range(500):
        for y in range(500):

            r,g,b=image.getpixel((x,y))
            rgb = (r, g, b)
            if rgb!=(255,255,255):
                if y>1 and y<499:
                    r1,g1,b1=image.getpixel((x,y-1))
                    rgb1=(r1,g1,b1)
                    r2, g2, b2 = image.getpixel((x,y+1))
                    rgb2 = (r2, g2, b2)
                    if rgb1 ==(255,255,255) and rgb ==(50,205,50) and rgb2 ==(20,205,50):
                        rgb=(50,205,50)
                    elif rgb1 ==(20,205,50) and rgb ==(50,205,50) and rgb2 ==(255,255,255):
                        rgb = (50, 205, 50)
                    if rgb1 ==(50,205,50) and rgb ==(50,205,50) and rgb2 ==(50,205,50):
                        rgb=(255,255,255)

            new_img.putpixel((x,y),(int(rgb[0]), int(rgb[1]), int(rgb[2])))

    new_img.save(save_path)
    if yes_show:
        new_img.show()

def issameorno(Standard_pic_path, path_need_compare):

    path1 = Standard_pic_path
    path2 = path_need_compare
    image1 = pic_change(path1)
    image2 = pic_change(path2)
    list_image1 = []
    list_image2 = []

    rgb = (50, 205, 50)
    for x in range(500):
        for y in range(500):
            '''纵向扫描'''
            r1, g1, b1 = image1.getpixel((x, y))
            rgb1 = (r1, g1, b1)
            if rgb1 == rgb:
                list_image1.append(y)
            else:
                list_image1.append(-1)

            r2, g2, b2 = image2.getpixel((x, y))
            rgb2 = (r2, g2, b2)
            if rgb2 == rgb:

                list_image2.append(y)
            else:
                list_image2.append(-1)
    '''提取对比,我把一张图片纵向分割为500条线,把每条线的绿色点的纵坐标记录下来其余的记为-1
    这样一来我就得到了这张人脸图片的特征列表,之后我要做的就是选择一个合适的算法去对比这两个列表的相似度
    先确定一点如果等比例放大(尺度内)列表的总和不变,图片上下移动,总差值与非-1点数程倍数.
    所以非-1点数与sum(list)相等就是同一个
    image1为标准图.
    '''
    green1_point = len(list_image1) - list_image1.count(-1)
    green2_point = len(list_image2) - list_image2.count(-1)

    sum_1 = sum(list_image1)
    sum_2 = sum(list_image2)

    if green1_point == green2_point and sum_1 == sum_2:
        print('they are same people')


    else:
        '''这里算方差吧'''
        S1 = Dxmath(list_image1)
        S2 = Dxmath(list_image2)

        same_pa = (S1 / S2) * 100
        print("%f%s" % (same_pa, "%"))


def main(path1,path2):
    path1=path1
    path2=path2
    save_path=r'E:\projects\python_projects\pic_compare\outface.png'
    save_path1 = r'E:\projects\python_projects\pic_compare\outface1.png'
    outface(path1,save_path,0)
    outface(path2, save_path1,0)

    path11 = r'E:\projects\python_projects\pic_compare\outface.png'
    path22 = r'E:\projects\python_projects\pic_compare\outface1.png'

    issameorno(path11,path22)



if __name__=='__main__':


    path1=r'E:\projects\python_projects\pic_compare\see_rgb.png'
    path2=r'E:\projects\python_projects\pic_compare\see_rgb.png'
    main(path1,path2)




Insert picture description here

Now make a slight modification to the picture. Here, directly call the issameorno() function for the processed picture.
Insert picture description here

Insert picture description here
Of course, this algorithm has great limitations.
At present, it can only compare human heads. If it is such a picture, the
Insert picture description here
result is as follows
Insert picture description here

Guess you like

Origin blog.csdn.net/FUTEROX/article/details/111514081