(none) imager-devel
/ help / lists / applications / search /
 

Re: Compositing images

From:
07695@xyz.molar.is
Date: Thu 27 Mar 2003 - 15:49:55 GMT

  • Next message: Arnar Mar Hrafnkelsson: "Re: Compositing images"

    Hi Thad,

    I've been enjoying the rub-through feature. A .tga file is the graphic format I've been using. Here is some sample code.

    # Save the uploaded file
    open (FILE, ">/home/www/upload_orig.jpg");
    flock(FILE, 2);
    print FILE $CGI{'file_attached'}->{'Contents'};
    flock(FILE, 8);
    close (FILE);

    # Get the file into Imager
      $img=Imager->new();
      $img->open(file=>'/home/www/upload_orig.jpg',type=>'jpeg')
        || print "failed: ",$img->{ERRSTR},"\n";

    # Resize the file, 2x
      $width=$img->getwidth();
      if ($width > 420) {
              $scaled=$img->scale(xpixels=>420);
            $scaled->filter(type=>'conv', coef=>[-0.16, 1, -0.16]);
              $scaled->write(file=>'/home/www/upload_large.jpg',type=>'jpeg')
                || print "failed: ",$scaled->{ERRSTR},"\n";
              }
      else {
              $img->write(file=>'/home/www/upload_large.jpg',type=>'jpeg')
                || print "failed: ",$img->{ERRSTR},"\n";
              }
      if ($width > 125) {
              $scaled=$img->scale(xpixels=>125);
            $scaled->filter(type=>'conv', coef=>[-0.2, 1, -0.2]);
    # $scaled->filter(type=>'unsharpmask');
              $scaled->write(file=>'/home/www/upload_small.jpg',type=>'jpeg')
                || print "failed: ",$scaled->{ERRSTR},"\n";
              }
      else {
              $img->write(file=>'/home/www/upload_small.jpg',type=>'jpeg')
                || print "failed: ",$img->{ERRSTR},"\n";
              }

    # Place the rub through on the image
    my $small = Imager->new;
    $small->read(file=>'/home/www/cgi-bin/_small_no_raster.tga',type=>'tga') or die "cannot open image file: ",$large->errstr,"\n";
    $rub_small = $small->convert(matrix=>[ [ 1], [ 1], [ 1], [ -1, 1] ]);

      $img1=Imager->new();
      $img1->open(file=>'/home/www/upload_large.jpg',type=>'jpeg')
        || die "failed: ",$img1->{ERRSTR},"\n";
          $scaled1=$img1->rubthrough(src=>$rub_small, tx=>10, ty=>10);
              $scaled1->write(file=>'/home/www/upload_large.jpg',type=>'jpeg')
                || die "failed: ",$scaled1->{ERRSTR},"\n";

      $img2=Imager->new();
      $img2->open(file=>'/home/www/upload_small.jpg',type=>'jpeg')
        || die "failed: ",$img2->{ERRSTR},"\n";
          $scaled2=$img2->rubthrough(src=>$rub_small, tx=>3, ty=>3);
              $scaled2->write(file=>'/home/www/upload_small.jpg',type=>'jpeg')
                || die "failed: ",$scaled2->{ERRSTR},"\n";

    Hope that helps,

    Greg

    The following message was sent by 07658@xyz.molar.is on Wed, 26 Mar 2003 17:01:01 -0500 (EST).

    >
    > I can't quite figure out how to composite one image on another with a
    > degree of transparency. All the "rubthrough" examples seem to show
    > how to do it by creating a new image and then drawing on it using a 4
    > channel color. I'm thinking that I want to read in two images and
    > then draw to the alpha of one of them. Is this the right approach?
    > If so, how do you draw on just the alpha? If not, how should I do it?
    >
    > Thanks,
    > Thad
    >
    >
    >



  •