Needed to do this to be able to post to another forum with very limited file type support. Thanks to all above for the hints. Wanted to share my proof of concept code for the next one needing to do this.

Update: Added Wx GUI version below.

#! /usr/bin/perl # text_to_png.pl - Convert a text file into a png format image # James M. Lynes, Jr. - January 15, 2015 # Proof of concept. File names hard coded. Expand to read command line + arguments # or move to Wx gui app. # Tab expansion regex from The Perl Cookbook use strict; use warnings; use GD; open INFILE, "<", "text_to_png.pl" || die; my $image = GD::Image->new(700, 700) || die; my $white = $image->colorAllocate(230,230,230); # backgroun +d color - Lt Gray my $black = $image->colorAllocate(0,0,0); # text color - Bl +ack my $x = 5; my $y = 0; while(<INFILE>) { chomp; 1 while s/\t+/' ' x (($+[0] - $-[0]) * 8 - $-[0] % 8)/e; # do t +ab expansion regex here $image->string(gdMediumBoldFont, $x, $y, $_, $black); $y += 14; } my $png_data = $image->png; open OUTFILE, ">", "text_to_image.png" || die; binmode OUTFILE; print OUTFILE $png_data; close OUTFILE;

Wx Version

#! /usr/bin/perl # Name: wxtext_to_png.pl # Author: James M. Lynes, Jr. # Created: February 11, 2015 # Modified By: James M. Lynes, Jr. # Last Modified: February 11, 2015 # Change Log: 2/11/2015 - Program Created - Wx version using + sizers # and filepicker control. # Based on wxtabexpand.pl structur +e. # Description: Convert a single page text file into a png for +mat image. # Allows conversion of multiple files. # Tab expansion regex from The Perl Cookbook. package main; use strict; use warnings; my $app = App->new(); $app->MainLoop; package App; use strict; use warnings; use base 'Wx::App'; sub OnInit { my $frame = Frame->new(); $frame->Show(1); } package Frame; use strict; use warnings; use Wx qw(:everything); use base qw(Wx::Frame); use GD; use Data::Dumper; sub new { my ($class, $parent) = @_; my $frame = $class->SUPER::new($parent, -1, "Text File to png File + Converter", wxDefaultPosition, wxDefaultSize); #Create controls # Fields my $pdfLabel=Wx::StaticText->new($frame, wxID_ANY,"Select a Text F +ile to Convert to png", wxDefaultPosition, wxDefaultSize); my $pdfFilePicker=Wx::FilePickerCtrl->new($frame, wxID_ANY, "", "S +elect an Input File","*.*", wxDefaultPosition, [400,-1], wxDIRP_DIR_MUST +_EXIST| wxDIRP_CHANGE_DIR|wxDIRP_USE_TEXTCTRL); $pdfFilePicker->SetPath("~/"); $frame->{inpath} = "default"; # Buttons my $okButton = Wx::Button->new($frame, wxID_OK, "", wxDefaultPosit +ion, wxDefaultSize); my $exitButton = Wx::Button->new($frame, wxID_EXIT, "", wxDefaultP +osition, wxDefaultSize); # Create sizers. my $verticalSizerFrame = Wx::BoxSizer->new(wxVERTICAL); $frame->SetSizer($verticalSizerFrame); my $verticalSizerControls = Wx::BoxSizer->new(wxVERTICAL); my $horizontalSizerButtons = Wx::BoxSizer->new(wxHORIZONTAL); # Lay 'em out. $verticalSizerFrame->Add($verticalSizerControls,0,0,0); $verticalSizerFrame->Add($horizontalSizerButtons,0,0,0); $verticalSizerControls->AddSpacer(15); # Spacer $verticalSizerControls->Add($pdfLabel,0,0,0); $verticalSizerControls->Add($pdfFilePicker,0,0,0); $verticalSizerControls->AddSpacer(15); # Spacer $horizontalSizerButtons->Add($okButton,0,0,0); $horizontalSizerButtons->Add($exitButton,0,0,0); # Event handlers Wx::Event::EVT_BUTTON($frame, $okButton, sub { my ($self, $event) = @_; #print "\nOK Button\n"; open INFILE, "<", $self->{inpath} || die; my $image = GD::Image->new(700, 700) || die; my $white = $image->colorAllocate(230,230,23 +0); # background color - Lt Gray my $black = $image->colorAllocate(0,0,0); + # text color - Black my $x = 5; my $y = 0; while(<INFILE>) { chomp; 1 while s/\t+/' ' x (($+[0] - $-[0]) * 8 + - $-[0] % 8)/e; # do tab expansion regex here $image->string(gdMediumBoldFont, $x, $y, + $_, $black); $y += 14; } my $png_data = $image->png; open OUTFILE, ">", $self->{inpath}.".png" || + die; binmode OUTFILE; print OUTFILE $png_data; close INFILE; close OUTFILE; }); Wx::Event::EVT_BUTTON($frame, $exitButton, sub { my ($self, $event) = @_; #print "\nEXIT Button\n"; $self->Close; }); Wx::Event::EVT_FILEPICKER_CHANGED($frame, $pdfFilePicker, sub { my ($self, $event) = @_; $self->{inpath} = $pdfFilePicker->GetPath; }); $verticalSizerFrame->Layout(); return $frame; } 1;

James

There's never enough time to do it right, but always enough time to do it over...


In reply to Re: Adding text to an image for printing by jmlynesjr
in thread Adding text to an image for printing by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.