{"id":1012018,"date":"2024-12-27T11:35:09","date_gmt":"2024-12-27T03:35:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012018.html"},"modified":"2024-12-27T11:35:11","modified_gmt":"2024-12-27T03:35:11","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bbgif","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1012018.html","title":{"rendered":"\u5982\u4f55\u7528python\u753bgif"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085940\/6e739c47-f979-4056-97c0-4a87cc28e8c5.webp\" alt=\"\u5982\u4f55\u7528python\u753bgif\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u8981\u7528Python\u753bGIF\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u5e27\u3001\u4fdd\u5b58\u4e3aGIF\u6587\u4ef6<\/strong>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5b89\u88c5\u4e86PIL\uff08Pillow\uff09\u5e93\uff0c\u56e0\u4e3a\u5b83\u662f\u5904\u7406\u56fe\u50cf\u7684\u5f3a\u5927\u5de5\u5177\u3002\u63a5\u4e0b\u6765\uff0c\u751f\u6210\u8981\u7528\u4e8eGIF\u7684\u5e27\uff0c\u53ef\u4ee5\u901a\u8fc7matplotlib\u7b49\u5e93\u521b\u5efa\u52a8\u6001\u56fe\u50cf\u3002\u6700\u540e\uff0c\u4f7f\u7528Pillow\u5c06\u8fd9\u4e9b\u5e27\u4fdd\u5b58\u4e3aGIF\u6587\u4ef6\u3002\u8be6\u7ec6\u6765\u8bf4\uff0c\u521b\u5efa\u5e27\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u8fd9\u51b3\u5b9a\u4e86GIF\u7684\u5185\u5bb9\u548c\u8d28\u91cf\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u751f\u6210\u4e00\u7cfb\u5217\u56fe\u50cf\uff0c\u8c03\u6574\u6bcf\u4e00\u5e27\u7684\u5185\u5bb9\u4ee5\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u3002\u63a5\u4e0b\u6765\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5\u4e00\u4e9bPython\u5e93\uff0c\u8fd9\u4e9b\u5e93\u5c06\u5e2e\u52a9\u6211\u4eec\u521b\u5efa\u548c\u4fdd\u5b58GIF\u6587\u4ef6\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecPillow\u3001matplotlib\u548cimageio\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff0c\u5e76\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p>pip install matplotlib<\/p>\n<p>pip install imageio<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pillow\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0cmatplotlib\u7528\u4e8e\u751f\u6210\u56fe\u5f62\uff0cimageio\u5219\u662f\u7528\u4e8e\u521b\u5efa\u548c\u5904\u7406\u591a\u5a92\u4f53\u6587\u4ef6\u7684\u5e93\uff0c\u5305\u62ecGIF\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u5e27<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u5e27\u662f\u5236\u4f5cGIF\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u5e27\u662f\u6784\u6210GIF\u52a8\u753b\u7684\u6bcf\u4e00\u4e2a\u9759\u6b62\u753b\u9762\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528matplotlib\u521b\u5efa\u8fd9\u4e9b\u5e27\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528matplotlib\u751f\u6210\u9759\u6001\u56fe<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528matplotlib\u751f\u6210\u4e00\u7cfb\u5217\u9759\u6001\u56fe\u50cf\uff0c\u6bcf\u4e00\u4e2a\u56fe\u50cf\u90fd\u662f\u4e00\u4e2a\u5e27\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u6b63\u5f26\u6ce2\u52a8\u753b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>frames = []<\/p>\n<p>x = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>for i in range(10):<\/p>\n<p>    plt.figure()<\/p>\n<p>    y = np.sin(x + i * 0.1)<\/p>\n<p>    plt.plot(x, y)<\/p>\n<p>    plt.xlim(0, 2 * np.pi)<\/p>\n<p>    plt.ylim(-1, 1)<\/p>\n<p>    plt.title(f&quot;Frame {i}&quot;)<\/p>\n<p>    plt.savefig(f&quot;frame_{i}.png&quot;)<\/p>\n<p>    frames.append(f&quot;frame_{i}.png&quot;)<\/p>\n<p>    plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u52a8\u6001\u751f\u6210\u5e27\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u52a8\u6001\u751f\u6210\u6570\u636e\uff0c\u4ee5\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u3002\u6839\u636e\u9700\u8981\u8c03\u6574\u6bcf\u4e00\u5e27\u7684\u5185\u5bb9\uff0c\u6bd4\u5982\u5728\u65f6\u95f4\u8f74\u4e0a\u79fb\u52a8\u6ce2\u5f62\uff0c\u6539\u53d8\u989c\u8272\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4fdd\u5b58\u4e3aGIF\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u751f\u6210\u6240\u6709\u5e27\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u5c06\u8fd9\u4e9b\u5e27\u7ec4\u5408\u6210GIF\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>images = [Image.open(frame) for frame in frames]<\/p>\n<p>images[0].save(&#39;animation.gif&#39;, save_all=True, append_images=images[1:], loop=0, duration=100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>save_all=True<\/code>\u8868\u793a\u4fdd\u5b58\u6240\u6709\u5e27\uff0c<code>append_images<\/code>\u7528\u4e8e\u5c06\u5176\u4ed6\u5e27\u9644\u52a0\u5230\u7b2c\u4e00\u5e27\u4e4b\u540e\uff0c<code>loop=0<\/code>\u8868\u793a\u65e0\u9650\u5faa\u73af\u64ad\u653eGIF\uff0c<code>duration=100<\/code>\u6307\u5b9a\u6bcf\u5e27\u7684\u663e\u793a\u65f6\u95f4\uff08\u6beb\u79d2\uff09\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f18\u5316GIF\u6587\u4ef6<\/p>\n<\/p>\n<ol>\n<li>\u51cf\u5c11\u5e27\u6570<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u751f\u6210\u7684GIF\u6587\u4ef6\u8fc7\u5927\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u5e27\u6570\u6765\u4f18\u5316\u6587\u4ef6\u5927\u5c0f\u3002\u9009\u62e9\u5173\u952e\u5e27\uff0c\u4e22\u5f03\u4e0d\u5fc5\u8981\u7684\u8fc7\u6e21\u5e27\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8c03\u6574\u56fe\u50cf\u5c3a\u5bf8<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u964d\u4f4e\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u6765\u51cf\u5c11\u6587\u4ef6\u5927\u5c0f\u3002\u53ef\u4ee5\u5728\u751f\u6210\u5e27\u65f6\u8c03\u6574\u56fe\u50cf\u7684\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(4, 4))  # \u8c03\u6574\u56fe\u50cf\u5c3a\u5bf8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u8c03\u8272\u677f<\/li>\n<\/ol>\n<p><p>Pillow\u652f\u6301\u4f7f\u7528\u8c03\u8272\u677f\u6765\u51cf\u5c11GIF\u7684\u989c\u8272\u6df1\u5ea6\uff0c\u4ece\u800c\u51cf\u5c0f\u6587\u4ef6\u5927\u5c0f\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">images[0].convert(&#39;P&#39;, palette=Image.ADAPTIVE, colors=256).save(&#39;optimized_animation.gif&#39;, save_all=True, append_images=[img.convert(&#39;P&#39;, palette=Image.ADAPTIVE, colors=256) for img in images[1:]], loop=0, duration=100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528imageio\u5e93\u7b80\u5316\u6d41\u7a0b<\/p>\n<\/p>\n<p><p>\u9664\u4e86Pillow\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528imageio\u5e93\u6765\u7b80\u5316\u521b\u5efaGIF\u7684\u6d41\u7a0b\u3002imageio\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7b80\u5355\u6613\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import imageio<\/p>\n<p>with imageio.get_writer(&#39;animation.gif&#39;, mode=&#39;I&#39;, duration=0.1) as writer:<\/p>\n<p>    for frame in frames:<\/p>\n<p>        image = imageio.imread(frame)<\/p>\n<p>        writer.append_data(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>imageio\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u8bfb\u53d6\u548c\u5199\u5165GIF\u6587\u4ef6\uff0c<code>get_writer<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2aGIF\u6587\u4ef6\uff0c<code>append_data<\/code>\u65b9\u6cd5\u7528\u4e8e\u6dfb\u52a0\u5e27\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u751f\u6210GIF\u6587\u4ef6\u4e3b\u8981\u6d89\u53ca\u5230\u5e27\u7684\u521b\u5efa\u548c\u4fdd\u5b58\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528Pillow\u548cmatplotlib\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u52a8\u753b\u6548\u679c\u3002\u4f18\u5316GIF\u7684\u5927\u5c0f\u548c\u8d28\u91cf\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u5e27\u6570\u3001\u56fe\u50cf\u5c3a\u5bf8\u548c\u989c\u8272\u6df1\u5ea6\u5b9e\u73b0\u3002\u5efa\u8bae\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u53c2\u6570\u548c\u65b9\u6cd5\uff0c\u4ee5\u5e73\u8861\u6587\u4ef6\u5927\u5c0f\u548c\u52a8\u753b\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u751f\u6210\u5e76\u4f18\u5316GIF\u6587\u4ef6\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3001\u52a8\u753b\u6f14\u793a\uff0c\u8fd8\u662f\u5176\u4ed6\u5e94\u7528\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u52a8\u753bGIF\uff1f<\/strong><br \/>\u4f7f\u7528Python\u521b\u5efa\u52a8\u753bGIF\u901a\u5e38\u6d89\u53ca\u51e0\u4e2a\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684\u5e93\uff0c\u5982Pillow\u548cimageio\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5e93\u52a0\u8f7d\u56fe\u50cf\uff0c\u8bbe\u7f6e\u56fe\u50cf\u7684\u5e27\u548c\u6301\u7eed\u65f6\u95f4\uff0c\u6700\u540e\u4fdd\u5b58\u4e3aGIF\u683c\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5feb\u901f\u5165\u95e8\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport imageio\n\n# \u521b\u5efa\u56fe\u50cf\u5e27\nframes = []\nfor i in range(10):\n    img = Image.new(&#39;RGB&#39;, (100, 100), color=(i * 25, 100, 100))\n    frames.append(img)\n\n# \u4fdd\u5b58\u4e3aGIF\nimageio.mimsave(&#39;animation.gif&#39;, frames, duration=0.5)\n<\/code><\/pre>\n<p><strong>\u751f\u6210GIF\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u751f\u6210GIF\u65f6\uff0c\u56fe\u50cf\u7684\u5927\u5c0f\u548c\u8d28\u91cf\u662f\u5173\u952e\u56e0\u7d20\u3002\u8f83\u5927\u7684GIF\u6587\u4ef6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u52a0\u8f7d\u7f13\u6162\uff0c\u5efa\u8bae\u5728\u521b\u5efaGIF\u65f6\u8003\u8651\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u548c\u5e27\u6570\u3002\u6b64\u5916\uff0cGIF\u7684\u989c\u8272\u9650\u5236\u5728256\u79cd\uff0c\u56e0\u6b64\u5728\u5904\u7406\u9ad8\u8272\u5f69\u56fe\u50cf\u65f6\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u8272\u5f69\u4f18\u5316\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u751f\u6210GIF\u52a8\u753b\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u751f\u6210GIF\u52a8\u753b\u3002Pillow\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u9002\u5408\u57fa\u672c\u7684\u56fe\u50cf\u64cd\u4f5c\u3002imageio\u4e5f\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9009\u62e9\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002\u5176\u4ed6\u5e93\uff0c\u5982matplotlib\uff0c\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u5e76\u751f\u6210GIF\u52a8\u753b\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\u548c\u56fe\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316GIF\u52a8\u753b\u4ee5\u51cf\u5c0f\u6587\u4ef6\u5927\u5c0f\uff1f<\/strong><br \/>\u4f18\u5316GIF\u52a8\u753b\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u5e27\u6570\u3001\u964d\u4f4e\u5206\u8fa8\u7387\u548c\u4f7f\u7528\u8c03\u8272\u677f\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Pillow\u7684<code>save<\/code>\u65b9\u6cd5\u4e2d\u7684<code>optimize=True<\/code>\u53c2\u6570\u6765\u81ea\u52a8\u4f18\u5316GIF\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5de5\u5177\u5982GIMP\u6216\u5728\u7ebfGIF\u538b\u7f29\u5668\u4e5f\u662f\u51cf\u5c0f\u6587\u4ef6\u5927\u5c0f\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u8981\u7528Python\u753bGIF\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u5e27\u3001\u4fdd\u5b58\u4e3aGIF\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5b89\u88c5\u4e86P [&hellip;]","protected":false},"author":3,"featured_media":1012023,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012018"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1012018"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012018\/revisions"}],"predecessor-version":[{"id":1012025,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012018\/revisions\/1012025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012023"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}