japhy has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking at GD, Imager, and Image::Magick, and I'm not sure I'm finding what I need.

I'd like a module that allows me to skew an image. Basically, I want to "perspective-ize" an image. My goal is to take a bunch of rectangular images and skew some and resize others to make a pseudo-3D view.

_____________________________________________________
Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re: Module to skew an image
by BrowserUk (Patriarch) on Feb 19, 2005 at 03:22 UTC

    Try this. Supply an image name on the command line and it will produce an image (junk.jpg) the same size as the original that looks roughly like this:

    _____ |\___/| | | | | | | | | | |_| | |/___\|

    where each of the 3 walls, the floor and ceilng are the original image "perspectivated" to fit.

    You'll probably have to prefix the filename in the system command with your local favorite image viewer if your on *nix.

    #! perl -slw use strict; use GD; my $img = GD::Image->new( $ARGV[ 0 ] ); my( $ox, $oy ) = $img->getBounds(); my $im2 = GD::Image->new( $ox, $oy, 1 ); $im2->copyResampled( $img, $ox/4, $oy/4, ## dstXY 0, 0, ## srcXY $ox/2, $oy/2, ## dstWH $ox, $oy ## srcWH ); my( $ys, $ye, $dy ) = ( 0, $oy, $oy/$ox ); for my $x ( 0 .. $ox/4 ) { $im2->copyResampled( $img, $x, $ys+=$dy, $x*4, 0, 1, $ye-=($dy*2), 4, $oy ); } ( $ys, $ye, $dy ) = ( $oy/4, $oy/2, $oy/$ox ); for my $x ( 0 .. $ox/4 ) { $im2->copyResampled( $img, $ox*3/4+$x, $ys-=$dy, $x*4, 0, 1, $ye+=$dy*2, 4, $oy ); } my( $xs, $xe, $dx ) = ( 0, $ox, $ox/$oy ); for my $y ( 0 .. $oy/4 ) { $im2->copyResampled( $img, $xs+=$dx, $y, 0, $y*4, $xe-=$dx*2, 1, $ox, 4, ); } ( $xs, $xe, $dx ) = ( $ox/4, $ox/2, $ox/$oy ); for my $y ( 0 .. $oy/4 ) { $im2->copyResampled( $img, $xs-=$dx, $oy*3/4+$y, 0, $y*4, $xe+=$dx*2, 1, $ox, 4, ); } open IMG, '>:raw', 'junk.jpg' or die $!; print IMG $im2->jpeg; close IMG; system 'junk.jpg';

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Module to skew an image
by toma (Vicar) on Feb 19, 2005 at 05:47 UTC
    The graphics term for 'skew' is 'affine transformation'.

    See Imager::Matrix2d.

    It should work perfectly the first time! - toma
      I believe that the transformation he wants is not an affine one. The pseudo-3D effect is not the same as skew, and as such I'm not surprised it is not present out of the box. There may have to be some "manual" pixel-mangling, and I think one of the earlier posts explains the formulas behind it. Gimp::Fu may also be useful, as stated earlier.
Re: Module to skew an image
by moot (Chaplain) on Feb 19, 2005 at 02:04 UTC
    Have you tried Gimp/ Gimp::Fu ? The full power of The GIMP in one easy scriptable package! :)