Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python PIL时优化节省时间_Python_Python Imaging Library_Png_Jpeg - Fatal编程技术网

使用Python PIL时优化节省时间

使用Python PIL时优化节省时间,python,python-imaging-library,png,jpeg,Python,Python Imaging Library,Png,Jpeg,我正在使用PIL(用树莓上的Python 3)读取背景文件(PNG,1920x1080),向其中添加一些文本元素,然后将其保存为另一个具有相同格式的文件 这个过程有点时间紧迫,新文件保存到Raspberry的SD卡越快越好 代码非常基本: img = Image.open('background.png') draw = ImageDraw.Draw(img) draw.text((100, 100, 'test',myColor,myFont) img.save('newfile.png')

我正在使用PIL(用树莓上的Python 3)读取背景文件(PNG,1920x1080),向其中添加一些文本元素,然后将其保存为另一个具有相同格式的文件

这个过程有点时间紧迫,新文件保存到Raspberry的SD卡越快越好

代码非常基本:

img = Image.open('background.png')
draw = ImageDraw.Draw(img)
draw.text((100, 100, 'test',myColor,myFont)
img.save('newfile.png')
我已经对这个过程进行了计时,执行最后一行并保存文件需要3到4秒的时间

有没有办法在不牺牲质量(分辨率、颜色等)的情况下优化速度

现在我要测试JPG而不是PNG

提前谢谢

编辑:


最初的计时是运行Python3解释器。编译我的脚本(pyinstaller--onefile myscript.py)后,保存时间将下降到2.2秒。

我希望这对将来的人有用:将图像文件格式从PNG切换到JPG将保存FullHD文件(1920 x 1080 px)的时间缩短到不到1秒