pycairo surface.write_to_png太慢
我必须制作一个与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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论