0

I'm creating a paint program with an undo redo function. I'm implementing it by taking snapshots of the canvas and then saving them as jpeg files. I'm then add the name of the file to a list to be fetched later. How do i make these jpeg files temporary? What module should i use? I'm using python 3.2.5. thank you for your help!

Here's some of my code

    if mb[0]==0 and count==1:                       #if a drawing was made 
            count1+=1                                   #my filename assigner
            fileName=str(count1)+".jpg"                 #creates fileName
            image.save(screen.subsurface(touch),fileName)#saves image
            nowlist.append(fileName)                    #adds to nowlist
            count=0                                     #resets counter
            if len(nowlist)>1:                          #removes old data
                undolist.append(nowlist[0])             #adds it to undolist
                del(nowlist[0])                         #dels it
            print("nowlist :",nowlist)                  #prints lists
            print("undolist :",undolist)
    if undoRect.collidepoint((mx,my)) and mb[0]==1: #if mouse is over and clicks undo
        if len(undolist)>0:                         #if there are at least 1 item 
            fileName=undolist[-1]                   #last item of undolist
            canvas1=image.load(fileName)            #fetch file image
            canvas2=screen.blit(canvas1,[150,150])  #blit it onto the canvas
            nowlist.append(fileName)                #adds it to the canvas list
            if len(nowlist)>1:                      #ensures there is only 1 item on the canvas
                redolist.append(nowlist[0])
                del(nowlist[0])                              
            del(undolist[-1])                       
            print("nowlist :",nowlist)              
            print("undolist :",undolist)            #prints undolist
            print("redolist :",redolist)            #prints redolist
    if redoRect.collidepoint((mx,my)) and mb[0]==1: #if mouse is over and clicks redo
        if len(redolist)>0:                         #if there is at least 1 item 
            fileName=redolist[-1]                   #take last term
            canvas1=image.load(fileName)            #fetch file of that name
            canvas2=screen.blit(canvas1,[150,150])  #blit it onto the canvas
            nowlist.append(fileName)                #append it to the nowlist,
            if len(nowlist)>1:                      #removes what was on the canvas
                undolist.append(nowlist[0])         #adds it to the undo list
                del(nowlist[0])                     #del from nowlist
            del(redolist[-1])                       
            print("nowlist :",nowlist)              #prints the lists    
            print("undolist :",undolist)
            print("redolist :",redolist)
  • Keeping a collection of jpg for undo-redo is really expensive. If you have the time, I recommend you to read Bertrand Meyer's ["Object Oriented Software Construction"](http://www.amazon.com/Object-Oriented-Software-Construction-CD-ROM-Edition/dp/0136291554) chapter 21 (Inheritance case study: “undo” in an interactive system) on implementing an undo-redo. It will give you a simple and efficient way to implement it by keeping only the differences and not the whole files for every change. – avenet Dec 31 '14 at 22:56
  • If you want to save the images, just grab your copy of [Pillow](https://pypi.python.org/pypi/Pillow/2.6.1) which is a port of [PIL](http://www.pythonware.com/products/pil/) for Python 3.x. – avenet Dec 31 '14 at 23:02

1 Answers1

1

See the tempfile module for making temporary files and directories.

As for undo, start by thinking about undo/redo in a text editor. For each text change, there is an inverse change that undoes the change. Those are what you save, not a snapshot of the whole file. For large block changes, the undo may be more compact than a typical 'diff'. For a block replacement (such as performed when replacing a selection with a paste), the block start and old text need to be saved but only the length of new text. When such an undo is performed, it can be turned into a redo by retrieving the new text from the text buffer being reverted. One has to decide whether to allow 'infinite' undo (until memory runs out) or set a fixed limit.

For 2-D graphics, set((x,y), get(x,y)) undoes set((x,y), color). Beyond that, things tend to be more complicated due to the much larger number of pixels than characters and their planar rather than linear organization. But the principles above still apply.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52