import java.awt.Graphics2D import java.awt.image.BufferedImage import javax.imageio.ImageIO // Fetch images from previous tasks def parts = [] for (int i = 0; i < results.length; i++) { byte[] partBytes = results[i].value() BufferedImage buffImage = ImageIO.read(new ByteArrayInputStream(partBytes)) parts.push(buffImage) } int rows = Math.sqrt(parts.size()) int cols = rows int type = parts[0].getType() int partWidth = parts[0].getWidth() int partHeight = parts[0].getHeight() // Fill the final image BufferedImage finalImg = new BufferedImage(partWidth*cols, partHeight*rows, type) int num = 0 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { finalImg.createGraphics().drawImage(parts[num], partWidth * j, partHeight * i, null) num++ } } String filename = variables.get("outputFilename") File imageFile = new File( localspace, filename ) ImageIO.write( finalImg, "png", imageFile ) println 'Merged parts into ' + filename // Expose the image result = imageFile.getBytes() resultMetadata.put("file.name", imageFile.name) resultMetadata.put("content.type", "image/png")