7

I need to get a specific crop of an image and put it over another image at a certain position and resized.

I can crop the first image and save it to a file in one command and then I can composite the 2 images in another command. However, I would like to do it in a single command - is this possible with graphicsmagick and how?

Here are the 2 commands I am using atm:

gm convert -crop 1457x973+254+413 amber.jpg tmp.jpg
gm composite -geometry 6000x4000+600+600 tmp.jpg lux_bg.png out.jpg

The reason for wanting this is to avoid writing to disk then reading again when all this could be done in memory. With ImageMagick, for example, the same 2 commands would be written in a single command like this:

convert lux_bg.png \( amber.jpg -crop 1457x973+254+413 \) -geometry 6000x4000+600+600 -composite out.jpg

I am doing this with ImageMagick for now but would love to do it with GraphicsMagick.

Dan Caragea
  • 1,784
  • 1
  • 19
  • 24

2 Answers2

4

If your reason is simply to avoid creating a temporary file, you can still do it with two commands by constructing 'pipelines' (a great concept invented by, afaik, Douglas McIlroy around 1964):

gm convert -crop 1457x973+254+413 amber.jpg - | gm composite -geometry 6000x4000+600+600 - lux_bg.png out.jpg

hint: note the two - dashes in the two commands, and the | pipe

since the - can be used to mean the standard output and input in the two commands respectively.

This means that no file is created, all should happen in the memory.

You can find this in the help (gm -help convert | grep -i -e out -B 1):

Specify 'file' as '-' for standard input or output.

The use of - is common in unix-likes and must have been inspired by, or by something related to, the POSIX standard's Utility Syntax Guidelines.

n611x007
  • 8,952
  • 8
  • 59
  • 102
0

Have you tried && operator? Your command should become:

gm convert -crop 1457x973+254+413 amber.jpg tmp.jpg && gm composite -geometry 6000x4000+600+600 tmp.jpg lux_bg.png out.jpg
Andrea
  • 11,801
  • 17
  • 65
  • 72
  • 1
    They're still technically 2 commands. The reason for wanting a single gm command is to avoid creating a temporary file (tmp.jpg in my example) which involves unnecessary read/write from/to hard disk. – Dan Caragea Nov 13 '13 at 18:42
  • i'm using gm in node, how would I translate this command to it? currently this errors out if i uncomment crop line: `gm(img) .resize(1280) .composite(__dirname + '/../public/images/header.jpg') .geometry('+0+0') // .crop(1280,720,0,0)` – jasan Aug 31 '17 at 18:06