demo_pil.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import PIL
  2. import PIL.Image
  3. import PIL.ImageFilter
  4. import os
  5. import time
  6. import pygame
  7. pg = pygame
  8. pygame.init()
  9. w = 1600
  10. w = 600
  11. h = int(600/16*9) # 16:9
  12. main_size=(w,h)
  13. window = pygame.display.set_mode(main_size,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  14. #window = pygame.display.set_mode(main_size,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  15. pg.display.set_caption('LibreLight PIL')
  16. #pg.transform.smoothscale(window,(100,100))
  17. class IMG():
  18. def __init__(self,w=300,h=200,color=[255,0,0]):
  19. self.w = w
  20. self.h = h
  21. self.color = color
  22. self.img = PIL.Image.new("RGBA", (w, h))
  23. self.pixels = [(color[0],color[1],color[2],100)]*(w*h)
  24. self.img.putdata(self.pixels)
  25. self._blur_dir = 1
  26. self._blur = 5
  27. def reset(self):
  28. self.pixels = [(self.color[0],self.color[1],self.color[2],100)]*(self.w*self.h)
  29. def draw(self,x=10,y=10,b=10,h=10,color=(255,255,255,255)):
  30. _len = len(self.pixels)
  31. for i in range(b):
  32. _x = (i+x)* (self.w )
  33. if _x < _len:
  34. pass#print(self.pixels[_x])
  35. for j in range(h):
  36. _y = j +y
  37. idx = _x+_y
  38. if idx < _len:
  39. self.pixels[idx] = color #(255,255,255)
  40. def get(self):
  41. self.img.putdata(self.pixels)
  42. #self.img = self.img.resize((300, 200), PIL.Image.ANTIALIAS)
  43. #self.img = self.img.filter(PIL.ImageFilter.BLUR)
  44. if self._blur_dir:
  45. self._blur += .1
  46. else:
  47. self._blur -= .1
  48. if self._blur > 6:
  49. self._blur_dir = 0
  50. elif self._blur < 0:
  51. self._blur_dir = 1
  52. #self.img = self.img.filter(PIL.ImageFilter.GaussianBlur(self._blur))
  53. self.img = self.img.filter(PIL.ImageFilter.GaussianBlur(4))
  54. #print( dir(self.img)) #.getpixel((1,1)) )
  55. t = self.img.tobytes()
  56. #print( t[:20] )
  57. tt = bytearray(t)
  58. for i in range(100):
  59. tt[i+600] = 255 #).to_bytes(1,byteorder='big')
  60. t = bytes(tt)
  61. s = self.img.size
  62. m = self.img.mode
  63. img = PIL.Image.frombytes(m ,s ,t) #t,s,m) #s,tb)
  64. #img = PIL.Image.frombytes(m ,s ,t) #t,s,m) #s,tb)
  65. #img = img.resize((main_size[0], main_size[1]))
  66. #print( self.img.getpixel((1,1)) )
  67. t = img.tobytes()
  68. s = img.size
  69. m = img.mode
  70. out = pygame.image.fromstring( t,s,m).convert()
  71. return out
  72. run = True
  73. x = 10
  74. x_dir = 1
  75. #img = IMG(w=300,h=168)
  76. img = IMG(w=600,h=337)
  77. #img = IMG(w=500,h=281)
  78. #img = IMG(w=1900,h=800)
  79. start_fps = time.time()
  80. fps_c = 0
  81. fps = 0
  82. while run:
  83. #pg.clock.tick(60)
  84. for event in pygame.event.get():
  85. if event.type == pygame.QUIT:
  86. run = False
  87. pg.transform.smoothscale(window,(600,600))
  88. window.fill(0)
  89. #img = IMG(w=400,h=300)
  90. #img = IMG(w=1900,h=800)
  91. img.reset()
  92. img.draw()
  93. img.draw(x=30,y=30, color=(0,0,0))
  94. img.draw(x=x,y=x)#50)
  95. pygameSurface = img.get()
  96. pygameSurface = pygame.transform.scale(pygameSurface,[main_size[0]-10,main_size[1]-10])
  97. #player_rect = img.get_rect(center=(200, 200))
  98. #window.blit(pygameSurface, pygameSurface.get_rect(center = (150, 150)))
  99. window.blit(pygameSurface, pygameSurface.get_rect(topleft= (5, 5)))
  100. pygame.display.flip()
  101. #pg.transform.smoothscale(window,(100,200))
  102. pg.time.wait(10)
  103. print(fps,x_dir,x)
  104. if x_dir:
  105. x+=1
  106. if x > 200:
  107. x_dir = 0
  108. else:
  109. x-=1
  110. if x <= 0:
  111. x_dir = 1
  112. if start_fps+1 < time.time():
  113. start_fps = time.time()
  114. fps = fps_c
  115. fps_c = 0
  116. fps_c += 1
  117. exit()