PyCairo surface.write_to_png too slow

Viewed 33

I have to make a project that creates various images with pycairo and then uses all those images in ffmpeg to make a video. But the problem is that the method cairo.surface.write_to_png is too slow, so the code takes almost 1 minute to run. And i'm not finding anything that could make the program faster. Can someone help me ?

This is the method that is slowing the code:

    def rectangle(self, xstart, ystart, xfinish, yfinish, linewidth=5, outcolor=(0, 0, 0, 1), 
                  fillcolor=(0, 0, 0, 0), steps=1000, animation=0):
        ctx = self.context
        w = (xfinish-xstart) if xfinish >= xstart else (xstart-xfinish)
        h = (yfinish-ystart) if yfinish >= ystart else (ystart-yfinish)
        if fillcolor != (0, 0, 0, 0):
            ctx.set_source_rgba(fillcolor[0], fillcolor[1], fillcolor[2], fillcolor[3])
            ctx.fill_preserve()
        ctx.set_source_rgba(outcolor[0], outcolor[1], outcolor[2], outcolor[3])
        ctx.set_line_width(linewidth)
        if self.animate:
            c = 0
            i = 0
            clist, ilist = [], []
            cstep = w/steps
            istep = h/steps
            count = 0
            try:
                lastf = os.listdir('C:/Users/ferna/PycharmProjects/Draw/test')[-1][5:8]
            except IndexError:
                lastf = 0
                pass
            while True:
                if count != 0:
                    ctx.save()
                    ctx.set_source_rgba(self.background[0], self.background[1], 
                                        self.background[2], self.background[3])
                    ctx.set_line_width(linewidth+2)
                    ctx.rectangle(xstart, ystart, c-cstep, i-istep)
                    ctx.fill_preserve()
                    ctx.stroke()
                    ctx.restore()
                ctx.rectangle(xstart, ystart, c, i)
                ctx.stroke()
                self.surface.write_to_png(os.fsdecode(f'C:/Users/ferna/PycharmProjects/Draw/oi/teste{count + int(lastf):0>3.0f}.png'))
                clist.append(c)
                ilist.append(i)
                c += cstep
                i += istep
                count += 1
                if c > w and i > h:
                    break
        ctx.rectangle(xstart, ystart, w, h)
        ctx.stroke() 
0 Answers
Related