#!/usr/bin/env perl use YAML::XS qw(LoadFile); use Image::Magick; use Text::Wrapper; my $cards; $wrapper = Text::Wrapper->new(columns=>15,body_start => ''); if($ARGV[0]){ $cards = LoadFile($ARGV[0]); }else{ print "Usage: CardGen.pl [yaml file of cards]\n"; exit 1; } if($cards->{Deck}->{Background}){ $bgcolour = $cards->{Deck}->{Background}; }else{ $bgcolour='white'; } if($cards->{Deck}->{Font}){ $mainfont = $cards->{Deck}->{Font}; }else{ $mainfont = '/usr/X11R6/lib/X11/fonts/TTF/DejaVuSansMono.ttf'; } if($cards->{Deck}->{TextColour}){ $txtcolour = $cards->{Deck}->{TextColour}; }else{ $txtcolour = 'black'; } if($cards->{Deck}->{BorderColour}){ $bordercolour = $cards->{Deck}->{BorderColour}; }else{ $bordercolour = 'red'; } my $timage = Image::Magick->new(size=>'600x825'); foreach my $card (keys %{$cards->{Deck}->{Cards}}){ my $image = Image::Magick->new(size=>'600x825'); $image->ReadImage('canvas:' . $bgcolour); $image->Draw(stroke=>$bordercolour, fill => 'none', strokewidth=>1, primitive=>'rectangle', points=>'38,38 562,787'); my $surface = Image::Magick->new; $surface->Rotate(90); $surface->Resize('600x825!'); $image->Composite(compose => 'over', image => $surface, x => 0, y => 0); my $text; $image->Set(font => $mainfont, pointsize => 25); foreach my $line (@{$cards->{Deck}->{Cards}->{$card}}){ $text = $text . $wrapper->wrap($line) . "\n"; } $image->Annotate(text => $wrapper->wrap($card), x => 50, y => 100, font => $mainfont, fill => $txtcolour, pointsize => 60); my $newlines = () = $text =~ /(\n)/g; $image->Annotate(text => $text, x => 50, y => (750-(25*$newlines)), font => $mainfont, fill => $txtcolour, pointsize => 25); my $backgroundfilename = "Backgrounds/" . $card . ".png"; if(-e $backgroundfilename){ my $background = Image::Magick->new; $background->ReadImage($backgroundfilename); $image->Composite(compose => 'Atop', image => $background, gravity => 'Center'); } $image->Write($card . '.png'); }