[Bioperl-l] Bio::Graphics transparent background

bioperl-list at superfrink.net bioperl-list at superfrink.net
Thu Sep 13 05:15:39 UTC 2007


> Is it possible to set the bg colour of glyphs and the panel background
> to be transparent? If so, which output formats support transparency?

I had a look at the code and I don't believe it is possible.

You could produce a PNG file and knowing the red/green/blue values of the 
background colour run the following script to make an image with the bg 
colour transparent.

For example:
    ./make-transparent.pl 252 253 252 2004-11-22.png

will produce:  2004-11-22.png.new.png
with the RGB colour of (252, 253, 252) replaced with transparency.

Regards,
Chad


#!/usr/bin/perl -w
#
# file: make-transparent.pl
# purpose: make a single colour in a PNG file transparent
# author: chad c d clark
# $Id$

use strict;
use GD;

# -- subroutines -------------------------------------------------------
sub usage_message();


# -- main() ------------------------------------------------------------

if(scalar @ARGV < 4) {
     print usage_message();
     exit 1;
}

# get the colour and make sure it is valid
my @RGB = splice @ARGV, 0, 3;
for my $i (@RGB) {

     if ( ($i !~ /^[\d]+$/) or (255 < $i) ) {
 	print "Invalid colour '$i'.\n";
         print usage_message();
         exit 1;
     }
}
print "RGB: (@RGB)\n";

# process each file
FILE: while (my $filename = shift @ARGV) {

     # read the file
     my $image = GD::Image->new($filename);
     unless(defined $image) {
 	warn "Unable to read image from file.  Skipping '$filename'.\n";
 	next FILE;
     }

     # find the colour index
     my $index = $image->colorExact(@RGB);
     if(-1 == $index) {
 	warn "Colour not found in file.  Skipping '$filename'.\n";
 	next FILE;
     }

     # make the colour index transparent
     if(-1 == $image->transparent($index)) {
 	warn "Unable to make colour transparent.  Skipping '$filename'.\n";
 	next FILE;
     }

     # write the updated image file
     my $new_filename = $filename . ".new.png";
     # my $new_filename = $filename;  # use to over-write existing file
     open FH, ">" . $new_filename or die "can't open $new_filename";
     print FH $image->png;
     close FH;

     print "Found file '$filename'.\tCreated '$new_filename'.\n";
}

exit 0;


# -- subroutines -------------------------------------------------------

sub usage_message() {

return qq/
   Usage: $0 RED GREEN BLUE FILELIST

   Where:
       RED       - red   value in decimal (0 to 255)
       GREEN     - green value in decimal (0 to 255)
       BLUE      - blue  value in decimal (0 to 255)
       FILELIST  - list of files to convert

   Examples:
       $0 255 255 255 2004-11-22.png
       $0 252 253 252 2004-11-22.png second.png
       $0 1   1   200 2004-11-22.png second.png third.png

   Description:
   For each file "foo.png" a new file "foo.png.new.png" will be created (and
   over-written if it existed).  The new file will be the same as the original
   but the colour specified by the RED, GREEN, and BLUE value will be removed
   and replaced by transparent pixels.

/;
}



More information about the Bioperl-l mailing list