Make cards from YAML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.8 KiB

  1. #!/usr/bin/env perl
  2. use YAML::XS qw(LoadFile);
  3. use Image::Magick;
  4. use Text::Wrap;
  5. my $cards;
  6. if($ARGV[0]){
  7. $cards = LoadFile($ARGV[0]);
  8. }else{
  9. print "Usage: CardGen.pl [yaml file of cards]\n";
  10. exit 1;
  11. }
  12. if($cards->{Deck}->{Background}){
  13. $bgcolour = $cards->{Deck}->{Background};
  14. }else{
  15. $bgcolour='white';
  16. }
  17. if($cards->{Deck}->{Font}){
  18. $mainfont = $cards->{Deck}->{Font};
  19. }else{
  20. $mainfont = 'Courier';
  21. }
  22. if($cards->{Deck}->{TextColour}){
  23. $txtcolour = $cards->{Deck}->{TextColour};
  24. }else{
  25. $txtcolour = 'black';
  26. }
  27. if($cards->{Deck}->{BorderColour}){
  28. $bordercolour = $cards->{Deck}->{BorderColour};
  29. }else{
  30. $bordercolour = 'red';
  31. }
  32. foreach my $card (keys %{$cards->{Deck}->{Cards}}){
  33. my $image = Image::Magick->new(size=>'600x825');
  34. $image->ReadImage('canvas:' . $bgcolour);
  35. $image->Draw(stroke=>$bordercolour, fill => 'none', strokewidth=>1, primitive=>'rectangle', points=>'38,38 562,787');
  36. my $surface = Image::Magick->new;
  37. $surface->Rotate(90);
  38. $surface->Resize('600x825!');
  39. $image->Composite(compose => 'over', image => $surface, x => 0, y => 0);
  40. my $text;
  41. $image->Set(font => $mainfont, pointsize => 25);
  42. $Text::Wrap::columns = 30;
  43. foreach my $line (@{$cards->{Deck}->{Cards}->{$card}}){
  44. $text = $text . wrap('', '', $line) . "\n";
  45. }
  46. $Text::Wrap::columns = 15;
  47. $image->Annotate(text => wrap('', '', $card), x => 50, y => 100, font => $mainfont, fill => $txtcolour, pointsize => 60);
  48. my $newlines = () = $text =~ /(\n)/g;
  49. $image->Annotate(text => $text, x => 50, y => (750-(25*$newlines)), font => $mainfont, fill => $txtcolour, pointsize => 25);
  50. my $backgroundfilename = "Backgrounds/" . $card . ".png";
  51. if(-e $backgroundfilename){
  52. my $background = Image::Magick->new;
  53. $background->ReadImage($backgroundfilename);
  54. $image->Composite(compose => 'Atop', image => $background, gravity => 'Center');
  55. }
  56. $image->Write($card . '.png');
  57. }