Page 1 of 2

Get one layer from Imagemagick

Posted: Mon May 15, 2023 11:59 am
by Fredrik
Hi!

Quite new to Switch but have created a Switch/imagemagick flow that works fine, but with photoshop files i would like to add a couple of characters in my imagemagick command but i can't figure out how.

My IM command looks like this:
"%1" -resize [Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"]x[Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"] -sharpen 3 "%2"

In imagemagick there is a option to extract one layer only from photoshop images, like this "convert <filename>.psd[0]" ...

But how do i add the brackets and number to my IM command in Switch?
"%1[0]" -resize [Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"]x[Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"] -sharpen 3 "%2"
gives me an error "unknown variable"

Any help would be much appreciated!

Re: Get one layer from Imagemagick

Posted: Mon May 15, 2023 1:05 pm
by JimmyHartington
I have not tried, but may you need to escape the brackets with \.
Try with this:
"%1\[0\]" -resize [Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"]x[Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"] -sharpen 3 "%2"

Re: Get one layer from Imagemagick

Posted: Tue May 16, 2023 9:24 am
by Fredrik
Thanks for the suggestion "%1\[0\]" didn't work, neither did "%1\[0]\" or "\%1[0]\"

Re: Get one layer from Imagemagick

Posted: Tue May 16, 2023 9:44 am
by JimmyHartington
Fredrik wrote: Tue May 16, 2023 9:24 am Thanks for the suggestion "%1\[0\]" didn't work, neither did "%1\[0]\" or "\%1[0]\"
Then I think this could be a case for someone smarter than me. :D

Re: Get one layer from Imagemagick

Posted: Wed May 17, 2023 10:05 am
by freddyp
That is because anything between [] is considered to be a variable in a single-line text with variables and [0] is not a valid Switch variable.

My advice when working with more complex commands in the context of "Execute command" is to put everything in a batch file/shell script. In "Command or path" you select the bat/sh file and in "Arguments" you specify a list of, well, arguments for the bat/sh:
"%1" "%2" [SomeSwitchVariable1] [SomeSwitchVariable2] etc.

In a Windows batch file you use %1, %2, %3, ... following the order of the arguments as you specified them in "Arguments" and place them in the right place in the command, e.g.

"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" %1[0] --resize %3X%4 %2 (do not use double quotes)

In a Mac shell script it would look like:
#!/bin/bash
/usr/local/bin/convert "$1" -resize $3X$4 "$2" (note that here double quotes are required for paths to avoid problems with spaces and that the arguments are prefixed with a $ sign and not a % sign)

You must also not forget to make the .sh file executable: chmod +x pathToShellScript

Re: Get one layer from Imagemagick

Posted: Tue May 23, 2023 5:20 pm
by jan_suhr
freddyp wrote: Wed May 17, 2023 10:05 am That is because anything between [] is considered to be a variable in a single-line text with variables and [0] is not a valid Switch variable.

My advice when working with more complex commands in the context of "Execute command" is to put everything in a batch file/shell script. In "Command or path" you select the bat/sh file and in "Arguments" you specify a list of, well, arguments for the bat/sh:
"%1" "%2" [SomeSwitchVariable1] [SomeSwitchVariable2] etc.

In a Windows batch file you use %1, %2, %3, ... following the order of the arguments as you specified them in "Arguments" and place them in the right place in the command, e.g.

"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" %1[0] --resize %3X%4 %2 (do not use double quotes)

In a Mac shell script it would look like:
#!/bin/bash
/usr/local/bin/convert "$1" -resize $3X$4 "$2" (note that here double quotes are required for paths to avoid problems with spaces and that the arguments are prefixed with a $ sign and not a % sign)

You must also not forget to make the .sh file executable: chmod +x pathToShellScript
And there is an app for that :-) It let you create the shell script or bat file with Switch variables.
https://www.enfocus.com/en/appstore/pro ... and-friend

Re: Get one layer from Imagemagick

Posted: Tue May 23, 2023 5:29 pm
by magnussandstrom
jan_suhr wrote: Tue May 23, 2023 5:20 pm
And there is an app for that :-) It let you create the shell script or bat file with Switch variables.
https://www.enfocus.com/en/appstore/pro ... and-friend
But will it really work with that app, Jan? You still need to insert [1] into the command some how.

