Using different colours as mask and changing colour on target image [duplicate] - python

This question already has answers here:
Is it possible to change sprite colours in Pygame?
(1 answer)
Changing colour of a surface without overwriting transparency
(1 answer)
Trying to make sections of sprite change colour, but whole sprite changes instead
(1 answer)
Changing ememy's color to show that it is aking damage?
(1 answer)
Closed 6 months ago.
Image: Target Image
Image: Mask
I was wondering if there is a way using pygame to change the target image colours based on a mask that uses different colours for different areas.
What I thought of doing was to loop through the mask, check each pixel for its colour and if said colour is yellow for example, which is the area of the head, then to alter the saturation of the colour of the target image.
I'd like to know if there is a better way to do? Thank you

Related

Pygame create colored button using mask [duplicate]

This question already has answers here:
Is it possible to change sprite colours in Pygame?
(1 answer)
Changing colour of a surface without overwriting transparency
(1 answer)
Trying to make sections of sprite change colour, but whole sprite changes instead
(1 answer)
Closed 10 months ago.
I'm working on a pygame project and i need to create a button that changes color when hovered. I can't just use 2 images (one with the normal color and one with the hovered one) because i want to have a smooth transition between the 2 colors over a few frames. I could create all the images but it wouldn't be a very efficient method at all. So i got the idea of using an alpha mask like this one for the quit button:
Where white mean the color with full opacity and black means 0 opacity. To display my button, i wanted to create a surface of the same size as the mask, fill it with the current button color, and then use my image as the alpha mask like in this example:
mask = pygame.image.load("quit.png").convert()
button = pygame.surface(mask.get_size())
button.fill(ButtonColor)
---Here goes the part where i use my mask as the alpha for my button---
screen.blit(button, ButonPosition)
But i searched the pygame doc and it seams that i can't set per-pixel alpha. So i wanted to know if you had a solution, or an alternative idea to make my button.

convert_alpha() not creating image transparency in PyGame png sprites [duplicate]

This question already has answers here:
Pygame image transparency confusion
(1 answer)
How do I blit a PNG with some transparency onto a surface in Pygame?
(4 answers)
Closed 1 year ago.
I have a function that renders sprites:
self.sheet = pygame.image.load(file).convert_alpha()
But the sprites with transparency still render with black backgrounds. I can't use colorkey because for some reason my sprites always export with random discrepancies in colour where the colour is a few RGB units off the original colour, causing dots to randomly appear around the sprite.
And yes, the sprites I'm using are .png files, not jpg or jpeg.
So my question is, why isn't convert_alpha working, what can I do to make it work, or what else can I do to reach the same goal?
EDIT: here's the spritesheet:

Set opacity of image in pygame [duplicate]

This question already has answers here:
How do I blit a PNG with some transparency onto a surface in Pygame?
(4 answers)
How can I make an Image with a transparent Backround in Pygame?
(2 answers)
Closed 2 years ago.
Okay guys, I'm trying to fade out an image. So given this:
img_ = pygame.image.load('circle.png')
screen.blit(img_, [100, 100])
how can I change the opacity of img_? Thanks!
Pygame surfaces have a method named set_alpha() for this, which takes a value between 0 and 255.
Relevant docs: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_alpha

Rectangles to cover maximum area [duplicate]

This question already has answers here:
OpenCV Python: Draw minAreaRect ( RotatedRect not implemented)
(5 answers)
Find Rotated Rectangle in OpenCV Python
(1 answer)
Closed 2 years ago.
I been messing with Python and OpenCV on some images, and kind of got the result I want. Here is an example of the output image.
So the important part is opencv has draw red rectangles around the objects it has identified (I don't want to share what the original image was so yes those gray patches are all the remain after running a lot of functions on the image).
Now what I want to do is expand the rectangles (there are 2 here, but 3 or 4 are also possible) so they are as large as possible without touching. I just can't seem to come up with any kind of algorithm to do that. Any suggestions?

Find pixel offset for active plot area [duplicate]

This question already has answers here:
Incorrect rectangle location in matplotlib
(2 answers)
Clickable matrix image with python and matplotlib [duplicate]
Closed 8 years ago.
I saved multiple matrix images using
imshow
savefig
to disk and now I would like to detect the offset between the picture frame and the first pixel.
My problem is that I have to use labels around the plot area which moves the real data view around based on the label text length -> offsets become arbitrary.
Hence, I would like to automatically detect the offset between the upper left corner in the image and the upper left corner of the active plot area + the width & height used to plot one matrix entry.
I guess the parameter must be somewhere buried in the
axes.transData
axes.transAxes
properties (?), but I seem not to find it.
Thanks,
EL

Categories