ImageMagick C++ Template

This entry is intended to give anyone interested in using the C++ ImageMagick API a kickstart.

Base C++ code

#include <iostream>
#include <Magick++.h>

using namespace Magick;
using namespace std;

 

int main(void) {
cout << "hello ImageMagick.";
return 0;
}

Base Makefile

CC=g++
CFLAGS=-c -Wall -m32 -Wall -ansi -pedantic -O3 -Wno-long-long -I /usr/include/ImageMagick
LDFLAGS=-m32 -pthread -L /usr/lib/ImageMagick-6.6.2 -lMagick++ -ljpeg -lpng -ltiff -lbz2 -lxml2 -lz -lm -lgomp -lMagickWand -lMagickCore

SOURCES=image.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o:
$(CC) $(CFLAGS) $< -o $@

 

clean:
rm $(EXECUTABLE) *.o

Merge Images using Imagemagick Composite Utility

The following command replaces all images contained in the current folder as follows:

Each image will be copied into the center on top of the empty.gif. Thus, if you like to have a couple of images to fit 400×400 pixels then just create an empty.gif with that dimensions and run the command.

bash# PICS=ls -1; for pic in $PICS; do cp $pic temp.jpg; composite -gravity center temp.jpg ../empty.gif $pic; done; rm temp.jpg

Resize multiple images using Imagemagick

Ever tried to resize a whole folder of images in a single line? Make sure you have  the imagemagick convert utility installed. Paste to following line into your bash

bash# ls -1 *.jpg > images; while read line; do SRC=$line; cp $line temp.jpg; convert -resize 400x400 temp.jpg $SRC; done < images; rm images temp.jpg

The command will resize alle images to 400 pixels height or width depending on the orientation of the image. Thus, the propotions of the image will not change.