#!/usr/bin/perl-w##ImagetoTextv1.0#byfroque##convertsab&wpngorjpegimag e to t e x tfromaspecifiedfile.$|=1;useGD;usestrict;useGetopt::Std;my%opt;#checkto m a kesu r ewehavebeencalledcorrectlyPrintUsage()&&exitunlessgetopts('vrph:w:H:W : i :t :l : d:',\%opt);#setdefaultvariablesmy$threshold=defined($opt{'d'})?$opt{'d'} : 1 2 8;my$verbose=defined($opt{'v'})?1:0;my$reverse=defined($opt{'r'})?1:0;my$p r ese r v e=defined($opt{'p'})?1:0;my$w=defined($opt{'w'})?$opt{'w'}:1;my$h=defined( $o pt{'h' } ) ? $opt{'h'}:1;my$W=defined($opt{'W'})?$opt{'W'}:0;my$H=defined($opt{'H'})? $o pt{' H'}:0; m y$imgfile=defined($opt{'i'})?$opt{'i'}:PrintUsage()&&exit;my$txtfile=defined ($op t{ 't'}) ?$ o pt{'t'}:$0;#readinandparsethetexttouseprintSTDERR"Readingintext..."if$verbo s e ;o p en(TEXT, $ txtfile)ordie"Erroropening$txtfile:$!";my@text=;close(TEXT);my$text=join ('', @t e xt);$text = ~s/\s//gs;@text=split(//,$text);undef($text);printSTDERR"...done\n"if$verbose;#r eadi n andresizetheim a getouseprintSTDERR"Readinginimage..."if$verbose;my$image;if($imgfile=~/png$/i){$i mage= GD::Image->new Fr omPng($imgfile);}elsif($imgfile=~/jpg$/i){$image=GD::Imag e->newFromJpeg($imgfile);}else{ die"Error:unkno w nf il etype!";}my($x,$y)=$image->getBounds();#resizetheimageif($w!=1or$h!=1or$Wor$H){printSTDERR"...resizing..."if $ver b ose;if($preserve&&($Wor$H)){if($W&&!$H){$H=($y/$x)*$W;}if($H&&!$W) {$W=($x/$y)* $H;}}my$x2=$W?$W:$x;my$y2= $H ?$H:$ y;m y$oimage=$image;$y2=int($y2*$h);$y2=1unless$y2>0;$x2=int($x2* $w) ; $x2=1un le ss$x2>0;$image=GD::Image->ne w($x,$y2) ;$image->copyResized($oimage,0,0,0,0,$x2,$y2,$x,$y);$y=$y2; $x=$x2;}printS T D ERR"...done\n"if$verbose;#runthroug hi mageprintingeitheracharacterfromtextorablankprintSTDERR"Print i ngouttextim a ge..."if$verbose;my$p=0;formy$n (0 ..$y-1){formy$m(0..$x-1){my($r,$g,$b)=$image->rgb($image->getPixel($m,$n) ) ;i f (($r+$g+$b)/3>=$threshold){p ri nt$reverse?$text[$p++]:'';}else{print$reverse?'':$text[$p++];}$p=0if$p>$#te xt;}p rint"\n";}printSTDERR"...do n e \n"if$verbo se;exit;subPrintUsage{printSTDERR "usage:$0[ options]\n";printS TDERR"\ t-rreversecolours\n";prin t S T DERR"\t-ppreserveratio(usedwith-Hand-W)\n";printSTDERR" \ t-dthresholdrg b valuetoconsiderwhite/black\n\n" ;printSTDERR"\t-hheightresizeheightbythisamount\n";printSTD ER R"\t-wwidthre sizewidthbythisamount\n";prin tSTDERR"\t-Hpixelsresizeheighttothismanypixels\n";printST D ERR"\t-Wpixel sresizewidthtothismanypixels \n\n";printSTDERR"\t-iimagefilepngimagefile\n";printSTDERR "\ t-ttextfilete xtfile\n\n";printSTD E RR"\t-vverboseoutput\n\n";}=head1NAMEimage2text-imagetotextconverter=head1SYNO PSISimage2text[optio ns ]-rreversecolours-ppreserveratio(usedw ith-Hand-W )-dt hresholdrgbv aluetoconsiderwhite /black-hheightresizeheightbythisamount-w widthresizewidthbythisamoun t-Hpixelsresizehei ghttothismanypixels-Wpixelsresizewidtht othismanypixels-iimagefil epngimagefile-ttex tfiletextfile-vverboseoutput=head1DESCR IPTIONBconver tsapngorjpegimage intoatextfilesimilartotheimage.Iftheima gei snotalreadyblackandwhite, itattemptstoconve rttheimagetoblac kand white.Itcanalsor e sizetheimage.=head1EXAMPLES convertimage.pngt otextintext.txtfileim age2text-iimage.pn g-ttext.txtconvertimage.png totextintext.txtf ileafterresizingheightby80%image2text-ii mage.png-ttext.txt -h.8conver ti ma ge.pngtotextintext.t xtfileafterresizingheightby40%andresiz ing widthby40%(thiswi llproduceim agesimilartoprevioussamplebuth aveasbig)image2text-iimage.png-ttext. t x t -h.4-w.5conver timage.p ngtotextintext.txtfileafterres izingheightto 89 pixelsandpreservingr a tioofwidtht oheight,th enre sizingheig htto80%of89,andconvertingtheimagetob&w b asedonathre sholdo f50image2text-ii mage.png-ttext.txt-H89-p-h.8-d50acharact ero ftextishigh ert hanitiswide,unlik eapixelwhichhasthesa meheightaswidt h,s oyouwillwantto sc aletheheightofthei mageinrelationtothewidth.i'vefoundt h ataratioof.8:1w or kswell,hencethe"-h. 8"and"-w.5-h.4"usedabove.=head 1 A UTHORfrisco@black a nt.net#!/usr/bin/per l-w##ImagetoTextv1.0 #byfroque ## c onvertsab&wpngorjp egi ma getotextfromaspecifie d file.$|=1;useGD;usestrict;u s eGe topt::Std;my%opt;#c heckto makesurewehavebeencalle dcorrectlyPrintUsage()&&ex it u nl essgetopts('vrph:w:H: W :i: t:l:d:',\%opt);#setdefault variablesmy$threshold=define d($ opt{ ' d'})?$opt{'d'}:128;my$verbo se=defined($opt{'v'})?1:0; my$reverse=defined($opt{'r'})?1:0;m y $ p reserve=defined($opt{'p'})?1:0;my$w=def ined($o pt{'w'})?$opt{'w'}:1;my$h=defined( $ op t{'h'})?$opt{'h'}: 1;my$W=defined($opt{ 'W'})? $opt{'W'}:0;my$H=defined($opt{'H ' })?$opt{'H'}:0;my$ imgfile=defin ed ( $opt{'i'})?$opt{'i'}:PrintUsage()&&e xit ;my$txtfile=defined($opt{'t'})?$opt {'t' }:$0;#readinandparsethetexttouseprintSTD ERR"Readingint ext..."if$verbose; o pen(TEXT,$txtfile)ordie"Erroropening$ txtfile:$!";my@ text= ;clos e (TEXT);my$text=join('',@text);$text =~s/\s//g s;@text=split(//,$text);undef($text ) ;printSTDERR"...done\n"if$verbose;# readinandresizetheimagetouseprintSTDERR"Readinginimage. . ."if$verbose;my$image;if($imgfile=~ / png$/i){$image=GD::Image->newFromPng($imgfile);}e l sif($imgfile=~/jpg$/i){$image=GD:: Image->newFromJpeg($imgfile);}else{die"Error:unk n ownfiletype!";}my($x,$y)=$image- >getBounds();#resizetheimageif($w!=1or$h!=1or$W o r$H){printSTDERR"...resizing... "if$verbose;if($preserve&&($Wor$H)){if($W&&!$H){ $ H=($y/$x)*$W;}if($H&&!$W){$W=( $x/$y)*$H;}}my$x2=$W?$W:$x;my$y2=$H?$H:$y;my$oima g e=$image;$y2=int($y2*$h);$y2= 1unless$y2>0;$x2=int($x2*$w);$x2=1unless$x2>0;$ima g e=GD::Image->new($x, $ y2);$image->copyResized($oimage,0,0,0,0,$x2,$y2,$x, $ y);$y=$y2;$x= $x2;}printSTDERR"...done\n"if$verbose;#runthroughim a geprintin geitheracharacterfromtextorablankprintSTDERR"Printin g outt e xtimage..."if$verbose;my$p=0;formy$n(0..$y-1){formy$m ( 0. .$x-1){my($r,$g,$b)=$image->rgb($image->getPixel($m,$n)) ; i f (($r+$g+$b)/3>=$threshold){print$reverse?$text[$p++]:'';}e lse {pr int$reverse?'':$text[$p++];}$p=0if$p>$#text;}print"\n";}prin tSTDER R". .. d one\n" if$verbose;exit;subPrintUsage{printSTDERR"usage:$0[options]\n";printSTDER R" \t-rr eversecolours\n";printSTDERR"\t-ppreserveratio(usedwith-Hand-W)\n";printSTDERR"\t-d t h r esholdrgbvaluetoconsiderwhite/black\n\n";printSTDERR"\t-hheightresizeheightbythisamo unt\n";printSTDERR"\t-wwidthresizewidthbythisamount\n";printSTDERR"\t-Hpixelsresizeheig httothismanypixels\n";printSTDERR"\t-Wpixelsresizewidthtothismanypixels\n\n";printSTDE