Code for image compression

Discussion in 'Perl' started by Dak914, Jul 18, 2008.

  1. Dak914

    Dak914 Member

    Joined:
    May 3, 2008
    Messages:
    48
    Likes Received:
    5
    Trophy Points:
    8
    Not sure if it is really good code but i have been working on it in perl. You need Image::Magick for it to work...
    here it is:

    compression:

    Code:
    #!/usr/bin/perl
    use Image::Magick;
    #Ask for number of images to compress
    sub first
    {
    	print "type the number of images to compress\n";
    	print "2, 3, 4\n";
    	$number=<>;
    	chomp $number;
    	if ($number eq 2)
    	{
    		&two;
    	} elsif ($number eq 3) {
    		&three;
    	} elsif ($number eq 4) {
    		&four;
    	} else {
    		print "your numb#!/usr/bin/perl
    			use Image::Magick;
    #Ask for number of images to compress
    		sub first
    		{
    			print "type the number of images to compress\n";
    			print "2, 3, 4\n";
    			$number=<>;
    			chomp $number;
    			if ($number eq 2)
    			{
    				&two;
    			} elsif ($number eq 3) {
    				&three;
    			} elsif ($number eq 4) {
    				&four;
    			} else {
    				print "your number did not match any of the above numbers, try again\n\n";
    				reset (&first);
    				&first;
    			}
    		}
    #If desired number is two
    		sub two
    		{
    			print "input two images to compress\n";
    			print "1)";
    			$photo1=<>;
    			print "2)";
    			$photo2=<>;
    			chomp ($photo1, $photo2);
    			$image1=Image::Magick->new;
    			$image1->Read ("$photo1", "$photo2");
    			$image1->Write ('x2.gif');
    			undef $image1;
    			undef $photo1;
    			undef $photo2;
    			undef $number;
    			&first;
    		}
    #If desired number is three
    		sub three
    		{
    			print "input three images to compress\n";
    			print "1)";
    			$photo3=<>;
    			print "2)";
    			$photo4=<>;
    			print "3)";
    			$photo5=<>;
    			chomp ($photo3, $photo4, $photo5);
    			$image2=Image::Magick->new;
    			$image2->Read ("$photo3", "$photo4", "$photo5");
    			$image2->Write ('x3.gif');
    			undef $image2;
    			undef $photo3;
    			undef $photo4;
    			undef $photo5;
    			undef $number;
    			&first;
    		}
    #If desired number is four
    		sub four
    		{
    			print "input four images to compress\n";
    			print "1)";
    			$photo6=<>;
    			print "2)";
    			$photo7=<>;
    			print "3)";
    			$photo8=<>;
    			print "4)";
    			$photo9=<>;
    			chomp ($photo6, $photo7, $photo8, $photo9);
    			$image3=Image::Magick->new;
    			$image3->Read ("$photo6", "$photo7", "$photo8", "$photo9");
    			$image3->Write ('x4.gif');
    			undef $image3;
    			undef $photo6;
    			undef $photo7;
    			undef $photo8;
    			undef $photo9;
    			undef $number;
    			&first;
    		}
    		&first;er did not match any of the above numbers, try again\n\n";
    			reset (&first);
    		&first;
    	}
    }
    #If desired number is two
    sub two
    {
    	print "input two images to compress\n";
    	print "1)";
    	$photo1=<>;
    	print "2)";
    	$photo2=<>;
    	chomp ($photo1, $photo2);
    	$image1=Image::Magick->new;
    	$image1->Read ("$photo1", "$photo2");
    	$image1->Write ('x2.gif');
    	undef $image1;
    	undef $photo1;
    	undef $photo2;
    	undef $number;
    	&first;
    }
    #If desired number is three
    sub three
    {
    	print "input three images to compress\n";
    	print "1)";
    	$photo3=<>;
    	print "2)";
    	$photo4=<>;
    	print "3)";
    	$photo5=<>;
    	chomp ($photo3, $photo4, $photo5);
    	$image2=Image::Magick->new;
    	$image2->Read ("$photo3", "$photo4", "$photo5");
    	$image2->Write ('x3.gif');
    	undef $image2;
    	undef $photo3;
    	undef $photo4;
    	undef $photo5;
    	undef $number;
    	&first;
    }
    #If desired number is four
    sub four
    {
    	print "input four images to compress\n";
    	print "1)";
    	$photo6=<>;
    	print "2)";
    	$photo7=<>;
    	print "3)";
    	$photo8=<>;
    	print "4)";
    	$photo9=<>;
    	chomp ($photo6, $photo7, $photo8, $photo9);
    	$image3=Image::Magick->new;
    	$image3->Read ("$photo6", "$photo7", "$photo8", "$photo9");
    	$image3->Write ('x4.gif');
    	undef $image3;
    	undef $photo6;
    	undef $photo7;
    	undef $photo8;
    	undef $photo9;
    	undef $number;
    	&first;
    }
    &first;
    
    Decompression:
    
    #!/usr/bin/perl
    use Image::Magick;
    print "enter gif to decompress\n";
    $in=<>;
    chomp $in;
    $image= Image::Magick->new;
    $image->Read ("$in");
    $image->Write ('x.png');
    undef $image;
    
    

    That is all
    A friend and i use it to send messages to each other...LOL Childish......
     
    Last edited by a moderator: Jul 19, 2008
  2. Dak914

    Dak914 Member

    Joined:
    May 3, 2008
    Messages:
    48
    Likes Received:
    5
    Trophy Points:
    8
    If anyone can tell me how to shorten it up just a bit that would be very helpful...
     
  3. Dak914

    Dak914 Member

    Joined:
    May 3, 2008
    Messages:
    48
    Likes Received:
    5
    Trophy Points:
    8
    to use it just go to the directory you put the *.pl file in and type "perl *.pl"
    you must type the extensions for the images
    CASE SENSITIVE
    ~DBK
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Moved to PERL forum
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    I didn't quite understand the objective of the program, does it combine two images into one?
     
  6. Dak914

    Dak914 Member

    Joined:
    May 3, 2008
    Messages:
    48
    Likes Received:
    5
    Trophy Points:
    8
    Yes it does. It will create a single gif image from several other images. A gif image is seen as animations on the web. Sorry I havent been on in a while. Just been stuck doing other stuff.
    Learn.
    ~Connor
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice