#Pygame image convert alpha
Explore tagged Tumblr posts
longecono · 2 years ago
Text
Pygame image convert alpha
Tumblr media
PYGAME IMAGE CONVERT ALPHA HOW TO
PYGAME IMAGE CONVERT ALPHA CODE
This assumes that the images you will be blitting with alpha are usually the same ones over and over. You don't have to worry about what was on the image previously since you'll be blitting over it anyway. If the cache contains the size image you need, re-use it. A dictionary with string keys in the format of " x " will work. Ideally, you should keep a cache of temporary surfaces. Blit this image onto the background at the desired alpha.īecause I'm creating a temporary surface per frame, this is not a very performant solution.Because the temporary image is opaque, you can set the image alpha of it.The temporary image is completely opaque and has the transparent image on it above the background.Blit the per-pixel transparency image onto the temporary image.Blit the OPAQUE BACKGROUND onto this temporary image.Create a temporary image OPAQUE the size of the image you are trying to blit.If you replace the screen.blit(happy, (100, 100)) with a call to blit_alpha(screen, happy, (100, 100), 128), you get the following: Temp = pygame.Surface((source.get_width(), source.get_height())).convert() If you convert happy to not have per-pixel alpha, then you get an ugly box around it.ĭef blit_alpha(target, source, location, opacity): set_alpha does not work on per-pixel transparency. If you wanted to fade the sprite out, you'd probably try to use a t_alpha(new_alpha), however. The problem here is that the "happy" Surface contains alpha per pixels. Happy = ( 'happy.png') # our happy blue protagonistĬheckers = ( 'background.png') # 32x32 repeating checkered image You can rate examples to help us improve the quality of examples. I know very little about how it works or it's performance though.Screen = _mode(( 300, 300)) These are the top rated real world Python examples of nvertalpha extracted from open source projects. The _polygon function caught my attention. If you're feeling adventurous, you can look into the gfxdraw libraries for pygame. Also, copying a surface may not necessarily preserve the alphas, but like the last point, I've found that it does when the alphas are already defined in the original image. The StackOverflow question that I referenced details the two ways to ensure a surface has alphas which are unaffected by surfarray operations. So, the last few steps are actually taking your original surface and coloring it the way you need: origSurface = ('images/blue_circle.png')Ĭolor_surface(coloredSurface, 120, 78, 240)Ī couple of notes about this code: the call to convert_alpha on the surface seems to be unnecessary if the image already has per-pixel alphas defined. It might be necessary if you try to do more complicated things with the surface. Which I also left out because I've found this to be a startlingly slow call in practice. The tutorial I posted also uses an additional instruction, something like: iarray = numpy.array(arr) In that function, I use 3d because it references the pixels into an array, rather than copy them, making it a faster operation. def color_surface(surface, red, green, blue): Some credit goes to the poster who answered this question.
PYGAME IMAGE CONVERT ALPHA CODE
Below is a code example of what you're looking to do. I did a bit of toying around to make sure that this works as you need it. I should still note that in practice, I've found it not to be fast enough for some applications, such as operating on large images, but if you're only operating on small particle sprites, you should be fine. Ordinarily, this would be a very slow operation, but surfarrays use numpy/numeric as their core, so the operation is quite fast.
PYGAME IMAGE CONVERT ALPHA HOW TO
A simple tutorial on how to get started with them can be found here.Įssentially what a surfarray does is directly modify the pixel values of pygame surfaces, and can operate on each of the R, G, and B channels for every pixel "simultaneously," which I put in quotes because I just mean you can change all the pixels with just one line of code.
Tumblr media
0 notes