zoukankan      html  css  js  c++  java
  • 多组相同背景图片分类和背景提取

    背景分类

    from PIL import Image
    import os
    import shutil
    
    file_dir = 'C:/Users/10767/Desktop/SortCapcha/Necaptcha'
    new_dir = 'C:/Users/10767/Desktop/SortCapcha/Sort'
    image_filenames = [os.path.join(file_dir, x) for x in os.listdir(file_dir)] #get all url
    lenth = len(image_filenames)
    
    def SortCapcha(k): #start with pic[0], compare all, move to k
        sorted_dir = new_dir + str(k)
        os.mkdir(sorted_dir)
        imagine = Image.open(image_filenames[0])
        imagine.show()
        width, height = imagine.size
        listR = []
        listG = []
        listB = []
        for x in range(width):
            for y in range(height):
                r, g, b = imagine.getpixel((x, y))
                listR.append(r)
                listG.append(g)
                listB.append(b)
    
        for i in range(1, lenth):
            comImagine = Image.open(image_filenames[i])
            comWidth, comHeight = comImagine.size
            if(comWidth != width or comHeight != height):
                continue
            comListR = []
            comListG = []
            comListB = []
            for x in range(width):
                for y in range(height):
                    r, g, b = comImagine.getpixel((x, y))
                    comListR.append(r)
                    comListG.append(g)
                    comListB.append(b)
    
            cnt = 0
            pickNum = 0
            for x in range(0, width * height, 5):
                pickNum += 1
                if abs(listR[x] - comListR[x]) <= 10 and abs(listG[x] - comListG[x]) <= 10 and abs(listB[x] - comListB[x]) <= 10:
                    cnt += 1
            if cnt / pickNum > 0.6:
                shutil.move(image_filenames[i], sorted_dir)
    
        shutil.move(image_filenames[0], sorted_dir)
    
    runTimes = 0
    while lenth > 0:
        runTimes += 1
        SortCapcha(runTimes)
        image_filenames = [os.path.join(file_dir, x) for x in os.listdir(file_dir)]  # get all url
        lenth = len(image_filenames)
    

    背景提取

    from PIL import Image
    import os
    
    def GetBackground(file_dir, output_dir, k):
    
    
        image_filenames = [os.path.join(file_dir, x) for x in os.listdir(file_dir)]  # get all url
        lenth = len(image_filenames)
    
        imagine = Image.open(image_filenames[0])
        width, height = imagine.size
    
    
        listR = [[0] * width * height for i in range(lenth)]  # create list for data
        listG = [[0] * width * height for i in range(lenth)]
        listB = [[0] * width * height for i in range(lenth)]
    
        R = [0] * width * height  # ans
        B = [0] * width * height
        G = [0] * width * height
    
        for i in range(lenth):
            imagine = Image.open(image_filenames[i])
            for x in range(width):
                for y in range(height):
                    r, g, b = imagine.getpixel((x, y))
                    listR[i][x * height + y] = r
                    listB[i][x * height + y] = b
                    listG[i][x * height + y] = g
    
        for x in range(width):
            for y in range(height):
                backetR = [0] * 256  # backet sort
                backetG = [0] * 256
                backetB = [0] * 256
                for i in range(lenth):
                    backetR[listR[i][x * height + y]] += 1
                    backetG[listG[i][x * height + y]] += 1
                    backetB[listB[i][x * height + y]] += 1
                maxNumR = -1
                maxNumG = -1
                maxNumB = -1
                for i in range(256):
                    if backetR[i] > maxNumR:
                        R[x * height + y] = i
                        maxNumR = backetR[i]
                    if backetG[i] > maxNumG:
                        G[x * height + y] = i
                        maxNumG = backetG[i]
                    if backetB[i] > maxNumB:
                        B[x * height + y] = i
                        maxNumB = backetB[i]
    
        newImagine = Image.new("RGB", (width, height))
    
        for x in range(width):
            for y in range(height):
                newImagine.putpixel((x, y), (R[x * height + y], G[x * height + y], B[x * height + y]))
    
        newImagine.save(output_dir + '/output' + str(k) + '.jpg')
    
    sourse_dir = 'C:/Users/10767/Desktop/SortCapcha'
    save_dir = 'C:/Users/10767/Desktop/SortCapcha/Output'
    for i in range(51):
        GetBackground(sourse_dir + '/Sort' + str(i), save_dir, i)
    
  • 相关阅读:
    dcokee 安装 nginx
    docker 私有仓库
    docker下的images 保存和导出
    mybatis-puls 字段为null时候的更新问题
    MyBatis-Plus的一些问题
    60万数据的表添加索引查询的速度
    Velocity 模板引擎的应用
    什么是javabean及其用法
    java中this和super关键字的使用
    Windows-AutoHotkey-常用代码保存
  • 原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/15434517.html
Copyright © 2011-2022 走看看