pygame结合pillow滤镜对图像进行淡入淡出显示.py _pygame显示pillow美女

"""pygame结合pillow滤镜对图像进行淡入淡出显示.py
本程序用pillow的增强器对图形进行处理,然后用pygame显示出来"""

import pygame
from pygame.locals import *
from PIL import Image,ImageEnhance
 

pygame.init()
w,h = 300,300
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption("慢慢显示出来的美女,淡入淡出的图形编程技巧_PIL与pygame显示图形_作者:李兴球")

class Picture():
    def __init__(self,filepath):
        self.im  = Image.open(filepath)
        self.mode = self.im.mode
        self.size = self.im.size
        self.constract_enhancer = ImageEnhance.Contrast(self.im)
        self.color_enhancer = ImageEnhance.Color(self.im)
        self.brightness_enhancer = ImageEnhance.Brightness(self.im)
        self.sharpness_enhancer = ImageEnhance.Sharpness(self.im)
    def contract(self,factor):
        """factor应该在0和1之间的浮点数"""
        self.im = self.constract_enhancer.enhance(factor)
    def color(self,factor):
        """factor在0.0和1.0之间"""
        self.im = self.color_enhancer.enhance(factor)
    def brightness(self,factor):
        """factor在0.0和1.0之间"""
        self.im = self.brightness_enhancer.enhance(factor)
    def sharpness(self,factor):
        """factor在1.0和2.0之间, 异常处理略,上同"""
        self.im = self.sharpness_enhancer.enhance(factor)
    def update(self):
        self.data  = self.im.tobytes()
        self.image = pygame.image.fromstring(self.data,self.size,self.mode)
        screen.blit(self.image,(0,0))
        pygame.display.update()
    
        
      
clock = pygame.time.Clock()

picture1  = Picture("林素婉300.jpg")   #请自己设置一张像素为300x300照片即可.
factor = 0.0
x = 0
running = True
while running:
    for event in pygame.event.get():
        if event.type==QUIT:running = False
    picture1.brightness(factor)
    picture1.update()
    x = x + 1
    x = x % 20
    factor = (10 - abs(x-10)) /10.0
    clock.tick(5)
    
pygame.quit()                  #安全退出
 
"""
互相转换示例代码:
pygame.camera.init()
cam = pygame.camera.Camera(pygame.camera.list_cameras()[0],(1280,720))
cam.start()
time.sleep(1)
webcamImage = cam.get_image()
pil_string_image = pygame.image.tostring(img,"RGBA",False)
im = Image.frombytes("RGBA",(1280,720),pil_string_image)

"""

附:林素婉300.jpg (化名)