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

Get/Apply Canvas Size

Contributor ,
Mar 18, 2011 Mar 18, 2011

I'm doing some clean-up scripting for some photoshop processes used where I work and have thought of a way to eliminate having to modify the actions but am unsure the script used to perform the actions.  What I need is a script that will get the canvas size of one document (I'll call it original.psd for easy reference) and apply it to another document without damaging or resizing the layers present (I'll call this one target.psd).

I have most of the script already, so I just need the lines that will

a) get the canvas size of original.psd and

b) apply that canvas size to target.psd

Thanks in advance for any help!

DGolberg

TOPICS
Actions and scripting
7.6K
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 19, 2011 Mar 19, 2011

A script need to reference a document before it can work with it. You did not tell the order the images were opened or their names. The code below will get the canvas size of the first opened document and apply it to the second opened.

var w = app.documents[0].width;// the width of the first open document
var h = app.documents[0].height;
app.activeDocument = app.documents[1];// make sure the second open document is active
app.activeDocument.resizeCanvas (w, h);

If you want the document order switched

...
Translate
Adobe
Community Expert ,
Mar 18, 2011 Mar 18, 2011

If the target psd file has a Photoshop Background Layer and you change the canvas size that background layer will be changes new pixels will be added if the new canvas dimensions size is larger then or old ones will be cropped it the new canvas is smaller. If the PDS has a background layer it is a special Photoshop layer its the bottom layer that does not support transparency so it will always be at least partially locked and be canvas size .  Other layer do not have this special property and they can be any size they can have dimensions lager then smaller then and the same as the canvas size dimensions. Only unmasked Layer areas over the canvas area will be rendered or blended into the composite view.

JJMack
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
Contributor ,
Mar 18, 2011 Mar 18, 2011

Not entirely sure what you're saying, but it sounds like you're talking about the locked background layer you generally see when opening non-PSD files.  This I already know about and the actions I have take care of converting this to a layer.  What I need is the code to get the canvas size of one document and apply it to the one where the background has been converted to a layer.  I stress canvas size as image size converts all layers regardless of type.

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
Guide ,
Mar 18, 2011 Mar 18, 2011

I too am at a bit of a loss as to what exactly you are tying to do? From what its sounds like you don't need the help of script… You can just use resize canvas to a given value about given position in a recorded action… can't you? (It has been a while since I last used actions)…

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
Contributor ,
Mar 19, 2011 Mar 19, 2011
LATEST

Muppet Mark wrote:

I too am at a bit of a loss as to what exactly you are tying to do? From what its sounds like you don't need the help of script… You can just use resize canvas to a given value about given position in a recorded action… can't you? (It has been a while since I last used actions)…

You are correct in that I can do this through actions (and actually that's how we were doing it); however, the point of the script is so that we don't need to modify the action every time the original document's canvas size changes.  Sorry if my original explanations were a bit confusing for everyone.  A large portion of my work is under NDA, so I was probably being overly careful.

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
Community Expert ,
Mar 18, 2011 Mar 18, 2011

dgolberg wrote:

  I stress canvas size as image size converts all layers regardless of type.

You may stress that!!!  However canvas size will only change the size of one layer a background layer.  A PSD file may or may not not have a Photoshop Background layer. You can not create an action that will be able to convert the background layer to a normal layer for all PSD files do not have a background layer. However you can do that with a script for it can test to see if the bottom layer is a Background layer and convert it to a normal layer.  If you download my crafting action package one of the utility scripts I wrote for use within actions that are included does exactly that.

http://www.mouseprints.net/old/dpr/JJMacksCraftingActions.zip

Contains:

Action Actions Palette Tips.txt

Action Creation Guidelines.txt

Action Dealing with Image Size.txt

Action Enhanced via Scripted Photoshop Functions.txt

CraftedActions.atn Sample Action set includes an example Watermarking action

Sample Actions.txt Photoshop CraftedActions set saved as a text file. This file has some additional comments I inserted describing how the actions work.

12 Scripts for actions

Also to do what you want to do you need to script it for you want to get the canvas size (actions can not do that as you know) of one document and change the canvas size of a second document. Actually I don't think you know what you want to do or you can not describe what you want to do well.  For you think  that Canvas Size and Image size are the same function which is not the case.

I do not think you realize that all layers in a layered document do not always have the same size that layers can have different aspect ratios and sizes and can have a  size different then the canvas be larger or smaller then that size.

JJMack
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
Community Expert ,
Mar 18, 2011 Mar 18, 2011

I little demo using screen captures to try to help you undetstand Photoshop ships with a sample tiff file Ducky.tif 500 pixel wide 546 high I opened that image in Photoshop opened as a single layer "Background" I converted the to a normal layer Layer 0 and added a 5px black stroke around it and duplicated it four times. Then I filled the original layer 0 with white.  I then mover the four layers above the white layer about. Notice the Canvas Size Dialog in each all before OK was clicked. Notice how in the third capture I adding 1000 pixels to the Width and Hight Notice in the fourth all layers are still 500 Pixels by 546 Pixels however the canvas size is now 1500 px by 1546 px. Canvas Size only changes layers boundries..... Link  http://www.mouseprints.net/old/dpr/LayerSizes.jpg image http://www.mouseprints.net/old/dpr/LayerSizes.jpg

JJMack
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 19, 2011 Mar 19, 2011

A script need to reference a document before it can work with it. You did not tell the order the images were opened or their names. The code below will get the canvas size of the first opened document and apply it to the second opened.

var w = app.documents[0].width;// the width of the first open document
var h = app.documents[0].height;
app.activeDocument = app.documents[1];// make sure the second open document is active
app.activeDocument.resizeCanvas (w, h);

If you want the document order switched just swap the 0 and 1.

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
Contributor ,
Mar 19, 2011 Mar 19, 2011

Thanks for all the help guys!  I actually solved it myself yesterday, but was unable to get into the forums to post what I came up with.  Michael hit it almost spot on though!  The one document is always named original.psd, the other is a random image file such as .jpg that is opened with a batch process.  Anyway, here's the code I am using, and I included quotes in case anyone else might have use for it.

//store's the batch opened file name for later use.

var myDoc = app.activeDocument;

//stores the source file name
var theLayerSetDoc = app.documents.getByName("original.psd");

//sets the focus on the source file

app.activeDocument = theLayerSetDoc;

//changes the ruler units to pixels

preferences.rulerUnits = Units.PIXELS;

//these 2 lines grab the current document's width and height in pixels and stores them for use later

imageWidth = activeDocument.width.as('px');
imageHeight = activeDocument.height.as('px');

//changes the active document back to the batch opened file (also works with manually opened files, just focus on this document before running the script)

app.activeDocument = myDoc;

//ensures the ruler is set to pixels in this document as well, then resizes the canvas based on the previously stored width and height form the original.psd.
preferences.rulerUnits = Units.PIXELS;
activeDocument.resizeCanvas(imageWidth,imageHeight,AnchorPosition.MIDDLECENTER);

//remaining unrelated script follows here

Michael basically had the code that I was looking for though.  Thanks again for the help though!

DGolberg

Edit: Forgot to mention; the actions leading up to the script handle converting the image file's background to the layer "Layer 1".  I didn't see the need to do this in script, so decided to leave it as an action.

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