[SOLVED]: Resizing/Cropping based on info from another image

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
LDAsh
Posts: 28
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

[SOLVED]: Resizing/Cropping based on info from another image

Post by LDAsh » 2019-02-11T03:31:51-07:00

Leading on from this topic:-
viewtopic.php?f=1&t=35471
and very similar to concepts discussed here:-
https://www.imagemagick.org/discourse-s ... hp?t=33209

Although, the condition is that it's simply a Windows BAT file and using IM6, would not involve any fancy scripting environment.

I would like to crop/resize an image to match the dimensions of another image, since the exact resolution could never be known for each case.
Although not involving fancy programming, storing and then retrieving info from a TXT file might do the trick? If IM has such a function?
I honestly have no idea but ideally it would be within one command and not involve extras, if it's possible at all?

Something like this ridiculous fantasy example:-
"convert input.png -resize(%w="match.png"x%h="match.png") output.png"
The fantasy result would be that output.png would be input.png but with the resolution of match.png.
Last edited by LDAsh on 2019-02-11T06:34:56-07:00, edited 1 time in total.

snibgo
Posts: 11579
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing/Cropping based on info from another image

Post by snibgo » 2019-02-11T05:45:19-07:00

Code: Select all

for /F "usebackq" %%L in (`%IM%identify ^
  -format "MYSIZE=%%wx%%h" ^
  match.png`) do set %%L

echo MYSIZE=%MYSIZE%

%IM%convert input.png -resize %MYSIZE% output.png
This makes output.png fit into a box the same size as match.png. Use "!" if you want the exact size.
snibgo's IM pages: im.snibgo.com

LDAsh
Posts: 28
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

Re: Resizing/Cropping based on info from another image

Post by LDAsh » 2019-02-11T06:31:45-07:00

Wow, too awesome. Got the resize and crop happening, and figured out how to separate the X and Y values all by myself. I guess I can just do ~unlimited values for and from whatever I want, it seems, anything I can get out of identify and into anything that can be fed a number. You've just helped me open up a whole bunch of new possibilities.
Very powerful stuff, cheers snibgo!

Post Reply