Does the psd plug-in support multi-concurrency?

Does the psd plug-in support multi-concurrency?I want to generate many sets of images at the same time

@huziwei

The Aspose.PSD plug-in does support multi-concurrency, but it’s important to note that not every operation related to PSD processing can be parallelized effectively. Typically, it is recommended to use one thread per file being processed simultaneously, which should suffice for most use cases.

If you are looking to generate multiple sets of images at the same time, you can implement a multi-threaded approach. This can significantly enhance performance by allowing multiple image processing tasks to run concurrently.

For example, you can create a separate thread for each image processing task. Here’s a basic outline of how you might implement this in C#:

using Aspose.PSD;
using Aspose.PSD.ImageOptions;
using System;
using System.IO;
using System.Threading;

class Program
{
    static void Main()
    {
        string[] psdFiles = { "file1.psd", "file2.psd", "file3.psd" }; // Add your PSD file paths here
        foreach (var psdFile in psdFiles)
        {
            Thread thread = new Thread(() => ProcessPsdFile(psdFile));
            thread.Start();
        }
    }

    static void ProcessPsdFile(string psdFilePath)
    {
        // Load the PSD file
        using (var image = Image.Load(psdFilePath))
        {
            // Process the image (e.g., save it in a different format)
            string outputFilePath = Path.ChangeExtension(psdFilePath, ".png");
            image.Save(outputFilePath, new PngOptions());
        }
    }
}

In this example, each PSD file is processed in its own thread, allowing for concurrent image generation. Make sure to handle any potential exceptions and manage resources properly to avoid issues with file access.

For more detailed guidance on exporting images in a multi-threaded environment, you can refer to the official tutorial on this topic here which provides step-by-step instructions and best practices.

If you have further questions or need more specific examples, feel free to ask!

Sources:
[1]: Exporting Images in Multi-Threaded Environment with Aspose.PSD for .NET

I am using java

@huziwei Could you please clarify, do you use Aspose.PSD plugin for .NET from Java via some middleware? At this moment Aspose.PSD Java plugins are not released. Which plugin do you use? Or do you use full version of Aspose.PSD for Java?

Aspose.PSD blocks the root of PSD file while changing it. But you should be able to edit different layers or create multiple images if they are different.

At same time Aspose.PSD plugins have limitations on parallel processing of multiple images , you will need multiple instances where Aspose.PSD plugins work Process PSD, PSB, AI files with API

Aspose.PSD for Java This is what I’m currently using

image.png (10.8 KB)

@huziwei thank you. Aspose.PSD for Java supports concurrency, you should be able to to create and edit multiple images at once, but as I wrote before, with the editing of single file from can be issues, because Aspose.PSD locks PSD layers’ root.

thanks :grinning: i get it

1 Like

Is there any example I can refer to?

What if I want to operate the same PSD file multiple times at the same time? For example, I have many pictures but only one PSD file, and I need this PSD file to generate a set of pictures based on the different pictures I input.

@huziwei I’d recommend to create a required count of base PSD files (they can be just copied by any system functions) and then run the concurrent file manipulations.

If you describe your case, we can prepare example.

My requirement is: I have a lot of pictures and I need to use the same PSD file to generate sets of pictures, which need to be processed concurrently. Thank you for providing examples

@huziwei please check this case if it’s suitable for you. In this case PSD File is loaded to the memory N times, and then it can be edited in each instance:

public static void main(String[] args) {
    String inputFile = "src/main/resources/Stroke.psd";
    String outputFile = "src/main/resources/output_Stroke";

    // Load the PSD file once and store it in memory
    byte[] psdBytes = loadPsdIntoMemory(inputFile);

    for (int i = 0; i < 4; i++) {
        int finalI = i;
        new Thread(() -> processPsd(psdBytes, outputFile + finalI + ".psd")).start();
    }
}

private static byte[] loadPsdIntoMemory(String filePath) {
    try (PsdImage psdImage = (PsdImage) Image.load(filePath);
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        psdImage.save(outputStream, new PsdOptions()); // Save to memory
        return outputStream.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException("Error loading PSD into memory", e);
    }
}

private static void processPsd(byte[] psdBytes, String outputFile) {
    var loadOptions = new PsdLoadOptions();
    loadOptions.setLoadEffectsResource(true);
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(psdBytes);
         PsdImage psdImage = (PsdImage) Image.load(inputStream)) {
        psdImage.save(outputFile);
    } catch (Exception e) {
        System.err.println("Error processing PSD in thread ");
    }
}