Re: Get one layer from Imagemagick

Posted: Wed May 24, 2023 1:37 pm
by Fredrik
freddyp wrote: Wed May 17, 2023 10:05 am That is because anything between [] is considered to be a variable in a single-line text with variables and [0] is not a valid Switch variable.

My advice when working with more complex commands in the context of "Execute command" is to put everything in a batch file/shell script. In "Command or path" you select the bat/sh file and in "Arguments" you specify a list of, well, arguments for the bat/sh:
"%1" "%2" [SomeSwitchVariable1] [SomeSwitchVariable2] etc.

In a Windows batch file you use %1, %2, %3, ... following the order of the arguments as you specified them in "Arguments" and place them in the right place in the command, e.g.

"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" %1[0] --resize %3X%4 %2 (do not use double quotes)

In a Mac shell script it would look like:
#!/bin/bash
/usr/local/bin/convert "$1" -resize $3X$4 "$2" (note that here double quotes are required for paths to avoid problems with spaces and that the arguments are prefixed with a $ sign and not a % sign)

You must also not forget to make the .sh file executable: chmod +x pathToShellScript
Thank's for your input. I have created a .bat file containing this code, named Resize_psd.bat
C:/Program Files/ImageMagick-7.1.1-Q16-HDRI/magick.exe %1[0] -resize %2x%3 -sharpen 3 %4.tif

In my Execute Command i have entered the following
Command: C:/Program Files/ImageMagick-7.1.1-Q16-HDRI/MGG_Scripts/Resize_psd.bat
Arguments: "%1" "[Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"]" "[Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"]" "%2"

With a sample job the arguments looks fine and i have also tried without "" on my size values but it doesn't make any difference. But i don't get any output file from Image Magick.

If i try to alter my bat file to this magick.exe C:\Users\Admin\Desktop\Test-2560x1440.psd[0] -resize 1700x1700 -sharpen 3 Test.tif and executes it from the command prompt it works just fine. So i changed my Switch bat file to magick.exe %1[0] -resize %2x%3 -sharpen 3 %4.tif but neither did that do any difference.

Edit:
Also tried to use my Switch bat file from the command prompt and it works fine, but not from Switch.

Re: Get one layer from Imagemagick

Posted: Wed May 24, 2023 3:34 pm
by magnussandstrom
Fredrik wrote: Wed May 24, 2023 1:37 pm
freddyp wrote: Wed May 17, 2023 10:05 am That is because anything between [] is considered to be a variable in a single-line text with variables and [0] is not a valid Switch variable.

My advice when working with more complex commands in the context of "Execute command" is to put everything in a batch file/shell script. In "Command or path" you select the bat/sh file and in "Arguments" you specify a list of, well, arguments for the bat/sh:
"%1" "%2" [SomeSwitchVariable1] [SomeSwitchVariable2] etc.

In a Windows batch file you use %1, %2, %3, ... following the order of the arguments as you specified them in "Arguments" and place them in the right place in the command, e.g.

"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" %1[0] --resize %3X%4 %2 (do not use double quotes)

In a Mac shell script it would look like:
#!/bin/bash
/usr/local/bin/convert "$1" -resize $3X$4 "$2" (note that here double quotes are required for paths to avoid problems with spaces and that the arguments are prefixed with a $ sign and not a % sign)

You must also not forget to make the .sh file executable: chmod +x pathToShellScript
Thank's for your input. I have created a .bat file containing this code, named Resize_psd.bat
C:/Program Files/ImageMagick-7.1.1-Q16-HDRI/magick.exe %1[0] -resize %2x%3 -sharpen 3 %4.tif

In my Execute Command i have entered the following
Command: C:/Program Files/ImageMagick-7.1.1-Q16-HDRI/MGG_Scripts/Resize_psd.bat
Arguments: "%1" "[Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"]" "[Job.PrivateData:Key="[Job.Hierarchy:Index="3"]mm"]" "%2"

