使用psd-tools可将PSD转换为PNG或JPG,先通过PSDImage.open()打开文件,调用composite()合成图像后保存;如需导出单个图层,可遍历可见层并逐个渲染保存,注意颜色模式转换、透明通道处理及内存占用问题。

使用 psd-tools 可以方便地在 Python 中读取和操作 PSD 文件。如果你希望将 PSD 文件转换为其他图像格式(如 PNG、JPG 等),可以通过提取合成图像或逐层处理来实现。以下是具体方法。
pip install psd-tools
from psd_tools import PSDImage
<h1>打开 PSD 文件</h1><p>psd = PSDImage.open('example.psd')</p><h1>合成图像(包含所有图层的最终效果)</h1><p>image = psd.composite()</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/880">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175679979477571.png" alt="TextIn Tools">
</a>
<div class="aritcle_card_info">
<a href="/ai/880">TextIn Tools</a>
<p>是一款免费在线OCR工具,包含文字识别、表格识别,PDF转文件,文件转PDF、其他格式转换,识别率高,体验好,免费。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="TextIn Tools">
<span>69</span>
</div>
</div>
<a href="/ai/880" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="TextIn Tools">
</a>
</div>
<h1>保存为 PNG</h1><p>image.save('output.png')</p><h1>如需保存为 JPG(注意:JPG 不支持透明通道)</h1><h1>image.convert('RGB').save('output.jpg', quality=95)</h1><p># 遍历所有图层
for layer in psd:
if layer.is_visible(): # 只处理可见图层
layer_image = layer.composite()
if layer_image:
layer_image.save(f'layers/{layer.name}.png')
基本上就这些。通过 composite() 方法获取图像数据后,就可以用 Pillow 支持的任意格式保存。
以上就是Python psd-tools如何转换文件的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号