Layer group export image error:The rectangle has no common processing area

我使用的是psd-java,当我把图层组导出为png的时候报错。这是我的代码: for (Layer layer : psdImage.getLayers()) {
if (layer instanceof LayerGroup) {
count++;
LayerGroup layerGroup = (LayerGroup) layer;
System.out.println("画板名称: " + layerGroup.getName());
// 直接获取不带扩展名的文件名
fileNameNoExt = FilenameUtils.getBaseName(imageFilePath);
outPath = uploadPath + File.separator + fileNameNoExt + File.separator;
// uploadPathPic为素材名
Path uploadPathPic = Paths.get(outPath);
if (!Files.exists(uploadPathPic)) {
Files.createDirectories(uploadPathPic);
}
// 合并画板并保存为 PNG
String mergedPngPath = outPath + layerGroup.getName() + “.png”;

                // 假设 LayerGroup 有获取边界的方法(请根据 Aspose.PSD 的 API 调整)
                Rectangle bounds = layerGroup.getBounds();
                System.out.println("图层组 " + layerGroup.getName() + " 边界: " + bounds);
                if (bounds.getWidth() <= 0 || bounds.getHeight() <= 0) {
                    throw new IllegalStateException("图层组 " + layerGroup.getName() + " 的合并区域无效");
                }

                layerGroup.save(mergedPngPath, new PngOptions());
                String OSSPath = fileNameNoExt + count + ".png";
                System.out.println(OSSPath);

                System.out.println("画板 \"" + layerGroup.getName() + "\" 已合并并保存为 PNG:" + mergedPngPath);
            }
        }

image.png (109.1 KB)

@youxin

The error message “The rectangle has no common processing area” typically indicates that the bounds of the layer group you are trying to export are invalid, meaning that the width or height of the bounds is less than or equal to zero. This can happen if the layer group is empty or if all layers within the group are hidden or have no visible content.

To resolve this issue, you should check the bounds of the layer group before attempting to save it. Here’s a modified version of your code that includes a check for valid bounds:

for (Layer layer : psdImage.getLayers()) {
    if (layer instanceof LayerGroup) {
        count++;
        LayerGroup layerGroup = (LayerGroup) layer;
        System.out.println("Layer Group Name: " + layerGroup.getName());

        // Get the bounds of the layer group
        Rectangle bounds = layerGroup.getBounds();
        System.out.println("Bounds of Layer Group " + layerGroup.getName() + ": " + bounds);

        // Check if the bounds are valid
        if (bounds.getWidth() <= 0 || bounds.getHeight() <= 0) {
            System.out.println("Skipping Layer Group " + layerGroup.getName() + " due to invalid bounds.");
            continue; // Skip this layer group
        }

        // Create output path
        fileNameNoExt = FilenameUtils.getBaseName(imageFilePath);
        outPath = uploadPath + File.separator + fileNameNoExt + File.separator;
        Path uploadPathPic = Paths.get(outPath);
        if (!Files.exists(uploadPathPic)) {
            Files.createDirectories(uploadPathPic);
        }

        // Save the layer group as PNG
        String mergedPngPath = outPath + layerGroup.getName() + ".png";
        layerGroup.save(mergedPngPath, new PngOptions());
        System.out.println("Layer Group \"" + layerGroup.getName() + "\" has been saved as PNG: " + mergedPngPath);
    }
}

In this code, if the bounds of the layer group are invalid, it will skip the export for that group and print a message indicating that it was skipped. This should help you avoid the error you encountered.

If you continue to experience issues, ensure that the layer group you are trying to export actually contains visible layers and that they are not hidden. You can also refer to the Aspose documentation on exporting PSD layer groups for more details on handling layer groups.

Sources:
[1]: Export PSD Layer Group to Image using Java - Aspose

我使用的是JDK1.8,psd文件在这个链接中,感谢您!
AirPortal 空投快传 复制链接到浏览器打开

