Odd, thrown error.

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Asellia
Posts: 4
Joined: 2019-06-19T21:37:58-07:00
Authentication code: 1152

Odd, thrown error.

Post by Asellia »

I am unsure if this is a bug, or just me having no idea what I'm doing-- I've been hammering at this for a bit with basically no knowledge on the system. Basically, I'm attempting to resize (on windows 10) all jpg files in a directory to the nearest power of 2 dimensions (lower, not higher). It, however, is throwing me some errors. I notice in said errors it shows the power of two dimensions, though-- so I hope I'm close. I'm rather horrid at coding, and I sort of patchworked a lot of different code I found through google- this seemed like it's pretty close, thus why I'm asking.

Code: Select all

FOR %a in (D:\Mods\Images\*.jpg) DO D:\Applications\ImageMagick\magick.exe %a -resize ${"%[fx:2^(ceil(log(w)/log(2)))/2]"}x${"%[fx:2^(ceil(log(h)/log(2)))/2]"} D:\Mods\Images2\%a
I am pasting this into command prompt, basically.

The errors are all along the lines of:

Code: Select all

C:\WINDOWS\system32>D:\Applications\ImageMagick\magick.exe D:\Mods\Images\httpiimgurcomztwkBXfjpg.jpg -resize ${"%[fx:2^(ceil(log(w)/log(2)))/2]"}x${"%[fx:2^(ceil(log(h)/log(2)))/2]"} D:\Mods\Images2\D:\Mods\Images\httpiimgurcomztwkBXfjpg.jpg
magick.exe: invalid argument for option '-resize' '${2048}x${1024}' at CLI arg 2 @ error/operation.c/CLISimpleOperatorImage/3170.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Odd, thrown error.

Post by fmw42 »

Try removing the ${ and the ending }. I do not think that is correct either for Windows or Unix. Sorry, I am not a Windows user.

Code: Select all

FOR %a in (D:\Mods\Images\*.jpg) DO D:\Applications\ImageMagick\magick.exe %a -resize "%[fx:2^(ceil(log(w)/log(2)))/2]"x"%[fx:2^(ceil(log(h)/log(2)))/2]" D:\Mods\Images2\%a
Does that work. Also if this is in a .bat script, then % needs to be doubled. Also ^ in Windows is an escape and would need to be doubled.

But if this does not work, wait for a Windows user to help and correct my comments.
Asellia
Posts: 4
Joined: 2019-06-19T21:37:58-07:00
Authentication code: 1152

Re: Odd, thrown error.

Post by Asellia »

Sadly, no good- thank you though for trying to help, though. I wasn't too sure what to expect given I basically frankenstein'd this together, and hoped it worked. I'm not the best when it comes to this sort of thing, sort of just glad I've got it this far.

Edit: Using that code, in case anyone needs to know, it turns into:

Code: Select all

 Invalid argument @ error/blob.c/OpenBlob/3497.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Odd, thrown error.

Post by snibgo »

Removing ${ and } makes it work for me, in Windows 8.1.

The output filename isn't good. A typical value for %a is "D:\Mods\Images\abcd.jpg", so the output filename would be "D:\Mods\Images2\D:\Mods\Images\abcd.jpg".
snibgo's IM pages: im.snibgo.com
Asellia
Posts: 4
Joined: 2019-06-19T21:37:58-07:00
Authentication code: 1152

Re: Odd, thrown error.

Post by Asellia »

Thanks, that seems.. close. Replacing it with just %a seems to not resize anything, though. Is it because it's the same output file name, now? Is there a way to output it into a new folder, with the same filename? All I know is atm it goes through all the files, and proceeds to resize nothing despite themn ot being power of two. If I replace resize with -format, and use info:, it outputs what it SHOULD turn into, however.

Edit: I could be mistaken, but it may be resizing them, but the dimensions when I highlight the files with my cursor is still the old dimensions.

Edit 2: It appears the images just halved their width/height, and did not goto the next power of 2 lower. Is my math just bad? Or do I need to make it ignore aspect ratio?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Odd, thrown error.

Post by snibgo »

For the output filename, you might use "D:\Mods\Images2\%~nxa". This takes the name and extension from %a, with a new drive and path.

On resizing, I don't know what you want. If the input is 400x200, what should the output be? Likewise 512x512? etc.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Odd, thrown error.

Post by fmw42 »

It seems to work except if the image is already a power of 2, then it goes down one level of 2.

Code: Select all

dim=10
convert xc: -format "%[fx:2^(ceil(log($dim)/log(2)))/2]" info:
8

Code: Select all

dim=8
convert xc: -format "%[fx:2^(ceil(log($dim)/log(2)))/2]" info:
4

So perhaps you need to test first if already a power of 2.
Asellia
Posts: 4
Joined: 2019-06-19T21:37:58-07:00
Authentication code: 1152

Re: Odd, thrown error.

Post by Asellia »

I got it working, yeah-- I ended up simply scaling them all up, rather than down -- to insure readability. Some filesizes went up, but thats not an issue, and its all legible/working after a lot of fiddling with contrast/gamma, and some filters. Thank you so much for the help!
Post Reply