Search code examples
python-2.7savegifpyglet

Save image or animation in pyglet


I have an image saving issue. I need to take input of either a bmp, jpg, png, or gif and display it. The display part is fairly easy, the hard part is saving it. I want to take the user-supplied file and save it in a zipped directory that's going to be the save file for this project. I see that I can do pygletimage.save(filename) and get a png file. I'm fine with the format change for still images. The question is, how can I save a gif file easily in pyglet?


Solution

  • I use this function at every frame :

    pyglet.image.get_buffer_manager().get_color_buffer().save(filename)
    

    Here's the complete snippet. It saves whatever you display to numbered image files.

     #---EXPORT --------------------------------------------------------
    
        def save_a_frame(self):
                file_num=str(self.frame_number).zfill(5)
                file_t=str(self.chrono)
                filename="frame-"+file_num+'-@ '+file_t+'sec.png'
                pyglet.image.get_buffer_manager().get_color_buffer().save(filename)
                print 'image file writen : ',filename
    
        def export_loop(self,dt):
            constant_interval=1.0/PicPS
            if self.chrono<END_TIME:
                # update at a constant dt, regardless of real time
                # so that even if refresh is slow no frame should be missing
                # self.frame_draw(PicPS)
                self.update(constant_interval)
                self.frame_draw(dt)
                self.frame_number+=1
                self.save_a_frame()
            else:
                exit()