PNG Image Sprites retains background color - python

I am using this sprite for some experiments. I cropped a section of the sheet with GIMP and made the blue background transparent, the result being this. However, when I run the codes using this sprite, I still end up with a sprite with a blue background. Same thing happens with other sprites (different sprite sheet, same source, same editing process).
Now, I know that I can set transparent colors using PyGame itself but I'm wondering if that's the only way I can do it? Can't I do it through the image itself? How come PyGame still reads a blue backgorund even after I have cleared it with GIMP?
P.S. SSCE would be pvz_shooter_test.py and/or image_test.py of the GitHub repo linked.

Try mySurface.convert_alpha() (check the pygame documentation about it).

Just some more additional details on how come PyGame still read a blue background even after I've cleared it with GIMP.
When the image file format supports it, pixels aren't just represented as RGB bits. There's additional information for every pixel, the alpha channel. What GIMP did is to set the alpha bits to 0% opacity. The RGB values remained (in this instance, blue). Since my PyGame code isn't set to read the additional alpha bits, it only read the RGB describing blue. The convert_alpha method told PyGame to read the alpha channel as well.

Related

What is the simplest way to detect color in Python?

I'm trying to write a Python program that detects the color of a portion of the screen and puts it in a conditional. The easiest way, I would think, would be to detect the color underneath the cursor (which would work perfectly with my program), but I can't find many up-to-date resources on that. I would like to be able to have the RGB, HEX, or some other type of value in a variable. Some ways that would work for my program: detecting the color under the cursor, detecting the color of a certain pixel, or detecting the color of a portion of pixels. Thanks for your help!

saving modified screens in python/pygame for later use

When using python and pygame: after loading the screen with the background image and blitting new objects (Text, circles, rectangles, etc.), is there a way to save the modified screen so as to be recalled later in the program? Specifically, I am setting the background and blitting new objects and would like to save the screen image with all of the blits in intact so it can be used later in the program as a new background upon which sprites can be manipulated. Any suggestions welcomed!
Blitting works both ways, meaning you can blit something onto the display screen, but you can also blit the screen onto another surface. So simply make a new surface the same size of your display surface and blit the screen onto that surface for later use.
found a solution and it works better than I expected:
after blitted my raw background onto my surface and then adding numerous circles, rectangles and text to make an image with multiple dial, gauges and labels I ran the following:
pygame.display.update()
window = pygame.display.set_mode((800,480),0,32)
pygame.image.save(TFT,"screen_update.jpg")
the new image is saved to disk(XDcard on my RPi2) as "screen_update.jpg"
then I simply change the name to "ANAL_update.jpg" and use that as the background on my next program run. I commented out all of the code used to create the rectangles, circles and labels and it works. I will add an selectable "update" routine to the program and move all of extra drawing and labelling to that routine to be used when I wish to change the layout of he background. I like the fact that the program creates a new updated file that just needs to be renamed for use and for copying the background to other machines.
note: This is working on my RaspberryPi 2B with HDMI output to a 42" HD tv for development, but it is intended to run on an RPi3B with he official RPi 7 inch TFT display. Thanks to all of you that responded and to the others who left pertinent code for previous questions similar to mine.

How can I make the background of an image appear transparent without PIL?

I'm trying to make a GUI in tkinter that uses one image as an overlay on top of another, but when I place the image over the lower one, the transparent area of the image appears as grey.
I've searched for solutions to this, but all the results that I've found have pointed towards using PIL.
Is it possible for me to use transparent, or partially transparent images in the python tkinter module without using PIL?
You could use some basic photoshop tools like the magic wand tool to remove the background, but keep in mind, some PNG format images have a faint background. This is either in the from of a watermark, or the image background was rendered with a lower opacity than the rest of the image. Your GUI may also have a layer placed above the images by default. Does it appear on each image seperatly when loaded into the GUI?

PIL Image.getcolors() cannot distinguish between black and transparent?

I'm trying to count the number of distinct colors in an image using img.getcolors(). However, this does not distinguish between transparent and black pixels - they both report as one pixel colored [0,0,0].
How can I distinguish between transparent and black pixels? Many of the images I need to process are largely black on a transparent background.
For test purposes, I'm using a PNG I created which is half transparent, half black. len(img.getcolors()) is 1.
Embarrassing answer:
I was using convert('RGB') before calling getcolors(). Without the conversion, a 4-value tuple comes back with an alpha channel.

Transparent background for training OpenCV

I'm working on training OpenCV to detect an object using opencv_traincascade.
I see from the OpenCV documentation that there is an option to specify -bgcolorand -bgthresh for the positive images. From what I understand, this would set those pixels to clear and OpenCV would ignore them.
My questions:
Is this correct?
Would it be useful to shoot my objects against a green screen and specify the green as the background color?
Would anything be lost by doing that instead of shooting against a variety of backgrounds?
Yes
Yes -> This makes selecting the background color much, much easier.
The data of the backgrounds will be lost, but in most cases this is what you want. Otherwise, the trained data might recnogise the background more than the object.

Categories