{"id":1058945,"date":"2024-12-31T15:20:59","date_gmt":"2024-12-31T07:20:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058945.html"},"modified":"2024-12-31T15:21:01","modified_gmt":"2024-12-31T07:21:01","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86eps%e8%bd%ac%e5%8c%96%e4%b8%bapng","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1058945.html","title":{"rendered":"python\u5982\u4f55\u5c06eps\u8f6c\u5316\u4e3apng"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/81ef6e27-977c-49f7-9218-19b558b0de64.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06eps\u8f6c\u5316\u4e3apng\" \/><\/p>\n<p><p> <strong>Python\u5c06EPS\u8f6c\u5316\u4e3aPNG\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528Ghostscript\u548cImageMagick\u3001\u4f7f\u7528Matplotlib\u5e93\u3002<\/strong> \u5728\u8fd9\u91cc\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u8f6c\u6362\u3002PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u652f\u6301\u8bb8\u591a\u6587\u4ef6\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PIL\u5e93\uff08Pillow\uff09<\/h3>\n<\/p>\n<p><p>PIL\u5e93\uff08\u73b0\u4e3aPillow\uff09\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u56fe\u50cf\u5904\u7406\u5e93\u3002Pillow\u662fPIL\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5e76\u4e14\u652f\u6301Python 3.x\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pillow\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8f6c\u6362EPS\u4e3aPNG<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06EPS\u8f6c\u6362\u4e3aPNG\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>def convert_eps_to_png(eps_file, png_file):<\/p>\n<p>    # Open EPS file<\/p>\n<p>    image = Image.open(eps_file)<\/p>\n<p>    # Convert EPS to PNG<\/p>\n<p>    image.save(png_file, &#39;PNG&#39;)<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>eps_file = &#39;example.eps&#39;<\/p>\n<p>png_file = &#39;example.png&#39;<\/p>\n<p>convert_eps_to_png(eps_file, png_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Image.open<\/code>\u65b9\u6cd5\u6253\u5f00EPS\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>save<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u4e3aPNG\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Ghostscript\u548cImageMagick<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Pillow\u5e93\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06EPS\u8f6c\u6362\u4e3aPNG\uff0c\u6bd4\u5982\u4f7f\u7528Ghostscript\u548cImageMagick\u3002\u8fd9\u4e9b\u5de5\u5177\u540c\u6837\u5f3a\u5927\uff0c\u5e76\u4e14\u5728\u5904\u7406\u9ad8\u5206\u8fa8\u7387\u56fe\u50cf\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Ghostscript\u548cImageMagick<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Ghostscript\u548cImageMagick\u3002\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># Install Ghostscript<\/p>\n<p>sudo apt-get install ghostscript<\/p>\n<h2><strong>Install ImageMagick<\/strong><\/h2>\n<p>sudo apt-get install imagemagick<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8f6c\u6362EPS\u4e3aPNG<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def convert_eps_to_png(eps_file, png_file):<\/p>\n<p>    # Use Ghostscript to convert EPS to PNG<\/p>\n<p>    subprocess.run([&#39;gs&#39;, &#39;-dNOPAUSE&#39;, &#39;-dBATCH&#39;, &#39;-sDEVICE=pngalpha&#39;, &#39;-r144&#39;, &#39;-sOutputFile=&#39; + png_file, eps_file])<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>eps_file = &#39;example.eps&#39;<\/p>\n<p>png_file = &#39;example.png&#39;<\/p>\n<p>convert_eps_to_png(eps_file, png_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8c03\u7528Ghostscript\u547d\u4ee4\u884c\u5de5\u5177\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u7ed8\u5236\u56fe\u8868\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8f6c\u6362EPS\u4e3aPNG<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06EPS\u8f6c\u6362\u4e3aPNG\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<p>def convert_eps_to_png(eps_file, png_file):<\/p>\n<p>    # Read EPS file<\/p>\n<p>    image = mpimg.imread(eps_file, format=&#39;eps&#39;)<\/p>\n<p>    # Save as PNG file<\/p>\n<p>    plt.imsave(png_file, image)<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>eps_file = &#39;example.eps&#39;<\/p>\n<p>png_file = &#39;example.png&#39;<\/p>\n<p>convert_eps_to_png(eps_file, png_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>mpimg.imread<\/code>\u65b9\u6cd5\u8bfb\u53d6EPS\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>plt.imsave<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u4e3aPNG\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4f7f\u7528Pillow\u5e93\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u9ad8\u5206\u8fa8\u7387\u56fe\u50cf\u6216\u9700\u8981\u66f4\u591a\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Ghostscript\u548cImageMagick\u3002\u6b64\u5916\uff0cMatplotlib\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u8f6c\u6362\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>Pillow<\/code>\u5e93\u548c<code>Ghostscript<\/code>\u5de5\u5177\u6765\u5b8c\u6210EPS\u5230PNG\u7684\u8f6c\u6362\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8fd9\u4e24\u4e2a\u5de5\u5177\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u5b9e\u73b0\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00EPS\u6587\u4ef6\u5e76\u8f6c\u6362\u4e3aPNG\nimage = Image.open(&#39;example.eps&#39;)\nimage.save(&#39;example.png&#39;, &#39;PNG&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u8fd0\u884c\u4ee3\u7801\u4e4b\u524d\u5df2\u5b89\u88c5\u6240\u9700\u7684\u5e93\u548c\u5de5\u5177\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u7528\u6237\u5728\u8f6c\u6362EPS\u6587\u4ef6\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u65e0\u6cd5\u6253\u5f00\u3001\u8f6c\u6362\u540e\u56fe\u50cf\u8d28\u91cf\u4e0d\u4f73\u7b49\u95ee\u9898\u3002\u786e\u4fddEPS\u6587\u4ef6\u6ca1\u6709\u635f\u574f\uff0c\u5e76\u4e14\u4f7f\u7528\u7684\u5e93\u548c\u5de5\u5177\u662f\u6700\u65b0\u7248\u672c\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u5728\u4fdd\u5b58PNG\u65f6\u8c03\u6574\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u8fdb\u884cEPS\u5230PNG\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u9664\u4e86<code>Pillow<\/code>\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>C<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ro<\/code>\u3001<code>ImageMagick<\/code>\u7b49\u5e93\u8fdb\u884cEPS\u5230PNG\u7684\u8f6c\u6362\u3002\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u7279\u70b9\uff0c\u6bd4\u5982<code>ImageMagick<\/code>\u652f\u6301\u66f4\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u9009\u9879\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u8bf7\u53c2\u8003\u5404\u81ea\u7684\u6587\u6863\u4ee5\u4e86\u89e3\u5177\u4f53\u7684\u7528\u6cd5\u548c\u529f\u80fd\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684PNG\u6587\u4ef6\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u7f51\u9875\u6216\u6253\u5370\uff1f<\/strong><br \/>\u8f6c\u6362\u540e\u7684PNG\u6587\u4ef6\u901a\u5e38\u5177\u6709\u826f\u597d\u7684\u517c\u5bb9\u6027\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u7f51\u9875\u8bbe\u8ba1\u6216\u6253\u5370\u3002PNG\u683c\u5f0f\u652f\u6301\u900f\u660e\u5ea6\u548c\u9ad8\u8d28\u91cf\u56fe\u50cf\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u6e05\u6670\u5ea6\u548c\u7ec6\u8282\u7684\u573a\u666f\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u65f6\u9009\u62e9\u5408\u9002\u7684\u5206\u8fa8\u7387\uff0c\u4ee5\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06EPS\u8f6c\u5316\u4e3aPNG\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528PIL\u5e93\u3001\u4f7f\u7528Ghostscript\u548cImageMagick [&hellip;]","protected":false},"author":3,"featured_media":1058958,"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\/1058945"}],"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=1058945"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058945\/revisions"}],"predecessor-version":[{"id":1058965,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058945\/revisions\/1058965"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058958"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}