Skip to main content
New Participant
August 20, 2009
Question

Programmatically Replacing an Image in ArtLayer

  • August 20, 2009
  • 2 replies
  • 4477 views

Hi All,

I have a PSD file that contains several layers (a.k.a. ArtLayer in ExtendScript).

Each ArtLayer contains a single graphic image.

I would like to write an ExtendScript that:

* Opens the PSD file (I have that working)

* Iterates over the ArtLayers (I have that working)

* Finds the graphic image in the layer/finds the bounds of the single graphic image in the layer.

* Replaces the layer's original image with a new image of equal size and dimension.

Is this possible?

Many thanks,

John

This topic has been closed for replies.

2 replies

Inspiring
August 20, 2009

This might be easier. It works with PDF's saved by Photoshop.

Open the PDF,

Convert each layer you want to replace into a smart object.

Replace the contents of that smart object with the new image.

That way there is no need to worry about where the image is on the layer as long as the replacement is the same size.

I can post the code for working with smart objects if you need it or you can use scriptlistner. Or if you have Configurator you can get the code from there.

New Participant
August 20, 2009

Michael,

Thanks for your reply.

I am brand new to Photoshop, so I don't even know what a smart object is; however, it sounds promising. Please send or post the code to work with smart objects. I appreciate your help.

John

New Participant
August 20, 2009

Thanks to everyone for the (very quick) replies. Much appreciated.

I will post my final code, once I get it all working.

Best,

John

Inspiring
August 20, 2009

From what I'm reading, yes. But, to make sure I have a few questions first.

1. Are these specific images or random?

2. When you replace them, are you replacing each size with a specific image? What I mean is, say the image on the layer is 200x200 pixels. Would this be replaced by an image that would be specifically for that size or do you have multiple images that could replace this one? So if another psd were opened and one of those layers had an image size of 200x200 pixels, it would use the same file. Or, would the script have to find another image file of the same size to replace that one with?

3. Are these images in the layers set up with transparent background so that there are no other pixels on that layer other than the image itself?

If "Yes" to all 3, then I believe it could be done.

New Participant
August 20, 2009

Hi. Thank you for the quick reply. Here are my answers to your thoughtful questions:

>>>1. Are these specific images or random?

Each layer will contain only a single image, but its placement will vary. For example, let say I open myFile.psd and it contains 2 layers. Layer 1 will contain a single image located at some X,Y and be so wide and so high. Layer 2 will also contain a single image, located at some A, B and be of a certain width and height. Since each layer contains only a single image, I can simple clear the layer. From ExtendScript, this would be a myLayer.clear() call.

>>>2. When you replace them, are you replacing each size with a specific image? What I mean is, say the image on the layer is 200x200 pixels. Would this be replaced by an image that would be specifically for that size or do you have multiple images that could replace this one? So if another psd were opened and one of those layers had an image size of 200x200 pixels, it would use the same file. Or, would the script have to find another image file of the same size to replace that one with?

I would replace the original image with one of exactly the same dimensions. So, from my answer in #1, I could do something like this:

var myOrigBounds = myLayer.bounds;

myLayer.clear();

// PUT NEW IMAGE INTO THE LAYER (this is what I am struggling with)

>>>3. Are these images in the layers set up with transparent background so that there are no other pixels on that layer other than the image itself?

Yes, but that shouldn't matter, because I am going to clear() the original image anyway.

>>>If "Yes" to all 3, then I believe it could be done.

In essence, I believe the answer to all three is "yes" -- so, what I am looking for is how to best "put" the new image into the layer. The ArtLayer object does not seem to support a "paste" function or something similar.

Thanks for your help,

John

Inspiring
August 20, 2009

You are correct, ArtLayer does not support paste, but Document does. It allows you to paste into a selection.

app.activeDocument.paste(TRUE);

The TRUE will paste the contents of the clipboard into a selection if one exists. Otherwise, it will create a new layer and paste into that.

You can get the selection bounds from the ArtLayer bounds to paste into.

This should work for you. If not, post again and I'll work up a script unless Michael or xbytor gets here with one first. They always seem to beat me at writing scirpts.