Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The export artboard function exports the size of the entire psd canvas #399

Open
JHD1159351858 opened this issue Feb 14, 2025 · 2 comments

Comments

@JHD1159351858
Copy link

I hid all other artboards and exported the current artboard, but the image was the size of the psd canvas.

Image

Image

@JHD1159351858
Copy link
Author

for layer in doc.layers:
layer.visible = False
# for ab in artboards:
# ab.visible = False
# 遍历每个图层组,仅显示当前图层组进行导出
for artboard in artboards:
artboard.visible = True # 只显示当前图层组
pic_name = artboard.name
export_path_new = os.path.join(export_dir, f"{pic_name}.png")

                    # 复制当前文档,避免对原始文档做不可逆修改
                    dup_doc = doc.duplicate()  # 复制文档

                    # 对复制文档执行裁剪,移除连续的透明区域
                    # 注意:这里的 TrimType 和 SaveOptions 根据你的 ps_app API 可能需要调整
                    dup_doc.trim(ps_app.TrimType.TransparentPixels, True, True, True, True)

                    # 配置 PNG 保存选项
                    png_options = ps_app.PNGSaveOptions()

                    # 导出裁剪后的图像
                    dup_doc.saveAs(export_path_new, png_options, asCopy=True)
                    print(f"已导出图层组 '{artboard.name}' 为 PNG,路径:{export_path_new}")

                    # 关闭副本文档,不保存修改
                    dup_doc.close(ps_app.SaveOptions.DONOTSAVECHANGES)

                    artboard.visible = False  # 恢复当前图层组为隐藏状态

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


for layer in doc.layers:
layer.visible = False
# for ab in artboards:
# ab.visible = False
# traverse each layer group, display only the current layer group for export
for artboard in artboards:
artboard.visible = True # Show only the current layer group
pic_name = artboard.name
export_path_new = os.path.join(export_dir, f"{pic_name}.png")

                    # Copy the current document to avoid irreversible modifications to the original document
                    dup_doc = doc.duplicate() # Copy the document

                    # Perform cropping on the copied document to remove continuous transparent areas
                    # Note: TrimType and SaveOptions here may need to be adjusted according to your ps_app API
                    dup_doc.trim(ps_app.TrimType.TransparentPixels, True, True, True, True)

                    # Configure PNG save options
                    png_options = ps_app.PNGSaveOptions()

                    # Export cropped images
                    dup_doc.saveAs(export_path_new, png_options, asCopy=True)
                    print(f"Exported layer group '{artboard.name}' is PNG, path: {export_path_new}")

                    # Close the copy of this document without saving the modification
                    dup_doc.close(ps_app.SaveOptions.DONOTSAVECHANGES)

                    artboard.visible = False # Restore the current layer group to a hidden state  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants