Difference between revisions of "ImageMagick"

From HalfgeekKB
Jump to navigation Jump to search
Line 4: Line 4:
  
 
===Composite===
 
===Composite===
 +
 +
A <code>Composite</code> operation combines two images. Useful for such things as adding backgrounds to partially transparent images.
 +
 +
====General====
 +
 +
In the docs, the "src" (or "overlay") is combined with the "dst" (or "background") using a compose method.
 +
 +
<syntaxhighlight lang=bash>
 +
composite -compose METHOD SRC.png DST.png COMPOSED.png
 +
</syntaxhighlight>
 +
 +
When using Perl, the operation is destructive of the dst image. Use the <code>Clone</code> method to operate on a copy while preserving the original.
 +
 +
<syntaxhighlight lang=perl>
 +
# Result is $dst
 +
$dst->Composite(compose => 'METHOD', image => $src);
 +
 +
# Result is $composed, $dst is unchanged
 +
my $composed = $dst->Clone;
 +
$composed->Composite(...);
 +
</syntaxhighlight>
 +
 +
====over: Copy src on top of dst====
 +
 +
The default operation: Src (the foreground) is copied over dst (the background).
 +
 +
<syntaxhighlight lang=bash>
 +
 +
# The method may be omitted here
 +
composite -compose fg.png bg.png fg_on_bg.png
 +
# More verbosely
 +
composite -compose over fg.png bg.png fg_on_bg.png
 +
</syntaxhighlight>
 +
 +
<syntaxhighlight lang=perl>
 +
# Result is $bg
 +
$bg->Composite(image => $fg);
 +
# More verbosely
 +
$bg->Composite(method => 'over', image => $fg);
 +
</syntaxhighlight>
 +
 +
====dst_over: Copy src behind dst====
 +
 +
The same as <code>over</code> but with roles reversed: src (now the background) is copied under dst (now the foreground). In Perl, this is a useful way to mutate the foreground instead of the background.
  
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
Line 10: Line 54:
  
 
<syntaxhighlight lang=perl>
 
<syntaxhighlight lang=perl>
my $fg_on_bg = $fg->Clone; # or just $fg, if destructive
+
# Result is $fg
$fg_on_bg->Composite(compose => 'dst_over', image => $bg);
+
$fg->Composite(compose => 'dst_over', image => $bg);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 29: Line 73:
 
my $filled = new Image::Magick size => "${width}x${height}";
 
my $filled = new Image::Magick size => "${width}x${height}";
 
$filled->Read("xc:$color");
 
$filled->Read("xc:$color");
 +
</syntaxhighlight>
 +
 +
===New image the same size as an existing image===
 +
 +
<syntaxhighlight lang=perl>
 +
my $new_image = new Image::Magick size => join('x', $existing_image->Get('width','height'));
 +
</syntaxhighlight>
 +
 +
===Fill the background of an existing (partly transparent) image with a color===
 +
 +
<syntaxhighlight lang=perl>
 +
my $bg_color = 'white';
 +
my $bg = new Image::Magick size => join('x', $fg->Get('width','height'));
 +
$bg->Read("xc:$bg_color");
 +
 +
# Either put the result in the new image (result $bg)
 +
$bg->Composite(image => $fg);
 +
 +
# Or mutate the existing foreground image (result $fg)
 +
$fg->Composite(compose => 'dst_over', image => $bg);
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:00, 2 January 2015

ImageMagick is a common image processing program (convert) and library (e.g. PerlMagick, aka Image::Magick).

Snippets

Composite

A Composite operation combines two images. Useful for such things as adding backgrounds to partially transparent images.

General

In the docs, the "src" (or "overlay") is combined with the "dst" (or "background") using a compose method.

composite -compose METHOD SRC.png DST.png COMPOSED.png

When using Perl, the operation is destructive of the dst image. Use the Clone method to operate on a copy while preserving the original.

# Result is $dst
$dst->Composite(compose => 'METHOD', image => $src);

# Result is $composed, $dst is unchanged
my $composed = $dst->Clone;
$composed->Composite(...);

over: Copy src on top of dst

The default operation: Src (the foreground) is copied over dst (the background).

# The method may be omitted here
composite -compose fg.png bg.png fg_on_bg.png
# More verbosely
composite -compose over fg.png bg.png fg_on_bg.png
# Result is $bg
$bg->Composite(image => $fg);
# More verbosely
$bg->Composite(method => 'over', image => $fg);

dst_over: Copy src behind dst

The same as over but with roles reversed: src (now the background) is copied under dst (now the foreground). In Perl, this is a useful way to mutate the foreground instead of the background.

composite -compose dst_over bg.png fg.png fg_on_bg.png
# Result is $fg
$fg->Composite(compose => 'dst_over', image => $bg);

New image filled with color

export COLOR=white
export WIDTH=800
export HEIGHT=600
convert -size ${WIDTH}x${HEIGHT} xc:${COLOR} filled.png
my $color = 'white';
my $width = 800;
my $height = 600;
my $filled = new Image::Magick size => "${width}x${height}";
$filled->Read("xc:$color");

New image the same size as an existing image

my $new_image = new Image::Magick size => join('x', $existing_image->Get('width','height'));

Fill the background of an existing (partly transparent) image with a color

my $bg_color = 'white';
my $bg = new Image::Magick size => join('x', $fg->Get('width','height'));
$bg->Read("xc:$bg_color");

# Either put the result in the new image (result $bg)
$bg->Composite(image => $fg);

# Or mutate the existing foreground image (result $fg)
$fg->Composite(compose => 'dst_over', image => $bg);