Cut the sides - or Zooming

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?".
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Cut the sides - or Zooming

Post by Albireo »

Hi!
(for example)
I have an original image with the dimensions: 6668 x 4992pixels
I want to make an thumbnail with max size of 120x120 (it's not too hard)
The command below performs this (+ little scharp on the outcome)

Code: Select all

convert -size 140x140 7748790.jpg -thumbnail 120x120 -sharpen 0x1.0 Image.png
The thumbnail "Image.png" get the size: 120x90pixels - it's OK!

But I want to Cut a little from the sides so the image becomes slightly larger
Something like this:

Code: Select all

convert -define jpeg:size=140x140 7748790.jpg  -thumbnail 14400@ -gravity center -background blue -extent 120x120 -sharpen 0x1.0  Image2.png
(from example: http://www.imagemagick.org/Usage/thumbnails/#cut Fit to given space)

My problem:
I want to remove the area(the background) over and under
the result image (Image2.png) that not belong to the image.
(the size of the result image maybe is 120 x 105pixels)

I want it to function even if the original image is in portrait format
ie. instead cutting a bit up and down on the picture.

//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut the sides - or Zooming

Post by fmw42 »

not sure what you are asking. can you post a link to your example results to see what the issue really is?

You probably need to use -fuzz XX% -trim somewhere in your command line, but it is hard to tell without an example and description of the problem.
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Cut the sides - or Zooming

Post by Albireo »

I try to explain one more time :D
(the problem is not that complicated, but I don't know "best" solution.)

Look at the example: http://www.imagemagick.org/Usage/thumbnails/
"Creating Thumbnails and Framing"
and subtitle: Fit to a Given Space Summary

There are 3 examples ( "Padded fit" "Area fit" and "Cut to fit").
The example I thought looked interesting is: "Area to fit"

but in this example, you can see a bit of background (color: skyblue) above and below the image.

I want to remove the skyblue background
(or for me there is no reason to put there a background from the beginning)

(after that, I want to make rounded corners, maybe shadow, a frame and so on ......
Finally I want to fill up the Image with a white or transparent background,
so the images again becomes 120x120dpi)

Hope this explanation of my requests are easier to understand :D

//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut the sides - or Zooming

Post by fmw42 »

dpi has no bearing on images, you probably mean widthxheight=120x120

try this:

orig:
http://www.fmwconcepts.com/misc_tests/r ... g_orig.jpg

Note one has to compensate the size 120x120 for the shadow so that the end is 120x120

infile="hatching_orig.jpg"
size=120
shadow=4
newsize=`convert xc: -format "%[fx:$size-3*$shadow]" info:`
drawcmd=`convert $infile -define jpeg:size=${newsize}x${newsize} -thumbnail "${newsize}x${newsize}" \
-format "roundrectangle 1,1 %[fx:w-1],%[fx:h-1] 15,15" info:`

convert \( $infile -define jpeg:size=${newsize}x${newsize} -thumbnail "${newsize}x${newsize}" \) \
\( +clone -threshold "100%" -fill white -draw "$drawcmd" \) \
-alpha off -compose Copy_Opacity -composite \
\( +clone -background black -shadow 60x${shadow}+${shadow}+${shadow} \) +swap \
-background none -compose over -flatten -extent ${size}x${size} \
hatching_rounded_shadow.png

Image

final size=120x120
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Cut the sides - or Zooming

Post by Albireo »

Thank you for taking the time with my wishes :D
fmw42 wrote:dpi has no bearing on images, you probably mean widthxheight=120x120
(Maybe I was tired :) - of course it should be 120x120pixels:))
The proposal from "fmw42" above doesn't satisfy my wishes.....

One thing:
The original image (hatching_orig.jpg) is only resized. - from 1600x1200pixels to 120x90pixels.

I want to resize "hatching_orig.jpg" to 130x98 (I don't know the "right size" just now).
then I want to cut out the middle of this image and get a new image with size 120x98pixels.
(I lose a bit of the image on the left and right edge - This is what I mean with the zooming :) ).

I want to do the same as the example: http://www.imagemagick.org/Usage/thumbnails/
"Fit to a Given Space Summary" the example: "Area to fit"
(The only difference is that I don't want any background)

Maybe the resultimage have to be "sharpen" - to look a bit sharper
(the image isn't so big).

Now this is my problem :D
(I can't remove the background from the example above)
and it should work with both portrait and landscape images.

_____________________________________________

- After this, I would like rounded corners (white or maybe transparent).

- Maybe a smal border around the image.

_____________________________________________

then maybe I would like shadow of the image,
but this is a little complicated.

The image is centered in a box that is 120x120pixels
If you add a shadow, the image will still be in the same place.
(I don't want the shadow to center the image in a new location)

I can come back to that later :-)

//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut the sides - or Zooming

Post by fmw42 »

the @ approach may be too uncertain to use. You need to find the number of pixels you desire. Say 120x120=14400

convert hatching_orig.jpg -thumbnail "14400@" hatching_at.jpg

results in a size of 138 x103, but this may vary from image to image, I am not sure.

If you use

convert hatching_orig.jpg -thumbnail "120x120" hatching_at.jpg

you get a resulting size always of 120x90

or

convert hatching_orig.jpg -thumbnail "130x130" hatching_130x130.jpg

you get a resulting size of always 130x98

Your description of your desires seems to have changed or has not been very clear. I spent quite some time trying to get your result to 120x120 end size as you asked. Shadows will offset the image, so if that is not what you want, you will have make the image even smaller and then pad on the left and top to compensate for the shadow. I think you will have to work some more with what I gave you. I am not sure when I can get back to this as I have other commitments.

You misunderstand the examples given by Anthony, when you resize, it does not pad, it just keeps the aspect ratio. So if you use -thumbnail 130x130, it will resize to the larger dimension and then scale the smaller dimension. So the result will be 130x98 with no padding. If you want to "zoom" then you want to use -thumbnail "120x120^" which will make an image 160x120. Then you can crop to any smaller size, but you will lose some of the image.

So I am still not clear what you really want to have done here. I think you need to experiment your self with the various -resize or thumbnail geometries. See http://www.imagemagick.org/script/comma ... p#geometry

If you can provide a more precise description of what you want, perhaps I can modify what I have provided for you.

Here is one more try with a black border so that it remains centered, rather than a shadow.


infile="hatching_orig.jpg"
size=130
size2=`convert $infile -define jpeg:size=${size}x${size} -thumbnail "${size}x${size}" -format "%wx%h" info:`
ww=`echo $size2 | cut -dx -f1`
hh=`echo $size2 | cut -dx -f2`
if [ $ww -ge $hh ]; then
xsize=120
ysize=$hh
lsize=120
else
xsize=$ww
ysize=120
lsize=120
fi
drawcmd=`convert $infile -define jpeg:size=${size}x${size} -thumbnail "${size}x${size}" -gravity center -crop ${xsize}x${ysize}+0+0 +repage \
-format "roundrectangle 1,1 %[fx:w-1],%[fx:h-1] 15,15" info:`
convert \( $infile -define jpeg:size=${newsize}x${newsize} -thumbnail "${size}x${size}" -gravity center -crop ${xsize}x${ysize}+0+0 +repage \) \
\( +clone -threshold "100%" -fill white -draw "$drawcmd" \) \
-alpha off -compose Copy_Opacity -composite \
\( +clone -alpha transparent -background none \
-fill none -stroke black -strokewidth 1 -draw "$drawcmd" \) \
-compose Over -composite -background none -gravity center -extent ${lsize}x${lsize} \
hatching_rounded_border.png

Image

and if rotate input by 90

convert hatching_orig.jpg -rotate -90 hatching_orig_rot90.jpg

infile="hatching_orig_rot90.jpg"
size=130
size2=`convert $infile -define jpeg:size=${size}x${size} -thumbnail "${size}x${size}" -format "%wx%h" info:`
ww=`echo $size2 | cut -dx -f1`
hh=`echo $size2 | cut -dx -f2`
if [ $ww -ge $hh ]; then
xsize=120
ysize=$hh
lsize=120
else
xsize=$ww
ysize=120
lsize=120
fi
drawcmd=`convert $infile -define jpeg:size=${size}x${size} -thumbnail "${size}x${size}" -gravity center -crop ${xsize}x${ysize}+0+0 +repage \
-format "roundrectangle 1,1 %[fx:w-1],%[fx:h-1] 15,15" info:`
convert \( $infile -define jpeg:size=${newsize}x${newsize} -thumbnail "${size}x${size}" -gravity center -crop ${xsize}x${ysize}+0+0 +repage \) \
\( +clone -threshold "100%" -fill white -draw "$drawcmd" \) \
-alpha off -compose Copy_Opacity -composite \
\( +clone -alpha transparent -background none \
-fill none -stroke black -strokewidth 1 -draw "$drawcmd" \) \
-compose Over -composite -background none -gravity center -extent ${lsize}x${lsize} \
hatching_rot90_rounded_border.png

Image
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Cut the sides - or Zooming

Post by Albireo »

Thank you!
I'm glad that You give me Your time..

I'm impressed of your knowledge and
amazed that the problem was so big and difficult.

Back to my problem (again :) )
A picture (portrait or landscape) can get pretty much untapped area.
This image area (120x120) might be used better. (was my thought)

Then I found the three examples: (I don't think that I have missunderstod those)
Ex1 Pad to fit - (normal resize)
Ex3 Cut to Fit - (maybe I lose to much of the image)
But
Ex2 Fit to Area -it felt more interesting. a bit bigger and not so much cut off

Code: Select all

  convert -define jpeg:size=200x200 hatching_orig.jpg  -thumbnail 10000@ -gravity center -background skyblue -extent 100x100  area_fit.gif
Thought that there was only one problem - "the skyblue background".
This problem could have been solved by making a white background - but...

I tested the script on a number of images, felt that the idea was good
but some images have a lot of white in the picture.
and I wanted a bit sharper image. (don't think it's so difficult - nor :) )

Since the images is to be displayed on white background,
I began to think about how to "highlight" the image.

Here begins the real problem (As I see it).
(And perhaps that is why the solution is so complicated)

Apparently, is the problem solved so far that, there is a small border around the picture
(and it's easy to change how much of the edges that will disappear and so on)

______________________________________________
If the images do not appear good enough,
then it can be envisaged to add a shadow behind the image
to "lift" up even more (at the expense of image size)

But perhaps there is another way to highlight "small" images from the background.

If You are intrested in the image, you can click on it
and a larger image is shown - (which are not cut)
______________________________________________

Now is it "just" to translate fmw42's code to Windows ......
The big problem is that I don't understand everything what happen
(I don't know how to do, but want to try.).


I have some problem
- What's happen in the "if-sats"? "if [ $ww -ge $hh ]; then........."
- What's the value of "Newsize"
....................?`?



//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut the sides - or Zooming

Post by fmw42 »

Thought that there was only one problem - "the skyblue background".
This problem could have been solved by making a white background - but...
But you don't need to add the background and then try to get rid of it. Don't add the -background ... -extent! See my examples! If you don't add that, then you don't have to worry about getting rid of it. The image is sized to either the larger or the smaller dimension (depending upon if you use ^ or not) and the other dimension is scaled proportionally with NO PADDING.

I am not a windows programmer so cannot help with the If, then, else. See the excellent tutorial of Wolgang Hugemann on Anthony's site at http://www.imagemagick.org/Usage/windows/. It discusses the difference between unix IM and windows IM and how to convert things from unix to windows or gives the equivalent. Things to not are quotes and escapes, that need to be different on Windows. Also parenthesis are not escaped with \ in Windows and the continuation at the end of the line is different.

Perhaps you can get a windows IM user to help you convert my code.

see also viewtopic.php?f=1&t=15416 where user el_supremo translated my commands into Windows for you earlier

infile="hatching_orig.jpg" <-- define input image
size=130 <-- define initial size so that result will be 130x98 after thumbnail as you wanted
size2=`convert $infile -define jpeg:size=${size}x${size} -thumbnail "${size}x${size}" -format "%wx%h" info:` <-- this gets 130x98 after processing
ww=`echo $size2 | cut -dx -f1` <-- extracts 130
hh=`echo $size2 | cut -dx -f2` <-- extracts 98
if [ $ww -ge $hh ]; then <-- test for landscape or portrait and sets xsize, ysize and lsize (largest size)
xsize=120
ysize=$hh
lsize=120
else
xsize=$ww
ysize=120
lsize=120
fi
drawcmd=`convert $infile -define jpeg:size=${size}x${size} -thumbnail "${size}x${size}" -gravity center -crop ${xsize}x${ysize}+0+0 +repage \
-format "roundrectangle 1,1 %[fx:w-1],%[fx:h-1] 15,15" info:` <-- sets a string to use later that gets your size 130x98 and crops to 120x98 and then defines the roundrectangle arguments
convert \( $infile -define jpeg:size=${newsize}x${newsize} -thumbnail "${size}x${size}" -gravity center -crop ${xsize}x${ysize}+0+0 +repage \) \ <-- crops to 120x98
\( +clone -threshold "100%" -fill white -draw "$drawcmd" \) \ <-- copies the last result makes a black background and white rounded rectangle as mask
-alpha off -compose Copy_Opacity -composite \ <-- puts the black and white mask into the alpha channel of the reduced and cropped image
\( +clone -alpha transparent -background none \
-fill none -stroke black -strokewidth 1 -draw "$drawcmd" \) \ <-- copies the previous image and makes it transparent and puts a black border around it
-compose Over -composite -background none -gravity center -extent ${lsize}x${lsize} \ <-- overlays the black border on the cropped image and extends transparency to fill out to 120x120
hatching_rounded_border.png
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Cut the sides - or Zooming

Post by Albireo »

fmw42 wrote:
ww=`echo $size2 | cut -dx -f1`
We begin here :D
When I translate the row I get the message: "cut is not an internal command, external command program or batch file"

I tried to understand the-"cut command"
What happen? (i think that "size2" is OK! - it produce the right value)
fmw42 wrote:
if [ %ww -ge %hh ]; then
What happen?
%ww Is that a variable (containing 130)?
and %hh Is that a variable (containing 98)?
but what is "-ge"? is that the same as ">" greater than....

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

Re: Cut the sides - or Zooming

Post by snibgo »

Translating from Unix to Windows isn't trivial.

"Cut" is a Unix command. For Windows, you can do something like:

Code: Select all

convert %infile% -format "set ww=%%w\nset hh=%%h" info:%TEMP%\getsize.bat

call %TEMP%\getsize.bat
snibgo's IM pages: im.snibgo.com
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Cut the sides - or Zooming

Post by Albireo »

[quote="snibgo"]Translating from Unix to Windows isn't trivial.


What happen?
%ww Is that a variable (containing 130 or 1600)?
and %hh Is that a variable (containing 98 or 1200)?
but what is "-ge"? is that the same as ">" greater than....

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

Re: Cut the sides - or Zooming

Post by snibgo »

The "cut" command (see any Unix documentation) extracts data from a file, in this case data that is delimited by "x", taking the first or second fields.

I'm giving you an alternative to the "cut" command, by using convert to write a Windows batch file that sets two environment variables, then executing that batch file. Of course, you actually need to use the fuller convert command, as given by fmw.

"-ge" is "greater than or equals". See Windows "help if" for the equivalent.
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: Cut the sides - or Zooming

Post by fmw42 »

if [ %ww -ge %hh ]; then
It is not %, but $

You will have to read the Windows page and study it as I know little about windows. I marked the concepts in blue.

$ww sets the value to 130 as I said in blue.

-ge is the unix integer comparison that is equivalent to >= in other languages.

Read this http://www.imagemagick.org/Usage/windows/. Translation is not trivial, but this paper has most if not all of what you need.

Sorry, but I can be of no further help as I know little of Windows/DOS commands.


Your other solution is to install something like Cygwin (unix for Windows) and install the IM Cygwin version. Then you can run all these commands as I wrote them.
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Cut the sides - or Zooming

Post by Albireo »

fmw42 wrote:It is not %, but $
Thank you! (I have begint o convert)

One thing I wonder about: Where get the variable "newsize" its value?

Is it still the code below that assigns the variable "NewSize" (taken from one of your first example)

Code: Select all

newsize=`convert xc: -format "%[fx:$size-3*$shadow]" info:
_________________________________________________________________

Is it right encoded if I get the result: drawcmd= roundrectangle 1.1 119.97 15.15 from the code:

Code: Select all

drawcmd=`convert $infile -define jpeg:size=${size}x${size} -thumbnail "${size}x${size}" -gravity center -crop ${xsize}x${ysize}+0+0 +repage \
-format "roundrectangle 1,1 %[fx:w-1],%[fx:h-1] 15,15" info:`
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Cut the sides - or Zooming

Post by Albireo »

Yes! Yes! Yes! :D :D
Now it works! the result on the result image has the style I liked. :)
(I hope it vill work with all my images :) )

But:
Where do I best add the command "-sharp 0x1.0" - to get the images a bit sharper?

(The code will come when I have "improved" it a bit. :D )

//Jan
Post Reply