pycairo surface.write_to_png太慢

发布于 2025-02-14 02:00:01 字数 2129 浏览 2 评论 0原文

我必须制作一个与Pycairo创建各种图像的项目,然后在FFMPEG中使用所有这些图像来制作视频。但是问题在于方法cairo.surface.write_to_png太慢了,因此代码需要将近1分钟的时间才能运行。而且我找不到任何可以使程序更快的东西。有人可以帮我吗?

这是正在减慢代码的方法:

    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() 

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() 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文