Photoshop scale - compare Height/Width with calculation

Hi There,

I have a script that generates JPG’s with different dimensions.
In the current situation the JPEG’s are scaled like this:

If Width > Height : scale Width to 100, 225 and 500 pixels
iIf Width <= Height : scale Height to 80, 180 and 400 pixels

But they must fit within 100x80 ; 225x180 ; 500x400 (Ratio 5:4)

Is it possible to add the following:

if 4x Width > 5x Height : scale Width to 100, 225 and 500 pixels
if 4x Width <= 5x Height : scale Height to 80, 180 and 400 pixels

Example:

Image W200 x H190 pixels : 4x200=800 ; 5x190=950 → must be scaled to a Height of 80 pixels.
In the current situation the Width is scaled to 100 pixels and Height to 95 pixels.
This doesn’t fit in 100x80.

Can this be done ???


property theFileType_A : {"8BPS"}
property theFileType_B : {"EPSF"}
property newName : ""
property Saved_State_2 : missing value

set Input_Folder to (choose folder with prompt "Kies een Folder met Leen Bakker EPS of PSD beelden" without invisibles) as text

set LAY_Folder to Input_Folder & "LAY_OUT:"
set JPEG_Folder to Input_Folder & "JPEG_OUT:"

do shell script "/bin/mkdir -p " & quoted form of POSIX path of Input_Folder & "/{LAY_OUT,JPEG_OUT}/"
tell application "Finder" to set File_List to files in folder Input_Folder

tell application "Adobe Photoshop CS3"
	activate
	set display dialogs to never
	set User_Rulers to ruler units of settings
	set ruler units of settings to pixel units
	repeat with This_File in File_List
		set theFileType to file type of (info for This_File as alias)
		if theFileType is in theFileType_A or theFileType is in theFileType_B then
			with timeout of 180 seconds
				open (This_File as alias)
				set Doc_Ref to the current document
				tell Doc_Ref
					set Doc_Name to name
					set Base_Name to my getBaseName(Doc_Name)
					convert to profile "sRGB IEC61966-2.1" intent perceptual with blackpoint compensation and dithering
					if (bits per channel is sixteen) then
						set bits per channel to eight
					end if
					set Saved_State_1 to current history state
					resize image resolution 72 resample method bicubic sharper
					if theFileType is in theFileType_B then
						set New_Path to LAY_Folder & Base_Name & ".eps.lay"
						save Doc_Ref in file New_Path as Photoshop EPS with options {class:EPS save options, embed color profile:true, encoding:high quality JPEG, preview type:eight bit TIFF} appending no extension with copying
					else
						set New_Path to LAY_Folder & Base_Name & ".psd.lay"
						save Doc_Ref in file New_Path as Photoshop format with options {class:Photoshop save options, save alpha channels:true, save layers:true, embed color profile:true} appending no extension with copying
					end if
					set current history state to Saved_State_1
					if exists (path items whose kind is clipping) then
						set Clip to (name of every path item whose kind is clipping)
						create selection path item (item 1 of Clip) feather amount 0 with antialiasing
						invert selection
						fill selection with contents {class:RGB color, red:255, green:255, blue:255}
						deselect
					end if
					set Pixel_Height to height
					set Pixel_Width to width
					
					set newName to JPEG_Folder & Base_Name
					set Saved_State_2 to current history state
				end tell
				if Pixel_Height > Pixel_Width then
					my Resize_Save_Doc(Doc_Ref, 400, 72, true)
					my Resize_Save_Doc(Doc_Ref, 180, 72, true)
					my Resize_Save_Doc(Doc_Ref, 80, 72, true)
					
				else
					my Resize_Save_Doc(Doc_Ref, 500, 72, false)
					my Resize_Save_Doc(Doc_Ref, 225, 72, false)
					my Resize_Save_Doc(Doc_Ref, 100, 72, false)
				end if
				close Doc_Ref without saving
			end timeout
		end if
	end repeat
	set ruler units of settings to User_Rulers
end tell


on Resize_Save_Doc(doc, resizeValue, _res, _flag)
	tell application "Adobe Photoshop CS3"
		set Doc_Ref to the current document
		set filterOptions to {class:filter options, amount:25, radius:1.0, threshold:0}
		tell Doc_Ref
			set Pixel_Height to height
			set Pixel_Width to width
			if _flag then
				tell doc to resize image height resizeValue resolution _res
				filter every layer using unsharp mask with options filterOptions
			else
				tell doc to resize image width resizeValue resolution _res
				filter every layer using unsharp mask with options filterOptions
			end if
			if height = 400 then
				set New_Path to newName & "_" & "Portrait" & "_" & "L" & ".jpg"
			end if
			if height = 180 then
				set New_Path to newName & "_" & "Portrait" & "_" & "M" & ".jpg"
			end if
			if height = 80 then
				set New_Path to newName & "_" & "Portrait" & "_" & "S" & ".jpg"
			end if
			if width = 500 then
				set New_Path to newName & "_" & "Landscape" & "_" & "L" & ".jpg"
			end if
			if width = 225 then
				set New_Path to newName & "_" & "Landscape" & "_" & "M" & ".jpg"
			end if
			if width = 100 then
				set New_Path to newName & "_" & "Landscape" & "_" & "S" & ".jpg"
			end if
			save doc in file New_Path as JPEG with options {quality:6, embed color profile:false} with copying
			tell doc to set current history state to Saved_State_2
		end tell
	end tell
end Resize_Save_Doc

