Anonymous

User talk:BytePilot: Difference between revisions

From London Hackspace Wiki
working!
No edit summary
(working!)
Line 236: Line 236:
     }
     }
</pre>
</pre>
<hr>
OK, well its good to see that it's working more-or-less.
I've been trying to get it to accept a value instead of a percentage - it will - have to assume that MaxRGB = 256 (which depends on the settings when GraphicsMagick was compiled - so in a way it would be more portable to do it as a percentage - but values less than 1% appear to be rounded by GM).
so anyway, if we assume MaxRGB = 256 then it works OK.
http://hack.rs/cgi-bin/threshold_grayscale.pl
<pre>
#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use List::Util qw(max min);
my $q = CGI->new();
print $q->header;
print $q->start_html;
print $q->p("this is a thing for doing threshholding");
print $q->p("<small>or possibly <i>thresholding</i>?</small>");
print $q->start_form( -enctype => "multipart/form-data" );
print $q->p("file");
print $q->filefield('uploaded_file');
print $q->submit();
print $q->end_form;
# do we have an upload?
my $filehandle = $q->upload('uploaded_file');
if (defined $filehandle) {
    # do shit
    print $q->p("I'm doing shit");
    # no, actually do shit
    my ($filename, $extension) = ($q->param('uploaded_file') =~ /^(\S+)\.(\S+)$/);
    my $tempfile = $q->tmpFileName($q->param('uploaded_file'));
    foreach my $i (0 .. 7) {
        my $n = 2 ** $i;
        my $outfile = "$filename"."_$i.$extension";
        my @ar = ("gm",
                  "convert",
                    "-operator", "Gray", "And", $n ,
                    "-operator", "Gray", "Threshold",  $n - 1  ,
                  $tempfile,
                  "/var/www/threshold_output/$outfile" );
        print $q->p("$i : " . join " ", @ar);
        system(@ar) == 0 or die "system call failed: $? $!"; #safer than passing a string to system(),
                                                            # because doing it this way bypasses the shell
        print $q->img({src => "/threshold_output/$outfile"});
    }
}
print $q->end_html;
</pre>
--[[User:AndyE|AndyE]] 12:21, 13 January 2011 (UTC)
<hr>
105

edits