Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Pasting from a script centers the image

New Here ,
Mar 23, 2012 Mar 23, 2012

Hello,

I'm new to these formus and to extendScript in particular, and I would be verry happy if someone could point out what I'm doing wrong.

I have a script that opens a image (lets call it icon), and for all the images in a folder, resizes icon to the current folder image, pastes the image on top, and saves it.

It works great except for a small problem which I can't seem to fix. When the image from the folder is smaller than the canvas ej:
anchorage_campito_2.png

Then when the image gets copied over the icon image, it's pasted centered, like this:

anchorage_campito_2.png

Which defeats the purpose of what I wanted to do, I needed it pasted in the same position as it was in the original image. As we are talking of well over 1k images, doing this by hand is a bit problematic, so I would be really thankful if someone could help me out.

Here is the script:

#target photoshop

app.bringToFront();

function main()

{

    var iconFile = File.openDialog ("Select the base (icon0) file", "*.png", false);

   

    var inputFolder= Folder.selectDialog ("Please select folder to process");

    if(inputFolder == null) return;

    var fileList = inputFolder.getFiles("*.png");

    var outFolder = Folder(inputFolder +"/Processed");

    if(!outFolder.exists) outFolder.create();      

   

    // Open icon file and create a new layer, making the background one invisible

    var icon = open(iconFile);   

    var artLayerRef = icon.artLayers.add()

    icon.layers[1].visible = false;

   

    for(var z in fileList)

    {

        // Open icon file

        var icon = open(iconFile);

        var artLayerRef = icon.artLayers.add()

        icon.layers[1].visible = false;

       

        // Open 32 bit file

        var img32 = open(fileList);

        var img32Name = decodeURI(fileList.name.replace(/\.[^\.]+$/, ''));

   

        // select all document

        img32.selection.selectAll();

        img32.selection.copy();

       

        // Copy image to the icon layer

        activeDocument = icon;

        icon.resizeImage (img32.width.value, img32.height.value);

        icon.paste();

        // Save icon with name

       var saveFile= File(outFolder + "/" + img32Name + ".png");

        Save24 (icon, saveFile);

       

        // Clear layer, close open image

        icon.selection.selectAll();

        icon.selection.clear();       

        img32.close();

    }

    icon.close(SaveOptions.DONOTSAVECHANGES);

}

main();

function Save24(doc, saveFile)

{

     pngSaveOptions = new PNGSaveOptions();

     doc.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

}

TOPICS
Actions and scripting
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Mar 24, 2012 Mar 24, 2012

Right, you first get the original position of the activeLayer in img32. After the copy/paste you then translate the pasted layer back to the origianl positon.

Something like this

var img32 = open(fileList);

var img32Name = decodeURI(fileList.name.replace(/\.[^\.]+$/, ''));

var x = img32.activeLayer.bounds[0.value];// get the x position of the layer

var y = img32.activeLayer.bounds[1.value];// get the y position of the layer

// selectAll, copy, switch to icon, resize, paste code here

// ...

// after the

...
Translate
Adobe
Guru ,
Mar 23, 2012 Mar 23, 2012

You might want to try using layer.duplicate() to copy the layer to the other document. That should maintain the position better.

If that doesn't work you will need to get the position of the layer after it has been copied then use layer.translate() to move it where you want it to appear.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2012 Mar 24, 2012

Hy Michael, thank you for your reply.

Attempted to layer.duplicate and I got an error saying "general photoshop error, feature not supported in this version" or something along those lines (using Photoshop CS 5).

Adter that, I have been attempting to move the graphics once copied but I don't quite manage to sort out how to do it.

Can I somehow get the original position of the graphics in the first document, to move them to that position on the second one? A script example would really help, as the documentation I'm finding is not too explicit.

Thank you again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 24, 2012 Mar 24, 2012

Layer.duplicate works in CS5 but we can set that aside for now.

I am not sure what you are trying to position. I skimed your code the frist time and thought that the icon was the image being pasted. img32 will always be centered because you are pasting it into a document that has been sized to fit img32.

So I am guessing that it is the icon you want to position. That is being moved by the resizeImage commad as it is not pasted. If you switch resizeImage for resizeCanvas you can set the anchor positon.

Or you can get the current positon of the icon layer from that layer's bounds. The first two elements of that array are the x,y position of the layer. Later you can move the icon layer using the function below.

function positionLayer( lyr, x, y ){// layerObject, Number, Number

    // if can not move layer return

    if(lyr.iisBackgroundLayer||lyr.positionLocked) return

    // get the layer bounds

    var layerBounds = lyr.bounds;

    // get top left position

    var layerX = layerBounds[0].value;

    var layerY = layerBounds[1].value;

    // the difference between where layer needs to be and is now

    var deltaX = x-layerX;

    var deltaY = y-layerY;

    // move the layer into position

    lyr.translate (deltaX, deltaY);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2012 Mar 24, 2012

Hy Michael,

first, thank you for taking time to reply. I apologize for how confusing the post is, I realize how hard it is to understand by code, so I'll try it with images to make it clearer.

Here is the original icon file:

// Open icon file

        var icon = open(iconFile);

        var artLayerRef = icon.artLayers.add()

        icon.layers[1].visible = false;

aa.png

And now we open the first img32 file (sorry for the size, it's a bit hard to see but there is a small green hill on bottom left)

// Open 32 bit file

        var img32 = open(fileList);

        var img32Name = decodeURI(fileList.name.replace(/\.[^\.]+$/, ''));

ab.png

Then we select the img32 and copy it:

// select all document

        img32.selection.selectAll();

        img32.selection.copy();

ac.png

And then we resize the icon document to fit the img32 size:

// Copy image to the icon layer

        activeDocument = icon;

        icon.resizeImage (img32.width.value, img32.height.value);

ad.png

Finaly we paste on the icon the img32 content we had copied:

icon.paste();

ae.png

Notice how the img32 contents get copied smack in the center? I needed to copy it in it's original position, not the center.

You can probably see the problem with working with translate, as I can't figure out the displacement the image needs. All images I need to work are different sizes and positioned in random positions in the canvas, so I can't hack it to be always bottom right or something like that, I need it in the original position.

Any tips? Your help is greatly appreciated, and frankly, the only hope I have about this whole busines right now.

Thanks again.    

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 24, 2012 Mar 24, 2012

Right, you first get the original position of the activeLayer in img32. After the copy/paste you then translate the pasted layer back to the origianl positon.

Something like this

var img32 = open(fileList);

var img32Name = decodeURI(fileList.name.replace(/\.[^\.]+$/, ''));

var x = img32.activeLayer.bounds[0.value];// get the x position of the layer

var y = img32.activeLayer.bounds[1.value];// get the y position of the layer

// selectAll, copy, switch to icon, resize, paste code here

// ...

// after the paste line move the layer back into position.

positionLayer( icon.activeLayer, x, y );// using the function I posted earlier.

Note that x,y come from the opened img32 document so will change to match each file opened in the loop.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 24, 2012 Mar 24, 2012

Haveing a bad night Mike

the lines should be....

var x = img32.activeLayer.bounds[0].value;// get the x position of the layer

var y = img32.activeLayer.bounds[1].value;// get the y position of the layer

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 24, 2012 Mar 24, 2012

A bad night, bad eyesight, or typing in a hurry, not sure which.

Looking at the original script I am not sure why the icon file is being opened at the start of the script and then again inside the for loop. I also don't understand why duplicate didn't work as I still think that would solve the position problem and require fewer lines of code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 25, 2012 Mar 25, 2012
LATEST

Thank you all for your replies. Managed to get it to work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines