I'm working on a short intro in PyGame, and I need to blur some static lines. After two days of searching, I can't find anything.
Does PyGame have a built-in method to blur a shape or surface? Will I need to do it manually? What would be the best way to approach this?
To clarify, I'm not looking for motion blur – I just need a simple in-place blur, almost like a glowing effect.
There's no built-in way to blur a Surface.
Workarounds are:
scale the Surface with pygame.transform.smoothscale and then back to its original size (ugly)
iterating over each pixel, get the color of the neighbour pixels, calculate the average value for red, green, and blue, and set the color of that pixel (slow as hell if not using something like numpy)
I recommend using the Python Imaging Library (PIL), since it's nice and fast.
Here's a simple and running example:
import pygame
from PIL import Image, ImageFilter
pygame.init()
screen = pygame.display.set_mode((512, 512))
clock = pygame.time.Clock()
compressed = 'eJztnV2LHEUUhv0J+w+cf5CAELxQGT+IqNGsicYQds3kc4nEZDBiYhKTNmaFEBeyagzEiIMiKCIOKiiSi0HEGxU2F+qFKIMXGhTMXBiE3LT9jt1Db6c/qnqq6pyqroaXZGene2fqOXVO1anT1WEY3hIy0OLcpnak2UhBrF6kQY76qfd04vNa1J/fS4r1TMwOjFcihYoE+zgXX9vbBCPFzLuKeVdpJf6b3hbouLfifm6KeZF63g6Ms4cfHjFgn9Y56nZpguI4TM26LC7MULeRq1r8fxxPzdjbAB3/AQO+IupSt5WLitp1yICtiALqtnJRDLh6/p6/iAbUbeWiGHD1/D1/z7+B/F/atys8EQThseUL4dGL74RH3v1oIrz2wpmzYXD42fB0Z6vn7xB/MD36xqVVvKt0fGn5avRvi7q9XFLEYq3R/r5nftyvZbjnqOftQBn/tsk+//zb70/LPtEoEmk+KPr77UidSEFKXbxOzZUbf7BXxD2rIRgY4j0T8+4Lfja8b5aaMTV/jeyz/qAf98G2gFoS3FvxdUc1P9sKV5+gmz/ivUKfr0MrJXYTxL9X9bcCat6m+SsY67mmHjVzU/xPHdhXq40ufvnVRAx4OW0DOvkjdyPaJmc+/jz8/pdhmHfgdfyeATeVYlHXpJO/aNz/8Jvvcrlnj69/+pmamWq1XeUv6vtF2SfH5Ss/UjObKPjgk1WxCj9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ZcPX8aW2gx4CtiKy+146zFvnHgVEkZ+quOCpq39Yig/1gcgT/1LjcLKEdtJnEBNhim7o9mir0uUhdwz4Bfv4cfBH19/e6yRYwVwhiexgp7ueB7+v2KY4V7Zhfon7MNKte6j04x4/nJPUfe7dmpQ=='
size, image_mode, raw = (128, 128), 'RGBA', compressed.decode("base64").decode("zlib")
# create the original pygame surface
surf = pygame.image.fromstring(raw, size, image_mode)
# create a PIL image and blur it
pil_blured = Image.fromstring("RGBA", size, raw).filter(ImageFilter.GaussianBlur(radius=6))
# convert it back to a pygame surface
other = pygame.image.fromstring(pil_blured.tostring("raw", image_mode), size, image_mode)
pygame.time.set_timer(pygame.USEREVENT, 1000)
while True:
for e in pygame.event.get():
if e.type == pygame.USEREVENT: surf, other = other, surf
if e.type == pygame.QUIT: break
else:
screen.fill((255, 255, 255))
screen.blit(surf, (192, 192))
pygame.display.flip()
clock.tick(60)
continue
break
Hint: If you're on Windows and want to install PIL, download it from here and install it via
pip install Pillow-2.8.2-cp27-none-win32.whl
or whatever version you downloaded (installing PIL on Windows can be a great PITA otherwise).
Related
I would like to know how to draw images using pygame. I know how to load them.
I have made a blank window.
When I use screen.blit(blank, (10,10)), it does not draw the image and instead leaves the screen blank.
This is a typical layout:
myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()
while 1:
your_code_here
screen.fill(black)
screen.blit(myimage, imagerect)
pygame.display.flip()
import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))
player = pygame.image.load(os.path.join("player.png"))
player.convert()
while True:
screen.blit(player, (10, 10))
pygame.display.flip()
pygame.quit()
Loads the file player.png.
Run this and it works perfectly. So hopefully you learn something.
Images are represented by "pygame.Surface" objects. A Surface can be created from an image with pygame.image.load:
my_image_surface = pygame.load.image('my_image.jpg')
However, the pygame documentation notes that:
The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call convert() with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.
Use the appropriate conversion method for best performance:
image_surface = pygame.load.image('my_image.jpg').convert()
alpha_image_surface = pygame.load.image('my_icon.png').convert_alpha()
A Surface can be drawn on or blended with another Surface using the blit method. The first argument to blit is the Surface that should be drawn. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. It should be mentioned that the window respectively display is also represented by a Surface. Therefore, drawing a Surface in the window is the same as drawing a Surface on a Surface:
window_surface.blit(image_surface, (x, y))
window_surface.blit(image_surface,
image_surface.get_rect(center = window_surface.get_rect().center))
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
pygameSurface = pygame.image.load('apple.png').convert_alpha()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((127, 127, 127))
window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
pygame.display.flip()
pygame.quit()
exit()
pygame.image.load is bale to load most images. According to the documentation the following formats are supported: JPG, PNG, GIF (non-animated), BMP, PCX, TGA (uncompressed), TIF, LBM (and PBM), PBM (and PGM, PPM), XPM.
If you want to use images in PyGame that are loaded with other libraries, see:
PIL and pygame.image
How do I convert an OpenCV (cv2) image (BGR and BGRA) to a pygame.Surface object
For information on loading Scalable Vector Graphics (SVG) files, see:
SVG rendering in a PyGame application
Loading animated GIF files is presented at:
How can I load an animated GIF and get all of the individual frames in PyGame?
How do I make a sprite as a gif in pygame?
Or see how to load NumPy frames:
Pygame and Numpy Animations
After using blit or any other update on your drawing surface, you have to call pygame.display.flip() to actually update what is displayed.
I would like to know how to draw images using pygame. I know how to load them.
I have made a blank window.
When I use screen.blit(blank, (10,10)), it does not draw the image and instead leaves the screen blank.
This is a typical layout:
myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()
while 1:
your_code_here
screen.fill(black)
screen.blit(myimage, imagerect)
pygame.display.flip()
import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))
player = pygame.image.load(os.path.join("player.png"))
player.convert()
while True:
screen.blit(player, (10, 10))
pygame.display.flip()
pygame.quit()
Loads the file player.png.
Run this and it works perfectly. So hopefully you learn something.
Images are represented by "pygame.Surface" objects. A Surface can be created from an image with pygame.image.load:
my_image_surface = pygame.load.image('my_image.jpg')
However, the pygame documentation notes that:
The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call convert() with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.
Use the appropriate conversion method for best performance:
image_surface = pygame.load.image('my_image.jpg').convert()
alpha_image_surface = pygame.load.image('my_icon.png').convert_alpha()
A Surface can be drawn on or blended with another Surface using the blit method. The first argument to blit is the Surface that should be drawn. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. It should be mentioned that the window respectively display is also represented by a Surface. Therefore, drawing a Surface in the window is the same as drawing a Surface on a Surface:
window_surface.blit(image_surface, (x, y))
window_surface.blit(image_surface,
image_surface.get_rect(center = window_surface.get_rect().center))
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
pygameSurface = pygame.image.load('apple.png').convert_alpha()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((127, 127, 127))
window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
pygame.display.flip()
pygame.quit()
exit()
pygame.image.load is bale to load most images. According to the documentation the following formats are supported: JPG, PNG, GIF (non-animated), BMP, PCX, TGA (uncompressed), TIF, LBM (and PBM), PBM (and PGM, PPM), XPM.
If you want to use images in PyGame that are loaded with other libraries, see:
PIL and pygame.image
How do I convert an OpenCV (cv2) image (BGR and BGRA) to a pygame.Surface object
For information on loading Scalable Vector Graphics (SVG) files, see:
SVG rendering in a PyGame application
Loading animated GIF files is presented at:
How can I load an animated GIF and get all of the individual frames in PyGame?
How do I make a sprite as a gif in pygame?
Or see how to load NumPy frames:
Pygame and Numpy Animations
After using blit or any other update on your drawing surface, you have to call pygame.display.flip() to actually update what is displayed.
I would like to know how to draw images using pygame. I know how to load them.
I have made a blank window.
When I use screen.blit(blank, (10,10)), it does not draw the image and instead leaves the screen blank.
This is a typical layout:
myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()
while 1:
your_code_here
screen.fill(black)
screen.blit(myimage, imagerect)
pygame.display.flip()
import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))
player = pygame.image.load(os.path.join("player.png"))
player.convert()
while True:
screen.blit(player, (10, 10))
pygame.display.flip()
pygame.quit()
Loads the file player.png.
Run this and it works perfectly. So hopefully you learn something.
Images are represented by "pygame.Surface" objects. A Surface can be created from an image with pygame.image.load:
my_image_surface = pygame.load.image('my_image.jpg')
However, the pygame documentation notes that:
The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call convert() with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.
Use the appropriate conversion method for best performance:
image_surface = pygame.load.image('my_image.jpg').convert()
alpha_image_surface = pygame.load.image('my_icon.png').convert_alpha()
A Surface can be drawn on or blended with another Surface using the blit method. The first argument to blit is the Surface that should be drawn. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. It should be mentioned that the window respectively display is also represented by a Surface. Therefore, drawing a Surface in the window is the same as drawing a Surface on a Surface:
window_surface.blit(image_surface, (x, y))
window_surface.blit(image_surface,
image_surface.get_rect(center = window_surface.get_rect().center))
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
pygameSurface = pygame.image.load('apple.png').convert_alpha()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((127, 127, 127))
window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
pygame.display.flip()
pygame.quit()
exit()
pygame.image.load is bale to load most images. According to the documentation the following formats are supported: JPG, PNG, GIF (non-animated), BMP, PCX, TGA (uncompressed), TIF, LBM (and PBM), PBM (and PGM, PPM), XPM.
If you want to use images in PyGame that are loaded with other libraries, see:
PIL and pygame.image
How do I convert an OpenCV (cv2) image (BGR and BGRA) to a pygame.Surface object
For information on loading Scalable Vector Graphics (SVG) files, see:
SVG rendering in a PyGame application
Loading animated GIF files is presented at:
How can I load an animated GIF and get all of the individual frames in PyGame?
How do I make a sprite as a gif in pygame?
Or see how to load NumPy frames:
Pygame and Numpy Animations
After using blit or any other update on your drawing surface, you have to call pygame.display.flip() to actually update what is displayed.
I would like to know how to draw images using pygame. I know how to load them.
I have made a blank window.
When I use screen.blit(blank, (10,10)), it does not draw the image and instead leaves the screen blank.
This is a typical layout:
myimage = pygame.image.load("myimage.bmp")
imagerect = myimage.get_rect()
while 1:
your_code_here
screen.fill(black)
screen.blit(myimage, imagerect)
pygame.display.flip()
import pygame, sys, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((100, 100))
player = pygame.image.load(os.path.join("player.png"))
player.convert()
while True:
screen.blit(player, (10, 10))
pygame.display.flip()
pygame.quit()
Loads the file player.png.
Run this and it works perfectly. So hopefully you learn something.
Images are represented by "pygame.Surface" objects. A Surface can be created from an image with pygame.image.load:
my_image_surface = pygame.load.image('my_image.jpg')
However, the pygame documentation notes that:
The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call convert() with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.
Use the appropriate conversion method for best performance:
image_surface = pygame.load.image('my_image.jpg').convert()
alpha_image_surface = pygame.load.image('my_icon.png').convert_alpha()
A Surface can be drawn on or blended with another Surface using the blit method. The first argument to blit is the Surface that should be drawn. The second argument is either a tuple (x, y) representing the upper left corner or a rectangle. With a rectangle, only the upper left corner of the rectangle is taken into account. It should be mentioned that the window respectively display is also represented by a Surface. Therefore, drawing a Surface in the window is the same as drawing a Surface on a Surface:
window_surface.blit(image_surface, (x, y))
window_surface.blit(image_surface,
image_surface.get_rect(center = window_surface.get_rect().center))
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
pygameSurface = pygame.image.load('apple.png').convert_alpha()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((127, 127, 127))
window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
pygame.display.flip()
pygame.quit()
exit()
pygame.image.load is bale to load most images. According to the documentation the following formats are supported: JPG, PNG, GIF (non-animated), BMP, PCX, TGA (uncompressed), TIF, LBM (and PBM), PBM (and PGM, PPM), XPM.
If you want to use images in PyGame that are loaded with other libraries, see:
PIL and pygame.image
How do I convert an OpenCV (cv2) image (BGR and BGRA) to a pygame.Surface object
For information on loading Scalable Vector Graphics (SVG) files, see:
SVG rendering in a PyGame application
Loading animated GIF files is presented at:
How can I load an animated GIF and get all of the individual frames in PyGame?
How do I make a sprite as a gif in pygame?
Or see how to load NumPy frames:
Pygame and Numpy Animations
After using blit or any other update on your drawing surface, you have to call pygame.display.flip() to actually update what is displayed.
I'm trying to use opencv (cv2) to stream a webcam feed into a pygame surface object. The problem is the colors aren't displaying correctly. I think it is the type casting, but I'm having trouble understanding the pygame surface documentation to know what it expects.
This code demonstrates what I'm talking about
import pygame
from pygame.locals import *
import cv2
import numpy
color=False#True#False
camera_index = 0
camera=cv2.VideoCapture(camera_index)
camera.set(3,640)
camera.set(4,480)
#This shows an image the way it should be
cv2.namedWindow("w1",cv2.CV_WINDOW_AUTOSIZE)
retval,frame=camera.read()
if not color:
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.flip(frame,1,frame)#mirror the image
cv2.imshow("w1",frame)
#This shows an image weirdly...
screen_width, screen_height = 640, 480
screen=pygame.display.set_mode((screen_width,screen_height))
def getCamFrame(color,camera):
retval,frame=camera.read()
if not color:
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame=numpy.rot90(frame)
frame=pygame.surfarray.make_surface(frame) #I think the color error lies in this line?
return frame
def blitCamFrame(frame,screen):
screen.blit(frame,(0,0))
return screen
screen.fill(0) #set pygame screen to black
frame=getCamFrame(color,camera)
screen=blitCamFrame(frame,screen)
pygame.display.flip()
running=True
while running:
for event in pygame.event.get(): #process events since last loop cycle
if event.type == KEYDOWN:
running=False
pygame.quit()
cv2.destroyAllWindows()
The ultimate goal I have is to create a small photo booth application for a DIY wedding next year. I'm new to programming, but I've managed to cobble this together. I was also trying to accomplish this with VideoCapture, which outputs a PIL, which I also couldn't get to work with the surface object. I want to use a pygame surface so I can animate and overlay count-down text, borders, etc.
Update: The issue was that the cv2 function camera.read() returns a BGR image, but the pygame.surfarray expects a RGB image. This is fixed with the line
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
Also, when converting to grayscale, the following code works:
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame=cv2.cvtColor(frame,cv2.COLOR_GRAY2RGB)
So, the function getCamFrame should now be
def getCamFrame(color,camera):
retval,frame=camera.read()
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
if not color:
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame=cv2.cvtColor(frame,cv2.COLOR_GRAY2RGB)
frame=numpy.rot90(frame)
frame=pygame.surfarray.make_surface(frame)
return frame
No the color error lies in here
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
so for normal screen color u simply change that to
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
That will do because it works for me
I tried your code, but I'm only getting a picture not a movie, so i copied
frame=getCamFrame(color,camera)
screen=blitCamFrame(frame,screen)
pygame.display.flip()
into a while loop, it worked but then the video was flipped, to fix it i added cv2.flip(frame,1,frame) # mirror the image
before
frame=numpy.rot90(frame) in getcamFrame function and now everything works fine.
Sorry for the poor English.
This worked for me
to adapt it in your code ...
windowSurface = screen #or use directly variable screen
# convert windowSurface to cv2 ------------------------------------
view = pygame.surfarray.array3d(windowSurface)
# convert from (width, height, channel) to (height, width, channel)
view = view.transpose([1, 0, 2])
# convert from rgb to bgr
img_bgr = cv2.cvtColor(view, cv2.COLOR_RGB2BGR)
cv2.imshow('windowname',img_bgr)
cv2.waitKey(10)