画像をタイル状に並べたいなと思っていて調べていると
GD::Tilerというモジュールが使えそうだったのでサンプルコードを
書いてみました。
コード
最近 CPANにアップデートした人の顔写真を横 4つで
並べていくというものです。
#!perl use strict; use warnings; use Web::Scraper; use GD::Tiler qw(tile); use URI; use Furl; get_recent_author_image(); tile_images(); sub tile_images { my $output = 'output.png'; my ($img, @coords) = GD::Tiler->tile( Images => [ grep { $_ ne $output } glob '*.png' ], VEdgeMargin => 10, HEdgeMargin => 10, VTileMargin => 5, HTileMargin => 5, ImagesPerRow => 4, ); open my $fh, '>', $output or die "Can't open $output\n"; print {$fh} $img; close $fh; } sub get_recent_author_image { my $recents = scraper { process '.author', "authors[]" => scraper { process 'a > div > img', image => '@src'; }; }; my $res = $recents->scrape( URI->new('http://frepan.org/')); my $index = 1; my $furl = Furl->new; for my $author (@{$res->{authors}}) { my $img_url = $author->{image}->as_string; my $res = $furl->get( $img_url ); unless ( $res->is_success ) { warn "Can't download $img_url\n"; next; } my $file = $index++ . ".png"; open my $fh, '>', $file or die "Can't open $file"; print {$fh} $res->content; close $fh; } }
tile関数の戻り値はオブジェクトとドキュメントにはあるんだけど、
どうも生のデータが返ってきているように思えます。形式とかに
よるのかもしれないですが、確認してません。
おわりに
画像処理でいろいろできると楽しいものだなぁと思いました。