Simple 1px stroke border around 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
PA_Kid

Simple 1px stroke border around image

Post by PA_Kid » 2010-09-28T07:49:52-07:00

Hello all,

I'm trying to do something simple, or what would seem to be simple... but for the life of me I can't figure out the proper command line to get it done. What I want to do is add a 1px black border around an image, but I want to add this "border" to the inside of the image so as not to change the overall dimensions of the original image.

For a standard border, I know that I can use "mogrify -bordercolor black -border 1 *" to process all images in a specific directory, or I could also use the "convert -border" command to do the same. But both of these commands add 1px to each side of the image, thereby making a 100x100 image now 102x102 I have figured out that I will need to use the stroke command rather then border to accomplish what I'm looking to do.

I've tried the following:

Code: Select all

convert -stroke black -strokewidth 1 test.jpg testnew.jpg
To process one image - test.jpg - and output the modified image as testnew.jpg The testnew.jpg image does get created, but no stroke is applied. I'm not sure if stroke is not being applied because I'm not specifying coordinates in the command line arguments, or what the case may be.

Ideally I would like to find a stroke command that simply adds the 1px black border to all images in a directory, regardless of size dimensions of the input image. My test environment is Imagemagick for Windows and batch files, but I will be using the same on a Unix machine once I figure out the correct command.

Any help on this is appreciated.

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

Re: Simple 1px stroke border around image

Post by fmw42 » 2010-09-28T10:16:43-07:00

convert -stroke black -strokewidth 1 test.jpg testnew.jpg
These are all settings and no "operator".

You could use -draw and a rectangle the size of the image.


convert image -stroke black -strokewidth 1 -fill none -draw "rectangle 0,0 lastx,lasty" result

where lastx = width-1 and lasty = height-1

see
http://www.imagemagick.org/Usage/draw/#primitives

or

shave one pixel all around and then add a one pixel border

convert image -shave 1x1 -bordercolor black -border 1 result

see
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/crop/#border

PA_Kid

Re: Simple 1px stroke border around image

Post by PA_Kid » 2010-09-28T18:18:24-07:00

fmw42 wrote:shave one pixel all around and then add a one pixel border

convert image -shave 1x1 -bordercolor black -border 1 result
Many thanks for the reply. I've gotten this working so far with one image, using the following:

convert input.jpg -shave 1x1 -bordercolor black -border 1 output.jpg

But I'm a bit unsure how to process an entire directory of images. What I would like to do is process and overwrite all images in a directory. With something like a mogrify resize command, I've used the following in the past to process all jpg images in the current working directory:

mogrify -resize 50%% +profile "*" *.jpg

This mogrify command would overwrite all existing images with the newly sized images in the process. However, I've played around using similar syntax with "convert" to process multiple images... but I'm having little success. Any suggestions?

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

Re: Simple 1px stroke border around image

Post by fmw42 » 2010-09-28T18:39:50-07:00

you can use mogrify if it supports -shave and -border

mogrify -shave 1x1 -bordercolor black -border 1 -format jpg *.jpg

or

mogrify -shave 1x1 -bordercolor black -border 1 -format jpg *

The latter will try to convert all files to jpg.

But in my experience, mogrify does not overwrite the images as it is supposed to (at least on my Mac, but perhaps because I use a second directory as below).

However, I prefer to play it safe and use a second empty directory to put all the new files into and then delete the old directory

so if I have files in directory test1 and an empty directory test2, I would usually do

cd test1
mogrify -shave 1x1 -bordercolor black -border 1 -path /fullpathto/test2 -format jpg *.jpg

or

mogrify -path /fullpathto/test2 -format jpg -shave 1x1 -bordercolor black -border 1 *.jpg

(not sure if it makes a difference with the order of -path and -format relative to the other options)

see
http://www.imagemagick.org/Usage/basics/#mogrify

PA_Kid

Re: Simple 1px stroke border around image

Post by PA_Kid » 2010-09-28T19:08:15-07:00

fmw42 wrote:you can use mogrify if it supports -shave and -border

mogrify -shave 1x1 -bordercolor black -border 1 -format jpg *.jpg
This worked perfectly. Right now I'm just using on some test images, but the images will always be *.jpg -- so the second command where mogrify converts images to jpg probably isn't best for my application.

One concern with overwriting using mogrify -- will there be any additional compression done to the images with this command? Basically I'm just concerned with attempting to preserve the original jpg quality of the source images.

Also, one more addition to my question.... how can this command be modified so that it processes all images in current directory, as well as any subdirectories?

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

Re: Simple 1px stroke border around image

Post by fmw42 » 2010-09-28T19:25:36-07:00

One concern with overwriting using mogrify -- will there be any additional compression done to the images with this command? Basically I'm just concerned with attempting to preserve the original jpg quality of the source images.

Also, one more addition to my question.... how can this command be modified so that it processes all images in current directory, as well as any subdirectories?
Any reprocessing of a jpg will cause loss of quality and you cannot count on having the same compression level.

Mogrify does not allow directory traversing. You will have to write a script to loop over each directory and process using convert or mogrify for each subdirectory.

Anthony has some complex notes about this on the mogrify page at http://www.imagemagick.org/Usage/basics/#mogrify_not

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

Re: Simple 1px stroke border around image

Post by anthony » 2010-09-28T19:33:49-07:00

PA_Kid wrote:
fmw42 wrote:the images will always be *.jpg
WARNING: reading and re-saving JPEG images is a lossy operation.. That is JPEG image format will degrade every time you read and re-save the image.

It is recommended that you keep a copy of the original image, and always work from that image, or generate non-JPEG intermediate images to work with, and only save back to JPG for the absolute final image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

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

Re: Simple 1px stroke border around image

Post by fmw42 » 2010-09-28T19:35:10-07:00

anthony wrote:WARNING: reading and re-saving JPEG images is a lossy operation.. That is JPEG image format will degrade every time you read and re-save the image.

It is recommended that you keep a copy of the original image, and always work from that image, or generate non-JPEG intermediate images to work with, and only save back to JPG for the absolute final image.
I basically said that above, but it does not hurt the re-emphasize these important issues.

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

Re: Simple 1px stroke border around image

Post by anthony » 2010-09-28T19:42:38-07:00

Sorry I was posting at the same time you were fred.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

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

Re: Simple 1px stroke border around image

Post by fmw42 » 2010-09-28T19:44:11-07:00

anthony wrote:Sorry I was posting at the same time you were fred.

Great minds think alike! :wink:

Post Reply