how to crop jpg and maintain the dimension

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
janetsmith

how to crop jpg and maintain the dimension

Post by janetsmith »

I am trying to crop DSC03574.jpg into smaller file, test7.jpg. Notice the parameter 140x190-38-67. I use negative value because I want to have extra space in the result picture. I wish to have white color fill up the space. But the result image is 102x123, not 140x190.

How to make it become 140x190, with the extra space filled with white color?

F:\test>identify DSC03574.JPG
DSC03574.JPG JPEG 640x480 640x480+0+0 DirectClass 8-bit 147.811kb 0.030u 0:01

F:\test>convert -crop 140x190-38-67 DSC03574.JPG test7.jpg

F:\test>identify test7.jpg
test7.jpg JPEG 102x123 102x123+0+0 DirectClass 8-bit 14.0547kb 0.000u 0:01
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to crop jpg and maintain the dimension

Post by anthony »

Use -crop with a '!' flag followed by -flatten This tells crop to do a viewport crop, adjusting the virtual canvas to match the area cropped. The -flatten then 'fills out' the virtual canvas
with real pixels the color of the current background color (which can be 'none' if you want transparency).

See IM Examples... http://www.imagemagick.org/Usage/crop/#crop_viewport
and http://www.imagemagick.org/Usage/mosaics/#flatten
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
spammy63
Posts: 12
Joined: 2016-02-26T02:20:29-07:00
Authentication code: 1151

Re: how to crop jpg and maintain the dimension

Post by spammy63 »

Seems not working anymore - or at least the same way - under IM V7 (7.0.8-42).
Tried with 'magick' also with 'magick convert' - and drives me crazy.
Any ideas suggestions - whats special here with IM V7 ?
See examples: Tried to move crop and fill the gaps if crop exteds the original image view) - but the -flatten seems to overwrite left top, crop seems to ignore offset:

magick rose.gif -gravity NorthWest -crop 40x40+0+0 -background blue - flatten rb.gif
magick rose.gif -gravity NorthWest -crop 40x40+15+15 -background blue - flatten rb5.gif
magick rose.gif -gravity NorthWest -crop 40x40+15+15 -background blue - flatten rb15.gif
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to crop jpg and maintain the dimension

Post by snibgo »

Remove the space between "-" and "flatten".
snibgo's IM pages: im.snibgo.com
spammy63
Posts: 12
Joined: 2016-02-26T02:20:29-07:00
Authentication code: 1151

Re: how to crop jpg and maintain the dimension

Post by spammy63 »

Yes, corrected (but used it the right way in my external batch file ....) - also with exclamation mark behind geometry entry:

magick rose.gif -gravity NorthWest -crop 40x40+0+0! -background blue -flatten rb0.gif https://ibb.co/v4dnZwf
magick rose.gif -gravity NorthWest -crop 40x40+5+5! -background blue -flatten rb5.gif https://ibb.co/xgXj72p
magick rose.gif -gravity NorthWest -crop 40x40+10+10! -background blue -flatten rb10.gif https://ibb.co/G5x65fP
magick rose.gif -gravity NorthWest -crop 40x40+20+20! -background blue -flatten rb20.gif https://ibb.co/tJCg39y
magick rose.gif -gravity NorthWest -crop 40x40+50+50! -background blue -flatten rb50.gif https://ibb.co/4pW2rF4

Offset ignored, background 'overwrites' from left top - any new option in v7 ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to crop jpg and maintain the dimension

Post by snibgo »

When you change the offset, the result is different. But you say "Offset ignored". I don't understand your problem.

Your results seem to be the same as the example page, https://www.imagemagick.org/Usage/crop/#crop_viewport
snibgo's IM pages: im.snibgo.com
spammy63
Posts: 12
Joined: 2016-02-26T02:20:29-07:00
Authentication code: 1151

Re: how to crop jpg and maintain the dimension

Post by spammy63 »

many thanks snibgo .... seems to be a version conflict between V6.9.3 and newer ones including V7. So I assume, the newer ones do it "the right way". But the older version's results image works for me in a more logical way and I need exactly that output.
So my question is : Which option I forgot in the newer versions to get the same result images ?

Old:
convert lena.png -crop 512x512+50+50! -background blue -flatten lena50r-V6-9-3.gif
https://ibb.co/tBC2xFh

V7:
magick lena.png -crop 512x512+50+50! -background blue -flatten lena50r-V7x.gif
https://ibb.co/LYbk5qv
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to crop jpg and maintain the dimension

Post by snibgo »

Please post a link to your input, lena.png.
snibgo's IM pages: im.snibgo.com
spammy63
Posts: 12
Joined: 2016-02-26T02:20:29-07:00
Authentication code: 1151

Re: how to crop jpg and maintain the dimension

Post by spammy63 »

Thanks for your attention.
It's Lena as usual in 512x512:
https://ibb.co/zGsb1kd
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to crop jpg and maintain the dimension

Post by snibgo »

(There are so may versions of Lena, I wanted to ensure I used the same one.)

With your command and input, with both v6.9.9.50 and v7.0.7-28, I get the blue at right and bottom, the same as your v6.9.3 result, and different to your v7.0.8-42 result.

I conclude there is a change (a bug?) in 7.0.8-42.
snibgo's IM pages: im.snibgo.com
spammy63
Posts: 12
Joined: 2016-02-26T02:20:29-07:00
Authentication code: 1151

Re: how to crop jpg and maintain the dimension

Post by spammy63 »

Thanks Snibgo ... sadly I conclude the same - and no solution for that.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to crop jpg and maintain the dimension

Post by snibgo »

To help catch the change/bug, please insert "+write info:" before "-flatten" in both versions (v6 and v7). I get:

Code: Select all

convert lena.png -crop "512x512+50+50^!" -background blue +write info: -flatten out.png

lena.png PNG 462x462 512x512+0+0 8-bit sRGB 0.391u 0:01.577
... and the same result for v7.
snibgo's IM pages: im.snibgo.com
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: how to crop jpg and maintain the dimension

Post by 246246 »

@snibgo v 6.9.9-40 is the same with you.

% convert lena.png -background blue -crop 512x512+50+50\! +write info: -flatten out.png
lena.png PNG 462x462 512x512+0+0 8-bit sRGB 0.010u 0:00.000

Here is the result from v 7.0.8-42
% magick lena.png -background blue -crop 512x512+50+50\! +write info: -flatten out.png
lena.png PNG 462x462 512x512+50+50 8-bit sRGB 453962B 0.010u 0:00.001

Also, v6.9.10-42 is the same with 7.0.8-42
% convert lena.png -background blue -crop 512x512+50+50\! +write info: -flatten out.png
lena.png PNG 462x462 512x512+50+50 8-bit sRGB 453962B 0.000u 0:00.000

Also, this might not be your help, it seems using -extent with negative value still behaves the same way between 6.9.9-40 and 7.0.8-42 for various -gravity.

v6.9.9-40
% convert lena.png -background blue -gravity NorthWest -extent 512x512-50-50\! +write info: -flatten out.png
lena.png PNG 512x512 512x512+0+0 8-bit sRGB 0.000u 0:00.000

v7.0.8-42
% magick lena.png -background blue -gravity NorthWest -extent 512x512-50-50\! +write info: -flatten out.png
lena.png PNG 512x512 512x512+0+0 8-bit sRGB 453962B 0.010u 0:00.001
Post Reply