前言
gif图制作流程
1)获取当前的工作路径
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;">path = os.getcwd()<br />print(path)</span></section>
2)获取当前工作路径下的文件列表
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;">file_list = os.listdir(path)<br />print(file_list)</span></section>
3)复制一个列表
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;">png_list = file_list.copy()<br />print(png_list)</span></section>
4)移除结尾不是png的文件,只保留结尾都是png的文件
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;"><span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">for</span> file <span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">in</span> file_list:<br /> <span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">if</span> file[<span style="font-size: 13px;color: rgb(184, 215, 163);line-height: 26px;">-3</span>:]!= <span style="font-size: 13px;color: rgb(214, 157, 133);line-height: 26px;">"png"</span>:<br /> png_list.remove(file)<br />print(png_list)</span></section>
5)将图片文件中的数字,升序排列
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;">png_list.sort(key=<span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">lambda</span> x: float(x[:<span style="font-size: 13px;color: rgb(184, 215, 163);line-height: 26px;">-4</span>]))<br />png_list</span></section>
6)拼接得到每张图片的绝对路径
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;"><span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">for</span> png <span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">in</span> png_list:<br /> image_path = os.path.join(path, png)<br /> print(image_path)</span></section>
7)获取每张照片的RGB通道值,将7张照片的RGB通道值,保存在列表中
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;"><span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">for</span> png <span style="font-size: 13px;color: rgb(86, 156, 214);line-height: 26px;">in</span> png_list:<br /> image_path = os.path.join(path, png)<br /> frames.append(imageio.imread(image_path))<br />print(len(frames))</span></section>
8)将图片保存为gif图,设置了切换时间
<section style="overflow-x: auto;padding: 15px 16px 16px;color: rgb(220, 220, 220);display: -webkit-box;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;font-size: 12px;background: rgb(30, 30, 30);border-radius: 5px;margin-left: 0px;margin-right: 0px;"><span style="font-size: 13px;">gif_path = os.path.join(path, <span style="font-size: 13px;color: rgb(214, 157, 133);line-height: 26px;">"my_gif.gif"</span>)<br />imageio.mimsave(gif_path, frames, <span style="font-size: 13px;color: rgb(214, 157, 133);line-height: 26px;">'GIF'</span>, duration=DURATION)</span></section>
本篇文章来源于: 菜鸟学Python
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
内容反馈