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)
    
  • 相关阅读:
    多线程第四篇秒杀 一个经典的多线程同步问题
    用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能
    用JSP实现的商城购物车模块
    深入浅出JMS(一)——JMS简单介绍
    hdu 1700 Points on Cycle 水几何
    回调函数
    VC中获取窗体句柄的各种方法
    virus.win32.parite.H病毒的查杀方法
    [WF4.0 实战] WPF + WCF + WF 打造Hello World(基础篇)
    HDU 4946 Area of Mushroom 凸包 第八次多校
  • 原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/15434517.html
Copyright © 2011-2022 走看看