With a sample job the arguments looks fine and i have also tried without "" on my size values but it doesn't make any difference. But i don't get any output file from Image Magick.

If i try to alter my bat file to this magick.exe C:\Users\Admin\Desktop\Test-2560x1440.psd[0] -resize 1700x1700 -sharpen 3 Test.tif and executes it from the command prompt it works just fine. So i changed my Switch bat file to magick.exe %1[0] -resize %2x%3 -sharpen 3 %4.tif but neither did that do any difference.

Edit:
Also tried to use my Switch bat file from the command prompt and it works fine, but not from Switch.
My guess is that this approach will not populate %1 %2 %3 %4 with any information, since the Resize_ps.bat is a static file and will just be executed 'as is'.

Re: Get one layer from Imagemagick

Posted: Thu May 25, 2023 8:53 am
by Fredrik
magnussandstrom wrote: Wed May 24, 2023 3:34 pm My guess is that this approach will not populate %1 %2 %3 %4 with any information, since the Resize_ps.bat is a static file and will just be executed 'as is'.
Perhaps you are correct, i don't get any more error messages in Switch than that the expected output file doesn't exists. Is this an issue with Switch then? Because if i try to activate the bat file from the command prompt with the variables it works just fine.

Bat file contains.
magick.exe %1[0] -resize %2 -sharpen 3 %3

Command.
C:\Users\Admin\Desktop>Resize_psd_Test.bat C:\Users\Admin\Desktop\Test-2560x1440.psd 1700x1700 Test.tif

This works and creates a 1700 px tif image in the same location since i don't point at another one in my command.

Re: Get one layer from Imagemagick

Posted: Thu May 25, 2023 9:34 am
by jan_suhr
This works for me with the BAT-file created with the app "ExecuteCommand friend"

This is my BAT-file, you can add more variables if you want.

Code: Select all

set filename=%1
set output=%2
"C:\Program Files\ImageMagick-7.0.11-Q16-HDRI\convert.exe" %filename%[1] -resize [Job.PrivateData:Key="size"]x[Job.PrivateData:Key="size"] -sharpen 3 %output%
Flow looks like this since we have to save the BAT-file for execute command to pick it up. It will be over written with the next job.
Snag_3d848769.png
Snag_3d848769.png (14.71 KiB) Viewed 47513 times
Fredrik, you can email me if you need more help with it.
jan.suhr@colorconsult.se

Re: Get one layer from Imagemagick

Posted: Thu Jun 01, 2023 1:11 pm
by Fredrik
Hi Jan!

How does your execute command look like? Command or path and arguments?
I create a bat file with Execute Command Friend and it looks like this and i think it is correct.

set filename=%1
set output=%2
C:/Program Files/ImageMagick-7.1.1-Q16-HDRI/magick.exe C:/Users/Admin/Roaming/Enfocus/Switch Server/PropertySets/12/PSD_Files/%filename%[0] -resize 1772x1772 -sharpen 3 %output%

The bat file is saved as input filename .bat and placed in a temp folder, the psd file is also placed in a temp folder. When picked up by switch i run a execute command where i try to use the bat file, maybe this is not the correct way?

Re: Get one layer from Imagemagick

Posted: Thu Jun 01, 2023 10:10 pm
by jan_suhr
Here is the settings.

The %1 variable that Execute command uses is the full path to the incoming file. So you just set it up as in the attached screen shot and it will work for you.

Snag_64443c0d.png
Snag_64443c0d.png (21.7 KiB) Viewed 47450 times

Re: Get one layer from Imagemagick

Posted: Fri Jun 02, 2023 10:34 am
by Fredrik
Thanks! It works now, but i was afraid there would be an issue if multiple users uses this flow at the same time since we are overwriting the same .bat file and when testing this by putting two images in the flow with different sizes they both came out with the same size. So there is an issue with this.

I will try to go back to my first set up where every job gets it's own bat file.

Re: Get one layer from Imagemagick

Posted: Fri Jun 02, 2023 10:57 am
by JimmyHartington
You could set a private data key to the unique prefix.
Save the bat file as the private data unique prefix.
And run the command with private data unique prefix.
Just remember to set the private data before splitting the flow.