2014-12-31 24 views
0

我正在使用撤销重做功能创建绘图程序。我通过拍摄画布快照并将它们保存为jpeg文件来实现它。然后,我将文件的名称添加到列表中以供稍后获取。我如何使这些jpeg文件临时?我应该使用什么模块?我正在使用Python 3.2.5。感谢您的帮助!绘制程序使用临时文件撤销重做功能?

下面是一些我的代码

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) 
+0

保留一个用于撤消重做的jpg集合非常昂贵。如果您有时间,我建议您阅读Bertrand Meyer的[“面向对象的软件构造”](http://www.amazon.com/Object-Oriented-Software-Construction-CD-ROM-Edition/dp/0136291554)第21章(继承案例研究:在交互式系统中“撤消” )实现撤销重做。它会给你一个简单而有效的方法来实现它,只保留差异而不是每个变化的整个文件。 – avenet

+0

如果你想保存图像,只需抓住[Pillow](https://pypi.python.org/pypi/Pillow/2.6.1)这是[PIL](http:// www .pythonware.com/products/pil /)for Python 3.x. – avenet

回答

1

请参阅制作临时文件和目录的临时文件模块。

至于撤消,首先考虑在文本编辑器中撤销/重做。对于每个文本更改,都会有一个反向更改来解除更改。这些是你保存的内容,而不是整个文件的快照。对于大块更改,撤消可能比典型的“差异”更紧凑。对于块替换(例如用粘贴代替选择时执行的块),块开始和旧文本需要保存,但仅保存新文本的长度。当执行这种撤消操作时,可以通过从正在恢复的文本缓冲区中检索新文本,将其变为重做。必须决定是否允许“无限”撤销(直到内存用完)或设置固定限制。对于二维图形,设置((x,y),get(x,y))撤销设置((x,y),颜色)。除此之外,由于像素数量远大于字符及其平面而不是线性组织,事情往往会更复杂。但是上述原则仍然适用。