{"id":1094809,"date":"2025-01-08T14:44:45","date_gmt":"2025-01-08T06:44:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094809.html"},"modified":"2025-01-08T14:44:48","modified_gmt":"2025-01-08T06:44:48","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86eps%e8%bd%ac%e5%8c%96%e4%b8%bapng-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094809.html","title":{"rendered":"python\u5982\u4f55\u5c06eps\u8f6c\u5316\u4e3apng"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210745\/b588e6b1-5d3a-4eda-9f32-fce5ed0a6a5d.webp\" alt=\"python\u5982\u4f55\u5c06eps\u8f6c\u5316\u4e3apng\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u5e93\u6765\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\uff0c\u5305\u62ecPillow\u3001ImageMagick\u548cGhostscript\u7b49\u3002<\/strong> \u5176\u4e2d\uff0cPillow\u5e93\u662f\u8f83\u4e3a\u5e38\u7528\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u662fPython Imaging Library (PIL) \u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5728\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u65b9\u9762\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u501f\u52a9Pillow\u5e93\u6765\u5b9e\u73b0EPS\u5230PNG\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pillow\u5e93<\/h3>\n<\/p>\n<p><p>Pillow\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5b83\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u3002\u8981\u4f7f\u7528Pillow\u5e93\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>def convert_eps_to_png(input_path, output_path):<\/p>\n<p>    # \u6253\u5f00EPS\u6587\u4ef6<\/p>\n<p>    image = Image.open(input_path)<\/p>\n<p>    # \u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f<\/p>\n<p>    image.save(output_path, &#39;PNG&#39;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>convert_eps_to_png(&#39;example.eps&#39;, &#39;example.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u6b65\u9aa4\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00EPS\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>Image.open<\/code>\u65b9\u6cd5\u6253\u5f00EPS\u6587\u4ef6\uff0c\u751f\u6210\u4e00\u4e2a<code>Image<\/code>\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f<\/strong>\uff1a\u4f7f\u7528<code>Image.save<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u5bf9\u8c61\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\uff0c\u6307\u5b9a\u8f93\u51fa\u8def\u5f84\u548c\u683c\u5f0f\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528ImageMagick\u548cWand\u5e93<\/h3>\n<\/p>\n<p><p>ImageMagick\u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5904\u7406\u8d85\u8fc7200\u79cd\u56fe\u50cf\u683c\u5f0f\u3002Wand\u662fImageMagick\u7684Python\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u7528ImageMagick\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5ImageMagick\u548cWand\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install imagemagick<\/p>\n<p>pip install Wand<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from wand.image import Image<\/p>\n<p>def convert_eps_to_png(input_path, output_path):<\/p>\n<p>    with Image(filename=input_path) as img:<\/p>\n<p>        img.format = &#39;png&#39;<\/p>\n<p>        img.save(filename=output_path)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>convert_eps_to_png(&#39;example.eps&#39;, &#39;example.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u6b65\u9aa4\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00EPS\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>Image<\/code>\u5bf9\u8c61\u6253\u5f00EPS\u6587\u4ef6\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u56fe\u50cf\u683c\u5f0f<\/strong>\uff1a\u5c06\u56fe\u50cf\u683c\u5f0f\u8bbe\u7f6e\u4e3aPNG\u3002<\/li>\n<li><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong>\uff1a\u4f7f\u7528<code>save<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\uff0c\u6307\u5b9a\u8f93\u51fa\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528Ghostscript<\/h3>\n<\/p>\n<p><p>Ghostscript\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406PostScript\u548cPDF\u6587\u4ef6\u7684\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u3002\u9700\u8981\u5b89\u88c5Ghostscript\u548cPillow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install ghostscript<\/p>\n<p>pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import subprocess<\/p>\n<p>def convert_eps_to_png(input_path, output_path):<\/p>\n<p>    # \u4f7f\u7528Ghostscript\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6<\/p>\n<p>    subprocess.run([&#39;gs&#39;, &#39;-sDEVICE=pngalpha&#39;, &#39;-o&#39;, output_path, input_path])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>convert_eps_to_png(&#39;example.eps&#39;, &#39;example.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u6b65\u9aa4\u89e3\u6790\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u8c03\u7528Ghostscript<\/strong>\uff1a\u4f7f\u7528<code>subprocess.run<\/code>\u65b9\u6cd5\u8c03\u7528Ghostscript\uff0c\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6307\u5b9a\u8f93\u51fa\u8def\u5f84<\/strong>\uff1a\u5728\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u6307\u5b9a\u8f93\u51fa\u8def\u5f84\u548c\u8f93\u5165\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528Python\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u73af\u5883\u800c\u5b9a\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Pillow\u5e93<\/strong>\uff1a\u9002\u5408\u5904\u7406\u5355\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u6613\u4e8e\u4f7f\u7528\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>ImageMagick\u548cWand\u5e93<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u56fe\u50cf\u6587\u4ef6\u548c\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<li><strong>Ghostscript<\/strong>\uff1a\u9002\u5408\u5904\u7406PostScript\u548cPDF\u6587\u4ef6\uff0c\u8f6c\u6362\u901f\u5ea6\u5feb\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5Ghostscript\u5de5\u5177\u3002<\/li>\n<\/ul>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u76f8\u5e94\u7684\u4f9d\u8d56\u5e93\uff0c\u5e76\u6309\u7167\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u7bc7\u6587\u7ae0\u80fd\u4e3a\u60a8\u5728Python\u4e2d\u5b9e\u73b0EPS\u5230PNG\u7684\u8f6c\u6362\u63d0\u4f9b\u5e2e\u52a9\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><\/p>\n<p>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b8c\u6210EPS\u5230PNG\u7684\u8f6c\u6362\u3002\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>Pillow<\/code>\u5e93\u548c<code>Ghostscript<\/code>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86\u8fd9\u4e24\u4e2a\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u6253\u5f00EPS\u6587\u4ef6\nimg = Image.open(&#39;your_file.eps&#39;)\n\n# \u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\nimg.save(&#39;your_file.png&#39;, &#39;PNG&#39;)\n<\/code><\/pre>\n<p>\u5728\u4f7f\u7528<code>Pillow<\/code>\u8fdb\u884cEPS\u8f6c\u6362\u65f6\uff0c\u7cfb\u7edf\u9700\u8981\u5b89\u88c5<code>Ghostscript<\/code>\uff0c\u4ee5\u4fbf\u5904\u7406EPS\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><\/p>\n<p>\u5728\u8fdb\u884cEPS\u5230PNG\u7684\u8f6c\u6362\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\uff0c\u5982\u56fe\u50cf\u5931\u771f\u3001\u900f\u660e\u5ea6\u4e22\u5931\u6216\u662f\u8f6c\u6362\u540e\u7684\u6587\u4ef6\u5927\u5c0f\u8fc7\u5927\u3002\u8fd9\u901a\u5e38\u4e0eEPS\u6587\u4ef6\u7684\u590d\u6742\u6027\u548c\u6240\u7528\u5e93\u7684\u5904\u7406\u80fd\u529b\u6709\u5173\u3002\u786e\u4fdd\u60a8\u7684<code>Pillow<\/code>\u548c<code>Ghostscript<\/code>\u5e93\u662f\u6700\u65b0\u7248\u672c\uff0c\u80fd\u591f\u63d0\u9ad8\u8f6c\u6362\u6548\u679c\u3002\u540c\u65f6\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u8c03\u6574\u56fe\u50cf\u7684\u5206\u8fa8\u7387\u548c\u538b\u7f29\u8bbe\u7f6e\uff0c\u4ee5\u4f18\u5316\u8f93\u51fa\u6587\u4ef6\u7684\u8d28\u91cf\u548c\u5927\u5c0f\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5176\u4ed6\u5de5\u5177\u6216\u5e93\u53ef\u4ee5\u5b8c\u6210EPS\u5230PNG\u7684\u8f6c\u6362\uff1f<\/strong><\/p>\n<p>\u9664\u4e86<code>Pillow<\/code>\u548c<code>Ghostscript<\/code>\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>ImageMagick<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u8f6c\u6362\u3002\u4f7f\u7528<code>ImageMagick<\/code>\u65f6\uff0c\u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u76f4\u63a5\u8c03\u7528\u8f6c\u6362\u547d\u4ee4\uff0c\u6216\u8005\u4f7f\u7528Python\u4e2d\u7684<code>wand<\/code>\u5e93\u8fdb\u884c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>wand<\/code>\u5e93\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from wand.image import Image\n\nwith Image(filename=&#39;your_file.eps&#39;) as img:\n    img.save(filename=&#39;your_file.png&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u6709\u6548\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5927\u578b\u6216\u590d\u6742\u7684EPS\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u8868\u73b0\u5f97\u66f4\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u5e93\u6765\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u6587\u4ef6\uff0c\u5305\u62ecPillow\u3001ImageMagick\u548cGhost [&hellip;]","protected":false},"author":3,"featured_media":1094821,"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\/1094809"}],"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=1094809"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094809\/revisions"}],"predecessor-version":[{"id":1094824,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094809\/revisions\/1094824"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094821"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}