Posts Tagged ‘ Python ’
From over on my PyGame Wiki…
There doesn’t seem to be a built in way in PyGame to ‘blur’ a surface, but you can fake it bay scaling the surface twice: Scale it small once, then scale it back to normal size.
def blurSurf(surface, amt): """ Blur the given surface by the given 'amount'. Only values 1 and greater are valid. Value 1 = no blur. """ if amt < 1.0: raise ValueError("Arg 'amt' must be greater than 1.0, passed in value is %s"%amt) scale = 1.0/float(amt) surf_size = surface.get_size() scale_size = (int(surf_size[0]*scale), int(surf_size[1]*scale)) surf = pygame.transform.smoothscale(surface, scale_size) surf = pygame.transform.smoothscale(surf, surf_size) return surf
You can also use pygame.transform.rotozoom()
in place of smoothscale
. However, they have different effects: smoothscale
appears to blur (and push) the pixels ‘outward’ based on the center of the surface, while rotozoom
blurs (and pushes) them outward based on the top left corner of the image (0,0)
, effectively making them translate down and to the right in the process.
http://www.pygame.org/docs/ref/transform.html#pygame.transform.smoothscale
http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotozoom
Other methods I haven’t investigated would involve running a convolve routine on the surface pixels. As is, I’m not sure how could the performance would be though.
I’d be interested to know what methods others have come up with to solve this problem. My searching hasn’t come up with much.
After getting the book “INTRODUCING Fractal Geometry“, I thought it’d make a good learning project to tackle “The Chaos Game“, as coined by the mathematician Michael Barnsley. Seemed simple enough to grasp, and would be fun to code up in Python\PyGame.
What attracted me to it was that while it was a seemingly simple problem, a few things outside my realm of knowledge were needed: Find a random point inside of a triangle. After a bit of web searching, I ran across the post on Wolfram Mathworld describing ‘Triangle Point Picking‘. It shows how to find points based on a quadrilateral (2x triangle). Using that code, and a Python specific post called “Deciding if a Point is Inside a Polygon“, I was able to combine them with the game rules to achieve success, the Sierpinski triangle!
While the end result is nothing special to look at, Sierpinski triangles have been around for a long time, it was the fun of actually making the whole thing work through Python that kept me at it on Sunday afternoon 😉
Now has its own page on the blog.
Based on my previous post, I’ve got a usable version of Bubblepaint ready for the masses.
In a nutshell, it’s a simple ‘paint program’ using 2d physics. You ‘paint’ with ‘bubbles’ that collide with each other. Good fun. It’s implemented via Python through PyGame (graphics) using PyMunk (physics). It’s documentation is on a toggleable layer on the main screen once it starts.
You can find a zipped Windows executable here:
You can find the updated Python source here:
Put a few pics on up Flickr here.
I’d love to hear some feedback. I don’t have many plans to develop it much further, but you never know 😉
Originally posted on my Python Wiki.
This is a function I had made for my BubblePaint program in PyGame. In a nutshell, it will save the screen to a png
image, and increment the filename with each image in the dir. User can keep saving images, and this will keep incrementing the number of the newly saved file.
import glob, os, re def saveImage(): """ Save the current image to the working directory of the program. """ currentImages = glob.glob("*.png") numList = [0] for img in currentImages: i = os.path.splitext(img)[0] try: num = re.findall('[0-9]+$', i)[0] numList.append(int(num)) except IndexError: pass numList = sorted(numList) newNum = numList[-1]+1 saveName = 'bubblePaint.%04d.png' % newNum print "Saving %s" % saveName pygame.image.save(screen, saveName)
Will save out files like this:
bubblePaint.0001.png bubblePaint.0002.png etc...