I have a pygame surface (some text created by font.render) that is mostly transparent except some pixels are white. I'd like to change half of these white pixels black. I know I could iterate through each pixel, checking whether it is white by get_at() and changing its color using set_at(), but it strikes me that there must be a faster way of telling pygame to grab all the white pixels and change a random half of them to black. Any ideas?
I think you'll need to iterate through all the pixels to check if they're white. Once you have the list of whites, do something like:
for px in random.sample(whites,len(whites)/2):
Surface.set_at((px.x,px.y),'black')
Python Documentation - random
Related
I've got the following image:
I want to fill in the black hole and make it white. How can I fill in this hole?
Thank you
You could floodfill with white starting at the top-left corner, which will leave you with this - which should allow you to locate the "hole".
I have bordered artificially with red so you can see the extent of the image.
apply this method Anyone knows an algorithm for finding "shapes" in 2d arrays?
Images are basically arrays and you can apply this algorithm with little bit modification in order to find holes and set every black pixel in closed shape and set blacks to white
I need to keep only red text from images like this one.
Image with text
I have tried turning all pixels with less than 210-240 value for red in RGB to black, but it depends much on the image and the light of it, so it is not a good solution and is not always working.
I would need to make it using Python.
Having an issue with blitting order (I think) and transparent bounding boxes showing up during collisions.
Link to the code and the issue is here: https://github.com/CastleSeven/flappy-balloon/issues/1
Basically, whenever a collision is about to occur, the player model is occluded by a transparent bounding box around the obstacle. I'm not sure what I need to change so that JUST the non-transparent pixels are reblitted for the background on every loop.
If I change the code so that the balloon blit comes AFTER the obstacle blit, I get the inverse effect, the balloon's bounding box occludes the obstacle.
Try using a colorkey, make the background of the textures something ugly like (255, 0, 255) and set the colorkey of the textures the same color. https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey
I have got some surfaces in Pygame with a transparent background. They're all the same size. But there's a different sized circle drawn on each of them, so the circle doesn't exactly fit the image.
Here are some example images (I took a screenshot in Photoshop so you can clearly see the transparency and the size of the images):
Now I want to remove the transparent border around the image so the circle exactly fits into the image. I don't want the surface to be circle shaped, I don't think that's possible, but I want that the surface doesn't have blank columns on the left and right and that it doesn't have any blank rows on the top and the bottom. The wanted results:
The circle on the surfaces changes size every frame so I have to recalculate the new surfaces every frame.
I already Googled it, but I haven't found anything for Pygame surfaces yet. I also tried making my own function but it looks ugly and much worse: the framerate drops from 50 (if I don't call the function) to 30 fps (if I do call the function). I tested it a little bit and I found out that smaller circles take longer to process than bigger circles. How can I do this, but faster. If you want I can show the function I made.
The surface object has a method called get_bounding_rect which is where we will start. The function returns the smallest rect possible which contains all of the non-transparent pixels on the surface.
pixel_rect = image.get_bounding_rect()
With the size of this rect, we can create a new surface:
trimmed_surface = pygame.Surface(pixel_rect.size)
Now blit the portion of image contained within pixel_rect onto trimmed_surface:
trimmed_surface.blit(image, (0,0), pixel_rect)
At this point, trimmed_surface should be a surface the same size as pixel_rect, with the unwanted transparent rows and columns "trimmed" off of the original surface.
Documentation for Surface.get_bounding_rect: http://www.pygame.org/docs/ref/surface.html#Surface.get_bounding_rect
I have several images that claim to have a transparent background but are actually white. I'd like to use Python Image Library/PIL to set that white background color to actually be transparent.
Since PNG uses an alpha channel, I'd love to create the alpha channel by finding contiguous areas of white from the edges of the image (so I don't get "holes" of transparency when the image contains white data).
Any tips on how to create the alpha channel this way?
I'd guess you'd want to run across the image in a spiral from the outside, setting a pixel to transparent if it is white, and a pixel further towards the edge is also white transparent. Stop once you've done a whole circle without changing any pixels.
Shouldn't be too difficult to write such a loop.
Do some kind of flood fill, seeded from the white edge pixels.