I am currently working on creating a paint program using python and pygame.  I am currently having trouble with creating the undo/redo function in the program.  The way I was thinking of doing so would be to save the canvas image after each time the user releases the mouse, but I am not sure if the individual images would have to be saved in a temporary folder that is deleted after the program is closed.  I have also read that this method can affect performance of the program so I am wondering if there are any other methods that will work more efficiently.  Thank you.
My suggestion is to have a buffer of the last operations that have been done. Each operation will consist of a sprite, and a position of where it is placed. 
You will be drawing the canvas, as well as all sprites from that buffer. When you have to many sprites in the buffer, you can blit the oldest onto the canvas, thus saving memory.
The undo itself would be rather easy. Just remove the last sprite that was added.
A redo would be slightly more difficult. Since instead of removing, I would have a pointer, that points to the last sprite that I will draw. If a new action will be added, only then I remove all the sprites that have been "invisible".
Paint Program in Python - Undo and Redo