Extend side of 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
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Extend side of image

Post by chaoscarnage »

I want to do this, but extend a side of an image.

Code: Select all

convert buffer.png -extent YxX 
I do not know if I can use crop and just give it negative numbers.

Is there a command for this?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Extend side of image

Post by GeeMack »

chaoscarnage wrote:I do not know if I can use crop and just give it negative numbers.

Is there a command for this?
You can use "-splice" to add a row of a particular width to just one side of an image. Something like...

Code: Select all

convert image.jpg -background white -gravity northwest -splice 20x0 result.jpg
... would add a 20 pixel wide white row to the left side of the image. By setting "-background" and "-gravity" you can add any amount of any color to any one side (or any two adjacent sides) of the input image. Read more about it at this LINK.
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Extend side of image

Post by chaoscarnage »

GeeMack wrote: You can use "-splice" to add a row of a particular width to just one side of an image. Something like...

Code: Select all

convert image.jpg -background white -gravity northwest -splice 20x0 result.jpg
... would add a 20 pixel wide white row to the left side of the image. By setting "-background" and "-gravity" you can add any amount of any color to any one side (or any two adjacent sides) of the input image. Read more about it at this LINK.
Here is a more detailed example of what I am trying to do. I figure if I learn how to pad one side, I can pad all sides the amount I desire.
http://stackoverflow.com/questions/4053 ... te-of-crop
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Extend side of image

Post by GeeMack »

chaoscarnage wrote:Here is a more detailed example of what I am trying to do. I figure if I learn how to pad one side, I can pad all sides the amount I desire...
There are several ways to go about this sort of thing. You can create an empty canvas the size you want your finished image, set the geometry of the input image to the location you want it on that background, then composite the input image onto the canvas. A command to do that might look something like this...

Code: Select all

convert input.jpg -geometry +50+30 xc:white[400x400] +swap -composite result.jpg
You can "-splice" some rows of pixels to the left and top of your image (for example), then use "-extent" to make it the final desired size. A command to do that would be something like this...

Code: Select all

convert input.jpg -background white -gravity northwest -splice 50x30 -extent 400x400 result.jpg
There are many other ways to do it, too. Those two commands above should generate identical output images (as long as your input image is smaller than the background canvas and your geometry doesn't push it outside the bounds of the canvas). One method may be preferable to another depending on what other sorts of manipulation you want to do in the same command.
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Extend side of image

Post by chaoscarnage »

Geemack, Thank you. I already have a splice example working, and I will try the other to see if its faster.

What does +swap do?

Also, if I want the xc to be none instead of white, will that work?
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Extend side of image

Post by chaoscarnage »

Having tested this, the faster method is:

Code: Select all

convert input.jpg -background white -gravity northwest -splice 50x30 -extent 400x400 result.jpg
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Extend side of image

Post by GeeMack »

chaoscarnage wrote:What does +swap do?
ImageMagick can work with stacks of images, not just single images. In that command I brought the input image in first, then I built the 400x400 white canvas second. That's their order in the stack. The "+swap" trades places of the last two images in the stack (or the only two images in this case) so the input image gets composited on top of the background canvas. Without that "+swap" the background would have been layered over the input image (... although there are many other ways to rearrange the order of the stack and composites, too! :))
chaoscarnage wrote:Also, if I want the xc to be none instead of white, will that work?
Yes, you can make a canvas of any color, even transparent (none), but of course it will only be transparent for output formats that support it like PNG.
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Extend side of image

Post by chaoscarnage »

GeeMack wrote:
chaoscarnage wrote:What does +swap do?
ImageMagick can work with stacks of images, not just single images. In that command I brought the input image in first, then I built the 400x400 white canvas second. That's their order in the stack. The "+swap" trades places of the last two images in the stack (or the only two images in this case) so the input image gets composited on top of the background canvas. Without that "+swap" the background would have been layered over the input image (... although there are many other ways to rearrange the order of the stack and composites, too! :))
chaoscarnage wrote:Also, if I want the xc to be none instead of white, will that work?
Yes, you can make a canvas of any color, even transparent (none), but of course it will only be transparent for output formats that support it like PNG.
You know more about this then I do. I received an answer on stackoverflow. However... What I am doing is a lot more complex then what I have lead on. I am wondering if I can convert what I have into their answer and see if its faster. (I am doing 250 images at once and right now it takes 17.9 seconds to complete).


This is what I have based on what you told me and it is 17.9 seconds instead of what I had originally. With your help before I was able to get it to 17.9 from 18.3.

Code: Select all

exec('convert ' . $img . '.png /gradient/gradient.png -clut -background none -splice ' . $test[0] . 'x' . $test[1] . ' -extent 400x400 ' . $img . $r);
What was suggested is this.

Code: Select all

convert -size 400x400 xc:lime \( 52x107.png -repage +158+146 \) -flatten out.png
Is there anyway for me to do that in the structure in what I have? I have learned that without resizing it takes 14.4 seconds. So that means its taking 3.5 seconds to resize the images. I am wondering if I can speed that process up.
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Extend side of image

Post by chaoscarnage »

I do have one other, much simpler question. In the command line. How do I set an image to a name. for example...

How do I do this in command line
rose: rose-over.png
Then use rose in my convert.
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Extend side of image

Post by chaoscarnage »

If anyone has any direction I would appreciate it.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Extend side of image

Post by Bonzo »

What I am doing is a lot more complex then what I have lead on.
It is always better to say what you want in the first place rather than "start with something simple" and keep increasing the complexity.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extend side of image

Post by fmw42 »

chaoscarnage wrote:
How do I do this in command line
rose: rose-over.png
Then use rose in my convert.

It is not clear what you want.

To use the rose: image, just use it. You do not have to rename it first.

Code: Select all

convert rose: ...some commands... rose_processed.png
If you want to make a copy of the rose: image, you can do it in IM as

Code: Select all

convert rose: rose.png
But there is no reason to copy it, since IM does not destroy the input image.
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Extend side of image

Post by chaoscarnage »

fmw42 wrote:
chaoscarnage wrote:
How do I do this in command line
rose: rose-over.png
Then use rose in my convert.

It is not clear what you want.

To use the rose: image, just use it. You do not have to rename it first.

Code: Select all

convert rose: ...some commands... rose_processed.png
If you want to make a copy of the rose: image, you can do it in IM as

Code: Select all

convert rose: rose.png
But there is no reason to copy it, since IM does not destroy the input image.
What I was wondering is if I used the same image 10 times, would it be better to first copy it so it does not have to load that initially image 10 times.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extend side of image

Post by fmw42 »

Post Reply