How to convert image like following effects

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
hiyoucai
Posts: 3
Joined: 2012-07-17T20:19:32-07:00
Authentication code: 15

How to convert image like following effects

Post by hiyoucai »

I have 100X100 image(a.jpg) , i want to scale image to 10x10(b.jpg)

script like this:

Code: Select all

convert -sample 10x10 a.jpg b.jpg
but i want to the final image size is 30x10

Another way of saying is

10x10 b.jpg is filled into 30x10 image,other part is fill with white color.

Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to convert image like following effects

Post by anthony »

First read the image in BEFORE operating on it.

Code: Select all

   convert a.jpg -sample 10x10 b.jpg
Second that should have created an image that fits inside a 10x10 pixel box while perserving the aspect ratio.
As your image is square that is the size you would get. If you image was not square say twice as wide as high (200x100) then the resulting image would be 10x5 so as to fit in the 10x10 box.

To force an image to be a specific size without preserving aspect ratio, add a '!' flag to the size argument.

Code: Select all

   convert a.jpg -sample 30x10\! b.jpg
I recommend backslash escaping the '!' character as some shells interpret it as a 'history substitution' rather than just pass that character to the command.

Finally are you really wanting to 'sample' the image? That is just select 30x10 actual pixels from the source image?
More commonly people want to use -resize or -scale to merge colors together when shrinking images.

Code: Select all

   convert a.jpg -resize 30x10\! b.jpg
For more information on ALL of these aspects of resizing images (and much much more) see
IM Examples, Resize Images
http://www.imagemagick.org/Usage/resize/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert image like following effects

Post by fmw42 »

Please clarify what size the unpadded image should be after resizing and then what size the white padded image should be! Is your image always square? Is your real goal to have a resized image that is padded to square if needed when the image is not square?

convert image.jpg -resize 30 -gravity center -fill white -extent 30x30 result.jpg

see
http://www.imagemagick.org/Usage/thumbnails/#pad
hiyoucai
Posts: 3
Joined: 2012-07-17T20:19:32-07:00
Authentication code: 15

Re: How to convert image like following effects

Post by hiyoucai »

thank you all.

the second replier's code is work, i want to answer other question.

What is different between the following two statements

A:
convert a.jpg -strip -sharpen 0.3 -quality 90% -resize 480x296 -gravity center -fill white -extent 480x360 result_a.jpg

B:
convert a.jpg -resize 480x296 -gravity center -fill white -extent 480x360 -strip -sharpen 0.3 -quality 90% result_b.jpg

[hiyoucai@localhost home]# ll -k
-rw-r--r--. 1 root root 658 7月 18 21:43 a.jpg
-rw-r--r--. 1 root root 88 7月 18 22:15 result_a.jpg
-rw-r--r--. 1 root root 104 7月 18 22:15 result_b.jpg

result_a.jpg file size is 88 less than result_b.jpg file size 104
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to convert image like following effects

Post by anthony »

Sharpen is done after all other actions in the second.

In the first it is done before.

However again read your image in BEFORE applying an operator on it.
It works now, but IMv7 will produce a 'no image for operation' type error.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
hiyoucai
Posts: 3
Joined: 2012-07-17T20:19:32-07:00
Authentication code: 15

Re: How to convert image like following effects

Post by hiyoucai »

anthony wrote:Sharpen is done after all other actions in the second.

In the first it is done before.

However again read your image in BEFORE applying an operator on it.
It works now, but IMv7 will produce a 'no image for operation' type error.
thank you...

--------

I am a java programmer. if i use jmagick-6.4.0.jar to generate an image with the same parameters,the image file size is small and quality is good.

but i use convert script to generate an image that quality is less than java. i don't know why.call you tell me.

but java program sometimes will overflow memory.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert image like following effects

Post by fmw42 »

convert a.jpg -resize 480x296 -gravity center -fill white -extent 480x360 -strip -sharpen 0.3 -quality 90% result_b.jpg
sharpen has arguments radiusxsigma

The best way to use those arguments are to provide 0xsigma. That tells IM to allow the Gaussian function to roll off to near zero value and make the radius half the window size needed to achieve that. If you just provide a radius, you are not truely using a Gaussian function, but one that is truncated too early by your radius and thus does not roll off to near zero. Nominally, using 0xsigma will generate a radius that is about 3xsigma automatically.

If you want a uniform blur rather than a Gaussian shaped blur, then use radiusx65000. 65000 should be atleast 100 x radius, but I doubt you will have a radius that is greater than 650.
Post Reply