Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2009/04/01

Confronting Big Avatars with Perl and Magick

I do twitter. I run Linux. I use Twitux, not because I like it the most but because I dislike it the least of everything I can run without installing AIR.

It has a problem, however. Avatars are 48x48, but if you upload a way-too-freakin'-big avatar, like the huge pics you can get off your phone these days, it doesn't complain. It just screws up your display. And I finally got sick of it.

  1. #!/usr/bin/perl  
  2.   
  3. use Modern::Perl ;  
  4. use Image::Magick ;  
  5. use subs qw{ check_image fix_image } ;  
  6.   
  7. # probably an easy change to make this more portable.  
  8. # oh well  
  9. my $dir = '/home/jacoby/.gnome2/twitux/avatars' ;  
  10.   
  11. #go through all avatars  
  12. if ( opendir my $DH , $dir ) {  
  13.     while ( definedmy $file = readdir$DH ) ) ) {  
  14.         next if $file =~ m{^\.}mx ;  
  15.         my $full_filename = join '/' , $dir , $file ;  
  16.         check_image $full_filename ;  
  17.         }  
  18.     }  
  19. else {  
  20.     say 'Fail' ;  
  21.     }  
  22.   
  23. # are they 48x48?  
  24. sub check_image {  
  25.     my $file = shift ;  
  26.     my $image = new Image::Magick ;  
  27.     $image->Read($file) ;  
  28.     my $width  = $image->Get('width')  || 0 ;  
  29.     my $height = $image->Get('height') || 0 ;  
  30.     return if $height == 48 && $width == 48 ;  
  31.     return fix_image $file ;  
  32.     }  
  33.   
  34. #make them 48x48.  
  35. sub fix_image {  
  36.     my $file = shift ;  
  37.     my $image = new Image::Magick ;  
  38.     $image->Read($file) ;  
  39.     $image->Resize(width=>48,height=>48) ;  
  40.     $image->Write($file) ;  
  41.     return ;  
  42.     }  


You need Perl. You need Image::Magick. You need crontab to run it ever so often.

No comments:

Post a Comment