python---对指定文件内所有验证码图片颜色更换,PIL,Image,os,time

python—对指定文件内所有验证码图片颜色更换,PIL,Image,os,time
参考:https://www.cnblogs.com/WonderHow/p/4403727.html
https://www.cnblogs.com/dreamer-fish/p/3820625.html

1、python源码

# -*- coding: utf-8 -*-

from PIL import Image
import os
import time

i = 1
j = 1
filelist = []
def filelist1():
    rootdir = 'e:\pic2'
    list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
    for i in range(0,len(list)):
        path = os.path.join(rootdir,list[i])
        filelist.append(path)#每个文件的觉得路径做一个列表元素
        #print filelist[-1]

filelist1()
m = len(filelist)#打印出目录下所有的文件
print m

for n in range(0,m):
    img = Image.open(filelist[n])#读取系统的内照片
    #print filelist[n]
    #print (img.size)#打印图片大小
    #print (img.getpixel((4,4)))
    width = img.size[0]#长度
    height = img.size[1]#宽度
    for i in range(0,width):#遍历所有长度的点
        for j in range(0,height):#遍历所有宽度的点
            data = (img.getpixel((i,j)))#打印该图片的所有点
            #print data#打印每个像素点的颜色RGBA的值(r,g,b,alpha)
            #print (data[0])#打印RGBA的r值
            if (data[0]<=100 and data[1]<=150 and data[2]>=70):#RGBA的r值大于170,并且g值大于170,并且b值大于170
                img.putpixel((i,j),(234,53,57,255))#则这些像素点的颜色改成大红色
                img = img.convert("RGB")#把图片强制转成RGB
                localtime = time.strftime("%Y%m%d%H%M%S",time.localtime())
                filename = "e:/pic1/"+localtime+".png"
                img.save(filename)#保存修改像素点后的图片

2、脚本运行情况:运行结果不理想,目标结果是所有图片验证码的蓝色背景替换成红色背景
这里写图片描述

参考:制造图片验证码http://blog.csdn.net/liangyuannao/article/details/8969551

猜你喜欢

转载自blog.csdn.net/xwbk12/article/details/79008191
今日推荐