I need to duplicate a layer, getPixels from either layer, modify those pixels, then save the modified pixels to the new layer with putPixels. I’m modifying the imaging-test sample code, below. It works fine until I uncomment the code duplicating the layer, even if I don’t do anything with that duplicated layer. I have the same problem if I try to get and put pixels from the duplicated layer. From debugging, the problem is from putPixels.
What am I doing wrong?
let doc = app.activeDocument;
let layers = doc.activeLayers;
let layer = layers[0];
// duplicate the layer; **** code works if this line commented out, not otherwise.******
const CopyLayer = await layer.duplicate();
const layerBounds = layer.boundsNoEffects;
let width = layerBounds.width;
let height = layerBounds.height;
let left = layerBounds.left;
let top = layerBounds.top;
const sourceBounds = { "left": left, "top": top, "width": width, "height": height };
// Get pixels from the current tile
const pixelResult = await imaging.getPixels(
{
"documentID": doc.id,
"layerID": CopyLayerID, // layer.id,
"sourceBounds": sourceBounds,
"colorSpace": "RGB",
"targetSize": { "height": sourceBounds.height }
}
);
const imageData = pixelResult.imageData;
const components = imageData.components;
const componentSize = imageData.componentSize;
const buffer = await imageData.getData();
// Modify pixels
width = imageData.width;
height = imageData.height;
const pixelCount = width * height;
for (let index = 0; index < pixelCount * components; index += components) {
buffer[index] = 30000; // set the first pixel value to max
}
// Create a new image object and assign pixels back to the target layer
let options = {
"width": width,
"height": height,
"components": components,
"chunky": true,
"colorProfile": imageData.colorProfile,
colorSpace: "RGB"
};
let image = await imaging.createImageDataFromBuffer(buffer, options);
//********this following code is where it crashes during debugging, only if the layer was duplicated *******
await imaging.putPixels({
//"documentID": doc.id,
"layerID": CopyLayerID, // layer.id,
"imageData": image,
//"replace": false,
targetBounds: { "left": sourceBounds.left, "top": sourceBounds.top }
});
Thanks,
John