How to fix OOP Issue with opacity change in pygame [duplicate] - python-3.x

This question already has answers here:
How to convert the background color of image to match the color of Pygame window?
(1 answer)
How do I make the screen ignore the background color of some image?
(1 answer)
Pygame image transparency confusion
(1 answer)
Closed 1 year ago.
I'm trying to make pieces of what makes pygame into it's own separate classes/definitions. But when trying to set the opacity of an image, Instead of showing up as excepted, It starts from it's set opacity and lightens up until it reaches the maximum opacity possible.
I have tried to see if the opacity changes in the module that contains the game, and in the module that contains the classes. Yet they end up giving me the correct number that should be the number that is set for the opacity. And yes I have looked for anything that could possibly start adding up the opacity by mistake.
This is the piece of the code that i'm using to edit an image.
class PicEditor:
def scale(Pic,scaleX,scaleY):
Pic.blit = pygame.transform.scale(Pic.blit,(scaleX,scaleY))
def opacity(Pic,opc):
Pic.blit.set_alpha(opc)
print(opc) #attempt at figuring out the problem
And this is the code that i'm trying to run.
while not crashed:
display.gameLoopTop() #the usual game loop used ontop in pygame
PicEditor.opacity(templatePic,opacityTemplate) #the issue
PicEditor.scale(templatePic,displayX,displayY) #scaling an image (works fine)
templatePic.blitIMG() #bliting the image
display.gameLoopBottom() #the bottom of a game loop which includes the clock function and pygame display update
pygame.quit()
exit()
The expected results are that the image will change it's opacity by 50 (opacityTemplate = 50), then that the image will fit the size of the screen (displayX,displayY) and then that it is blited.
Everything runs perfectly, except that the opacity of the image goes from it's number given- 50, and adds another 50 until it reaches the maximum opacity possible without crashing.

Related

How can I get the color of a pixel on screen with Node.js or C?

I am trying to get the color of a pixel on my screen using node.js. I want it to be returned in RGB format, e.g. (255, 0, 0). My current solution is to use screenshot-desktop to screenshot my entire screen in JPG format, decode it to get the raw pixel data, and get the color of a given pixel. However, this lags out my entire computer for 1-2 seconds as it is taking the screenshot. This is unusable as I would like to do this multiple times per second. So my question is: How can I get the color of a given pixel on the screen, without taking a full screenshot?
I am using Linux with X11. There is an X11 library for node.js, so I asssume I should use that to get the pixel color, I'm just not sure how. If you could show me how to do it in C then I can easily use node.js to do the same thing.
Thanks!
Oh my gosh I just figured it out after posting this. I was using robotjs for reading the mouse position and I totally forgot it can do screen stuff too! So, the solution would be to do
var robot = require('robotjs');
var color = robot.getPixelColor(x, y);
X11 solution using x11 node library ( I am the author ):
query windows tree with QueryTree starting at the root window
get every child geometry using GetGeometry request
if your point is not inside any child, use current window id and get 1x1 pixmap from the current image: GetImage(format, currentWindow, x, y, 1, 1, planeMask) ( 2 for format and 0xffffffff for plane mask should work ). Make sure you calculate relative x y position as you travers windows tree.
if child window covers your point query children for that window and repeat again. Note that QueryTree returns windows in bottom to top stacking order so make sure you pick last one covering your point
Once you have 1x1 pixmap from the topmost window under your point - the buffer should contain only color bytes for your image, RGB order and bit mask might depend on red_mask, green_mask, blue_mask from display.screen[0].depths[visual].
If you cache "topmost window" between requests and only start from root when no match anymore the above solution might be much more performant then the one using robotjs ( although much more low level and complicated ) Good luck!

SVG animate tag causes undesired put back [duplicate]

This question already has an answer here:
XML SVG - persist the end state of an animation
(1 answer)
Closed 3 years ago.
I am using ANIMATE tag to add some animation to my SVG path. I just set the FROM and TO coordinates, with no loop at all.
It works well but when my path morphs to the final shape it puts back instantly to the beginning "frame" instead of remain in the final shape.
I don't get any error messages, curve just waits, morphs and then goes back to beginning shape.
I just found out that adding a "fill='freeze'" additional attribute does the job

Recognizing overlapping objects

I am processing an image with OpenCV on Python and I want to count every objects (worms) on it. Worms are rather light beige whereas the background is black (see picture) so it is rather easy to distinguish them. The problem is that sometimes worms are too close to each other (sometimes they even overlap) and cv.findContours() will draw one big contour instead of two smaller ones (see picture below).
Because I am using cv.foundContours(), I have to first turn the picture into black and white, then blur it (optional) and finally threshold it in order to have white worms in a black background.
I am using the following code :
import cv2 as cv
img = cv.imread('worms.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
blur=cv.GaussianBlur(gray,(5,5),1)
ret,osu = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
imsource,contours,test = cv.findContours(osu,1,1)
cv.drawContours(img,contours,-1, (0,0,255),2)
I tried to erode the thresholded picture but it doesn't work well since the "bond" between two worms is quite big.
Thanks for the help

Make canvas background transparent - python

So I am making a Frogger game, but have run into a problem. For the collision detection, I am using the following to check if one Tkinter canvas object is overlapping another:
canvas.find_overlapping(*canvas.bbox(imageObj))
However, I made the background a canvas object as well:
background = self.canvas.create_image(0, 0, image = self.imageData["Background"], anchor = "nw")
So the program is detecting a collision between the player and an object 24/7. Is there any way around this? I searched SO and tried putting the background in a label, but when I packed the canvas over it the background disappeared (probably because the canvas was covering it).
I can't find a way to make the canvas transparent without making the objects on it transparent as well. I also do not want to calculate the x and y boxes of each object, as that is just cumbersome and unreliable.
If someone could suggest another way, that would be awesome.
The find_overlapping method returns a list of items. Just cycle through the list and ignore the background item.

How to save images drawn in Python turtle module that are outside screen area

I am new to Python. I wrote a Python code using turtle module, but when i am trying to save the image, the part of the image that is visible in the screen is only getting saved, the parts that are outside are getting cropped. I am saving using the following command:
turtle.getcanvas().postscript(file = "filename.eps")
I have also tried to resize the turtle screen using turtle.screensize() to make it bigger than the drawn image, but then also the part of the image that was saved was the part visible in the screen only.
Please help,
Try setting the height and width arguments.
turtle.getscreen().getcanvas().postscript(file='outputname.ps', height=10000,
width=10000)

Resources