Page 1 of 1

Cropping to Center an Image and Doing This in a Batch

Posted: 2018-08-24T23:18:32-07:00
by Exaskryz
Alright, I've been searching for over an hour and slowly working my way through this.

My version is ImageMagick 7.0.8-10 Q16 x64 2018-08-14 being used on Windows 8.1 x64

First question is a curiosity, since I figured out a workaround, maybe. (Perhaps the workaround is giving me problems).

http://www.imagemagick.org/Usage/thumbnails/#square

This page let me see this code that I like

Code: Select all

  convert thumbnail.gif -set option:size '%[fx:min(w,h)]x%[fx:min(w,h)]' \
          xc:none +swap -gravity center -composite square_internal_2.gif
But the image always failed. It was almost like it was corrupted.

I instead used the longer form code given above it, and that worked.

Code: Select all

  convert thumbnail.gif   -set option:distort:viewport \
     "%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]" \
     -filter point -distort SRT 0  +repage  square_internal.gif
     
So that first question: Why did the first code, which comes from the sample documentation, not work?

Second question, how do I batch name?

I've found stuff like this, including old forum posts, that say I could do something to the effect of

Code: Select all

  convert *.gif   -set option:distort:viewport \
     "%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]" \
     -filter point -distort SRT 0  +repage  square_internal_%[filename:f].gif
     
But that just spits out a single corrupted file called "square_internal_%[filename".... I've alternatively tried square_internal_*.gif with little hope, but that didn't work either.

So, how can I just process all the images in the directory but prefix their name?

The closest I can get so far is "square_internal_1.gif" "square_internal_2.gif" etc, losing the original file names.

Re: Cropping to Center an Image and Doing This in a Batch

Posted: 2018-08-25T05:23:07-07:00
by snibgo
Exaskryz wrote:... used on Windows 8.1 x64
But your commands use bash syntax, with "\" for line-continuation, where Windows CMD or BAT wants "^". And quoting is different, and if these are in Windows BAT scripts you need to double % signs. See http://www.imagemagick.org/Usage/windows/
Exaskryz wrote:... square_internal_%[filename:f].gif
This needs the value of filename:f to be set first, typically using metadata escapes from http://www.imagemagick.org/script/escape.php, eg:

Code: Select all

-set filename:f %t
EDIT to add: As you are using v7, I suggest you use "magick" which gives you the new v7 syntax instead of "convert" which (probably) limits you to v6 syntax.

Re: Cropping to Center an Image and Doing This in a Batch

Posted: 2018-08-25T06:34:27-07:00
by Exaskryz
Hi, thanks for your comments.

In regards to the \ for BASH and stuff, I was just ignoring that completely on Windows; I copy and pasted from the example on the website instead of translating it entirely to what I was doing, since my version of Command Prompt does not have copy and paste (need Win 10 for that, or a third party CMD).

Had no idea what that \ was or that I could use ^ for that instead, so, thank you! I was doing it all one line.

These are not "BAT" scripts, I don't think. I'm not making a .bat file. I was just doing a single line.

Alright, thank you for letting me know about having to set first. That should help.

In response to your edit, here is a full rundown of my text as I type it up again, just to clarify what I've been doing.

(I set up the Magick Image install directory (Program FIles\MagickImage Q... numbers..\) inside my PATH system Environment Variable, using "magick".)

Code: Select all

magick convert "Chicago Tower.jpg" -set option:distort:viewport "%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]" -filter point -distort SRT 0 +repage chicago.jpg
I was able to do that code fine. You're suggesting the same code without the "Convert" command will be better? I'll first try that without file name business.

But that's not working. That didn't crop at all! Just remade the image.

Code: Select all

magick "Chicago Tower.jpg" -set option:distort:viewport "%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]" -filter point -distort SRT 0 +repage chicago.jpg
Like it literally copy and pasted and renamed. So, that's confusing. I'll keep the convert for now, since, well, that works.

Now I'll try the filename syntax.

What does the %t do there? That confuses me.

Code: Select all

magick convert *.jpg -set filename:f %t -set option:distort:viewport "%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]" -filter point -distort SRT 0 +repage "crop %[filename:f].jpg"
But awesome! This worked!

Hoping one day as I keep playing with these each of these parameters start to make sense. Thank you for your help.

Re: Cropping to Center an Image and Doing This in a Batch

Posted: 2018-08-25T07:08:41-07:00
by snibgo
I suggest you use "magick" by itself, not "magick convert". Yeah, it is confusing. We have "convert" or "magick" or "magick convert". I suggest you use "magick" by itself unless there is a good reason for one of the others.
Exaskryz wrote:But that's not working. That didn't crop at all! Just remade the image.
Please post a link to your input. It works fine for me, on my image, using either "magick" or "magick convert" in v7.0.7-28, or "convert" with v6.9.9-50.
Exaskryz wrote:What does the %t do there?
See http://www.imagemagick.org/script/escape.php . "%t" expands to the filename without drive, directory or extension. So if an input file is "C:\mypictures\myfile.jpg", "%t" expands to "myfile". So this sets the variable "filename:t" to the value "myfile". So when that variable is used in the output filename, the value "myfile" is used instead.

Re: Cropping to Center an Image and Doing This in a Batch

Posted: 2018-08-25T07:22:17-07:00
by Exaskryz
In regards to the %t stuff, that sounds like I'm assigning a built-in variable to my custom variable or something? Could I just do something like output file name "crop %"t".jpg" ?

And I realize that I had made a typo earlier. Not the code I shared on the webpage, but the code I put into command prompt. Where ther eis an "[fx:max((h-w)/2,0)], I forgot the opening [. So that's why it worked for you, but not for me. Oops!

Thank you for your time on this issue.

Edit: The below, independent issue is solved. Jump to bottom of post to see.


I'm going to be searching for an answer on another one, particularly why I can't get a transparent background. I will share the problem here, but I understand if you do not answer it since it's a different topic.

Source images are .jpg, but I'm outputting in .png to support the transparency. I've tried this code

Code: Select all

magick crop/*.jpg -set filename:f %t -resize 83x103! -fill "transparent" -rotate 3 "crop/res/%[filename:f].png"
I can do everything fine up through the resize; it's when I'm trying to do this rotation and create a transparent background that is tripping me up. I've tried using "rgba(0,0,0,0.0)" and "rgba(255,255,255,0)" and "none" (none with and without the quotation marks) and nothing is working. It keeps producing a white background.

Thank you in advance.

Solved this by using help from viewtopic.php?f=1&t=34277&p=157493&hili ... nt#p157493. I ditched the -fill parameter and switched to -background none and put that as the parameter after the -set filename:f %t part.

Code: Select all

magick crop/*.jpg -set filename:f %t -background none -resize 83x103! -rotate 3 "crop/res/%[filename:f].png"
Thanks again.

Re: Cropping to Center an Image and Doing This in a Batch

Posted: 2018-08-25T07:53:13-07:00
by snibgo
Exaskryz wrote:...sounds like I'm assigning a built-in variable to my custom variable or something?
Yes. %t is a built-in variable, and filename:f is your custom variable. You can use anything you want instead of "f", so you could have "filename:myownname" or whatever.
Exaskryz wrote:Could I just do something like output file name "crop %"t".jpg" ?
Sadly, no. When a variable is used in an output filename, the variable name must start with "filename:".

On "-rotate", see http://www.imagemagick.org/script/comma ... php#rotate :
Empty triangles in the corners, left over from rotating the image, are filled with the "background" color.
When asking a question that isn't related to the original question, we prefer that people start a new topic. This helps other users who might have the same question to search for an answer.