on getBaseName(fName)
	set baseName to fName
	repeat with idx from 1 to (length of fName)
		if (item idx of fName = ".") then
			set baseName to (items 1 thru (idx - 1) of fName) as string
			exit repeat
		end if
	end repeat
	return baseName
end getBaseName

tell application (path to frontmost application as Unicode text)
	with timeout of 3600 seconds
		display dialog "Beelden zijn verwerkt" buttons {"OK"} default button 1
	end timeout
end tell


If your images must be 5:4 then you have 3 choices. Crop data outside of that ratio, size data to fit and add canvas or distort to ratio.

Isn’t it possible to do an additional rescale while the file is open?

like
if Pixel_Width = 100 and Pixel_Height > 80 then
resize image resolution 72 height pixels 80

Another option would be to reopen the jpegs and check/adjust the width or height but saving them again isn’t that good for the quality.

The thing is that I dont know how to implement option one in my script - largely created by Stefan

–Peter–

.and the Resize_Save_Doc() handler messed up by yourself ;):wink:

I hope this math


if (4 * Pixel_Width > 5 * Pixel_Height) then

is correct.

Sorry for posting the whole script, but I changed a few things for efficiency



property theFileType_A : {"8BPS"}
property theFileType_B : {"EPSF"}
property newName : ""
property Doc_Ref : missing value
property sizeIndex : {"L", "M", "S"}
property Saved_State_2 : missing value

set Input_Folder to (choose folder with prompt "Kies een Folder met Leen Bakker EPS of PSD beelden" without invisibles) as text

set LAY_Folder to Input_Folder & "LAY_OUT:"
set JPEG_Folder to Input_Folder & "JPEG_OUT:"

do shell script "/bin/mkdir -p " & quoted form of POSIX path of Input_Folder & "/{LAY_OUT,JPEG_OUT}/"
tell application "Finder" to set File_List to files in folder Input_Folder

tell application "Adobe Photoshop CS3"
	activate
	set display dialogs to never
	set User_Rulers to ruler units of settings
	set ruler units of settings to pixel units
	repeat with This_File in File_List
		set theFileType to file type of (info for This_File as alias)
		if theFileType is in theFileType_A or theFileType is in theFileType_B then
			with timeout of 180 seconds
				open (This_File as alias)
				set Doc_Ref to the current document
				tell Doc_Ref
					set Doc_Name to name
					set Base_Name to my getBaseName(Doc_Name)
					convert to profile "sRGB IEC61966-2.1" intent perceptual with blackpoint compensation and dithering
					if (bits per channel is sixteen) then
						set bits per channel to eight
					end if
					set Saved_State_1 to current history state
					resize image resolution 72.0 resample method bicubic sharper
					if theFileType is in theFileType_B then
						set New_Path to LAY_Folder & Base_Name & ".eps.lay"
						save Doc_Ref in file New_Path as Photoshop EPS with options {class:EPS save options, embed color profile:true, encoding:high quality JPEG, preview type:eight bit TIFF} appending no extension with copying
					else
						set New_Path to LAY_Folder & Base_Name & ".psd.lay"
						save Doc_Ref in file New_Path as Photoshop format with options {class:Photoshop save options, save alpha channels:true, save layers:true, embed color profile:true} appending no extension with copying
					end if
					set current history state to Saved_State_1
					if exists (path items whose kind is clipping) then
						set Clip to (name of every path item whose kind is clipping)
						create selection path item (item 1 of Clip) feather amount 0 with antialiasing
						invert selection
						fill selection with contents {class:RGB color, red:255, green:255, blue:255}
						deselect
					end if
					set Pixel_Height to height
					set Pixel_Width to width
					
					set newName to JPEG_Folder & Base_Name
					set Saved_State_2 to current history state
				end tell
				if (4 * Pixel_Width > 5 * Pixel_Height) then
					my Resize_Save_Doc({500.0, 225.0, 100.0}, false)
				else
					my Resize_Save_Doc({400.0, 180.0, 80.0}, true)
				end if
				close Doc_Ref without saving
			end timeout
		end if
	end repeat
	set ruler units of settings to User_Rulers
end tell


on Resize_Save_Doc(resizeList, _flag)
	tell application "Adobe Photoshop CS3"
		set filterOptions to {class:filter options, amount:25, radius:1.0, threshold:0}
		repeat with i from 1 to 3
			if _flag then
				tell Doc_Ref
					resize image height (item i of resizeList) resolution 72.0
					filter every layer using unsharp mask with options filterOptions
				end tell
				set New_Path to newName & "_" & "Portrait" & "_" & (item i of sizeIndex) & ".jpg"
			else
				tell Doc_Ref
					resize image width (item i of resizeList) resolution 72.0
					filter every layer using unsharp mask with options filterOptions
				end tell
				set New_Path to newName & "_" & "Landscape" & "_" & (item i of sizeIndex) & ".jpg"
			end if
			save Doc_Ref in file New_Path as JPEG with options {quality:6, embed color profile:false} with copying
			tell Doc_Ref to set current history state to Saved_State_2
		end repeat
	end tell
end Resize_Save_Doc

on getBaseName(fName)
	return text 1 thru ((offset of "." in fName) - 1) in fName
end getBaseName

tell application (path to frontmost application as Unicode text)
	with timeout of 3600 seconds
		display dialog "Beelden zijn verwerkt" buttons {"OK"} default button 1
	end timeout
end tell


Many thanks Stefan.
I will look into your script ASAP

–Peter–