is deleting image from display surface possible without overdrawing it? (pygame) - python

i want to load an image for 5 seconds and then delete it from the display surface without overwriting it.
I load my image with this function:
pygame.image.load('image.png')
is there a function that undo this in python?

No, once you change a Surface, it remains change.
An easy solution is to just create a copy of the target Surface before you change it (using .copy()) and, if the Surface in question is the root Surface, simply blit the copy back onto it.

Related

Is there a function in Pygame to save a portion of the screen as an image? [duplicate]

This question already has answers here:
How can I crop an image with Pygame?
(4 answers)
Closed 1 year ago.
I am working on making a drawing program in Pygame. All features are basically complete except for the ability to save your image. Is there any built in Pygame function that does this, or will I have to find some other way (E.G. taking a screenshot or something).
screen is a Surface object which has method subsurface(Rect) to get part of surface - and this part will be also a Surface object.
And there is function pygame.image.save(surface, filename)
Other method: at start create smaller Surface and draw on this surface (instead of drawing directly on screen) and blit() this surface on screen to display it and save() this surface to file.
You can even use many surfaces and keep them on list to create function Undo. When you use new tool then you duplicate surface and put it on list. When you use Undo then you get last surface from list.
If it's a drawing game, then you'll probably find it very easy to manually make the image.
I know it sounds scary, but it really isn't, especially in python.
You can use PyPng to convert a 2D array into an image, as I assume you already use a 2D array to store the drawing

OpenGL export object as .png [duplicate]

I am developing some software in pygame that allows a user to draw things on a canvas using different colors, pen sizes, etc. The problem is I don't know how to save the thing they have drawn as an image (e.g. png).
I need a way to frame the area where the image is on the screen and save it to the device's file system. Does anyone know how I can do this?
The PyGame display (window) is associated to a pygame.Surface object. Use pygame.image.save() to store the content of a Surface (or the screen) to a bitmap. The file type is automatically determined by the file extension:
pygame.image.save(screen, "my_file.png")
If you just want to save a section of the screen, you can define a subsurface that is directly linked to the source surface with the method subsurface:
subsurface(Rect) -> Surface
Returns a new Surface that shares its pixels with its new parent. The new Surface is considered a child of the original. Modifications to either Surface pixels will effect each other.
Create a sub-surface and save it to a file:
rect_area = pygame.Rect(x, y, width, height)
area_surf = screen.subsurface(rect_area)
pygame.image.save(area_surf, "my-file.png")

How to delete drawn objects with OpenCV in Python?

How to delete drawn objects with OpenCV in Python ?
I draw objects on click (cv2.rectangle, cv2.circle) ...
Then I would like to delete only drawn objects.
I know that i need to make a layer in behind of the real image and to draw on another one.
But I do not know how to implement this in code.
Have a method or something that when it's executed, will replace the image with stuff drawn on it with an original unaltered image. It's best to create a clone of your original image to draw on.

Temporary images with Pyglet

Is there a way to make a temporary "image" with Pyglet? (Something akin to LÖVE's Canvas).
Basically, I want to have an object that I could blit stuff like sprites and text to, and then blit this temporary image to the window.
I tried creating an image with pyglet.image.create(), but apparently it procures an ImageData which you can't blit to.
Thank you very much for your attention.
Checkout AbstractImage. It has all the methods I think you want - creating an image offline, blitting stuff to it:
myimage.blit_into('sprite1',x,y,z)
myimage.blit_into('sprite2',x+20,y,z)
and then blitting it to the active frame:
myimage.blit(x,y[,z])
etc.

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.

Categories