pygame图像处理基础简单数学运算_PyGame image processing foundation

"""图像处理基础简单数学运算,像素也能加减乘除,所谓图像处理就是对像素操作,以下没有矩阵操作,适合于中小学生理解。"""

__author__ = "李兴球"
__date__ = "2019/5/5"


import pygame
import random

def pixel_add(pixel,number):
    """pixel:像素三元组,number:要增加的数值"""
    r,g,b = pixel
    b = min((b + number ) ,255)
    b = max(b,0)
    g = min((g + number ) ,255)
    g = max(g,0)
    r = min((r + number ) ,255)
    r = max(r,0)
    return r,g,b   


cyj = pygame.image.load("cyj.png")
width,height = cyj.get_size()

### 像素的加差
##for x in range(width):
##    for y in range(height):
##        r,g,b,a = cyj.get_at((x,y))        
##        r,g,b = pixel_add((r,g,b),-150) 
##        cyj.set_at((x,y),(b,g,r,255))
##
##pygame.image.save(cyj,"cyj_2.png")

# 像素值两级化,大于127的就让它的值变成255,否则为0
for x in range(width):
    for y in range(height):
        r,g,b,a = cyj.get_at((x,y))
        r = (r > 127) * 255
        g = (g > 127) * 255
        b = (b > 127) * 255
        cyj.set_at((x,y),(b,g,r,255))

pygame.image.save(cyj,"cyj_3.png")
        

        

pygame图像处理基础简单数学运算