Resizing an Image by selection size

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Revnart
Posts: 9
Joined: Wed Sep 28, 2016 8:56 am

Resizing an Image by selection size

Post by Revnart »

Hello I just wanted to ask if this is possible to do:

I have an image opened and create selection (for example marquee)
Now run the script which:
Record that selection is 14cm wide (lets assume that all images I use are 72PPI)
ask user to input value, for example 173cm
then it calculates 173/14 = 12,35
and change image size by 1235% (which comes from previous calculation)

What can be done, what can't? what are limitations in such example?

Thank you for your help :)
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Resizing an Image by selection size

Post by Kukurykus »

Code: Select all

$.level = 0, preferences.rulerUnits = Units.CM
try {
(bnd = (aD = activeDocument).selection.bounds)[2] - bnd[0], v = prompt('Enter the value:', '');
(/^\d*$/).test(v) ? (f = v / 14, aD.resizeImage(aD.width * f, aD.height * f)) : alert('That wasn\'t number!')
}
catch (errSel) {alert('Make selection to preceed...')}
Revnart
Posts: 9
Joined: Wed Sep 28, 2016 8:56 am

Re: Resizing an Image by selection size

Post by Revnart »

It works great, fast and exactly as I described thank you :)

Code: Select all

f  = v / 14
What should I put instead of "14" which is static value, to divide not by 14 but by current selection width?

EDIT:
Ok I made it :)

Code: Select all

$.level = 0, preferences.rulerUnits = Units.CM
try {
as = (bnd = (aD = activeDocument).selection.bounds)[2] - bnd[0];
v = prompt('Docelowa szerokość:', '');
(/^\d*$/).test(v) ? (f = v / as, aD.resizeImage(aD.width * f, aD.height * f)) : alert('Podawaj tylko liczby.')
}
catch (errSel) {alert('Najpierw zaznacz element')}
this will now work with value from selection width :) Once again thank you for your help :)
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Resizing an Image by selection size

Post by Kukurykus »

Yes I was too fast writing that code, so I forgot to change 14 to proper selection in document. But I did another mistake! I didn't add '.value' part at end of selection width. Use corrected code once again to get proper result this time:

Code: Select all

$.level = 0, preferences.rulerUnits = Units.CM
try {
if ((/^\d*$/).test(v = prompt('Enter the value:', ''))) {
f = v / ((bnd = (aD = activeDocument).selection.bounds)[2] - bnd[0]).value
aD.resizeImage(aD.width * f, aD.height * f)
}
else alert('That wasn\'t number!')
}
catch (errSel) {alert('Make selection to preceed...')}
Revnart
Posts: 9
Joined: Wed Sep 28, 2016 8:56 am

Re: Resizing an Image by selection size

Post by Revnart »

Thank you :) Works great :)