而且很奇怪,为什么我不能直接加载png图片呢?
报错信息:“Image loading failed. Cannot open an image. The image file format may be not supported at the moment.”
代码: RasterImage newContent = (RasterImage)com.aspose.psd.Image.load(“D:\J\Idea2024\IDEAProject\EnMingERP\python\file\TRF8502.png”);

@youxin
我可以确认这些问题。根据您的要求,我们创建了 2 个高优先级问题(另外还有 2 个针对 .NET 的问题)

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PSDJAVA-719,PSDNET-2409,PSDJAVA-720,PSDNET-2410

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

        // 加载 PSD 文件
        PsdImage psdImage = (PsdImage) com.aspose.psd.Image.load(psdFilePath,loadOptions);


        int width = 0;
        int height = 0;
        boolean isFirstSmartObject = true;
        // 导出智能对象并替换内容
        for (Layer layer : psdImage.getLayers()) {
            if (layer instanceof SmartObjectLayer) {
                SmartObjectLayer smartObjectLayer = (SmartObjectLayer) layer;
                System.out.println("智能对象名称: " + smartObjectLayer.getName());
                if (isFirstSmartObject) {
                    // 导出智能对象内容
                    smartObjectLayer.exportContents(outputSmartPNG);

                    // 读取导出的 PNG 文件,获取宽高
                    BufferedImage image = ImageIO.read(new File(outputSmartPNG));
                    if (image != null) {
                        width = image.getWidth();
                        height = image.getHeight();
                        System.out.println("图片宽度: " + width);
                        System.out.println("图片高度: " + height);
                    } else {
                        throw new IOException("无法读取导出的 PNG 文件");
                    }

                    // 调整素材图为智能对象大小
                    BufferedImage materialImage = ImageIO.read(new File(imageFilePath));
                    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g = resizedImage.createGraphics();
                    g.drawImage(materialImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
                    g.dispose();

                    // 保存调整后的图片
                    ImageIO.write(resizedImage, "png", new File(outputPath));

                    // 创建新图层并替换智能对象内容
                    InputStream inputStream = Files.newInputStream(Paths.get(outputPath));
                    Layer newLayer = new Layer(inputStream);
                    PsdImage newPsdImage = new PsdImage(width, height);
                    newPsdImage.setLayers(new Layer[]{newLayer});
                    newPsdImage.save(outputSmartPsd, new PsdOptions());

                    // 标记已经执行过一次
                    isFirstSmartObject = false;
                }
                // 替换智能对象内容
                PsdImage embeddedPsd = (PsdImage) com.aspose.psd.Image.load(outputSmartPsd);
                smartObjectLayer.replaceContents(embeddedPsd);
                smartObjectLayer.updateModifiedContent();
                System.out.println("智能对象内容已替换");
            }
        }     

This is my prepend code

image.jpg (88.6 KB)

image.jpg (74.4 KB)

哥们,你智能层的变形会保留嘛。

@LiXinChen could you please provide the input PSD file, also it’s better to create new forum topic. In this case you’ll get the notification when it will be fixed

我是直接替换的图片,这个插件有些bug,但是已经算是很优的解决方案了

有变形效果嘛?我这边自定义变形都没看见效果

没有变形效果,变形一般都在外部做的,直接做成那种智能对象那种,加个联系方式交流下?

加你了 我用的psd-java

@youxin 请看一下这篇文章。扭曲变形有些不同,但它们有效。我们正在努力尽快改进它们。

什么时候可以完成这个变形效果。我们急需要使用!如果你们支持了,请第一时间告诉我,我会来购买~

相信我,如果你们支持了变形效果,并且提速,这将会有很多行业内人使用。目前你们的效果只是基本,并无法商业盈利化运行。

@LiXinChen 在当前版本中,将添加对多重效果的支持。预计在下一个版本 25.5 中将会对 Warp 进行改进。

非常期待。我将持续关注。希望对warp进行改进后能达到我期待的效果,并且在运行速度上有提速。

1 Like