省钱大作战之图片色彩

来源:互联网 发布:stc12c5a08s2数据手册 编辑:程序博客网 时间:2024/04/29 15:41

考试周结束的那个晚上,表示超级无聊。。。由于上次去打印店被老板因为截得全黑的图多收了2块大洋,表示愤愤不平,于是,就去做了这么个程序,几十行代码,主要是供计算机专业的使用,至于对编程一窍不通的人,表示很无奈。。。。】

关于程序:

eps和

img = brightness.enhance(10.0) #这条语句

主要的目的是用来调整的,不同的图可能不同。。。如果为了省事,可以直接在

                if Near(x, y, z, 255):
这条语句改为

                if Near(x, y, z, 255) or True:
这样效果还是比较好的。。。

此次主要是是

x, y, z = img.getpixel((i, j))

img.putpixel((i, j), (255 - x, 255 - y, 255 - z))
语句的学习。。

__author__ = 'glcsnz123'import Image, ImageEnhanceimport osimport syssuffex = ['.png', '.jpg', '.jpeg']eps = 70def Check(name):    for i in suffex:        if name.endswith(i):            return True    return Falsedef Near(x, y, z, n):    tot = 0    if abs(n - x) <= eps:        tot += 1    if abs(n - y) <= eps:        tot += 1    if abs(n - z) <= eps:        tot += 1    if tot == 3:        return True    return Falseif __name__ == '__main__':    filelist = os.listdir(os.getcwd())    for filname in filelist:        if not Check(filname):            continue        print filname        img = Image.open(filname)        brightness = ImageEnhance.Brightness(img)        img = brightness.enhance(10.0)        #img.show()        #print img.histogram()        xsize, ysize = img.size        for i in range(xsize):            for j in range(ysize):                #print  img.getpixel((i, j))                x, y, z = img.getpixel((i, j))                #print x, y, z                if Near(x, y, z, 255):                    img.putpixel((i, j), (255 - x, 255 - y, 255 - z))                elif Near(x, y, z, 0):                    img.putpixel((i, j), (255 - x, 255 - y, 255 - z))        img.save("bak_" + filname)