User talk:BytePilot

From London Hackspace Wiki
Revision as of 12:21, 13 January 2011 by AndyE (talk | contribs) (working!)
Jump to navigation Jump to search

well I've done that, but the output is not what we want.

http://hack.rs/cgi-bin/threshold_bitwise.pl


#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

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 = $q->param('uploaded_file');
    my $tempfile = $q->tmpFileName($filename);

    foreach my $i (0 .. 7) {

        my $n = 2 ** ($i + 8);

        my @ar = ("gm",
                  "convert", "-type", "grayscale", "-colors", 256, "-operator", "ALL AND $n", "-threshold", $n-1,
                  $tempfile,
                  "/var/www/threshold_output/$filename".$i );

        print $q->p("$i : $n : " . join " ", @ar);

        system(@ar) == 0 or carp "system call failed: $?"; #safer than passing a string to system()

        print $q->img({src => "/threshold_output/$filename".$i});
    }
}


print $q->end_html;

I tried doing just the "-operator ALL AND $n" part by itself, it didn't seem to do anything...

--AndyE 14:16, 12 January 2011 (UTC)

Possible sytax error ?

                  "convert", "-type", "grayscale", "-colors", 256, "-operator", "ALL AND $n", "-threshold", $n-1,

Should perhaps be

                  "convert", "-type", "grayscale", "-colors", 256, "-operator", "ALL AND", $n, "-threshold", $n-1,

?


yes - seems to need to be "All", "And", "Whatever". Still, here's one that kinds does what we want...

http://hack.rs/cgi-bin/threshold_grayscale.pl


#!/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 = $q->param('uploaded_file');
    my $tempfile = $q->tmpFileName($filename);

    foreach my $i (0 .. 7) {

        my $n = ((2 ** (8 + $i)) / 2**16 )*100; #as a percentage

        my @ar = ("gm",
                  # "convert", "-type", "grayscale", "-colors", 8, "-threshold", 12.5*$i ."%",
                  "convert", "-type", "grayscale", "-operator",
                    "Gray", "And", $n   ."%",
                    "-threshold", max($n -1, 0) ."%",
                  $tempfile,
                  "/var/www/threshold_output/$filename".$i );

        print $q->p("$i : " . join " ", @ar);

        system(@ar) == 0 or carp "system call failed: $?"; #safer than passing a string to system()

        print $q->img({src => "/threshold_output/$filename".$i});
    }
}


print $q->end_html;



i converted the raw values to percentages of a max because it wasn't working the other way. I'm not clear on the bit depth of the greyscale image. tried setting it with -depth but still no joy. still, this version is kind of working as it is.

--AndyE 15:12, 12 January 2011 (UTC)


it might be better if, rather than laz0ring the same location multiple times, we converted the image to 8 shades of grey and then picked out each shade as a seperate layer with it's own power settings. that's what i was starting to try to do in the commented-out bit above - not sure how to pick out each shade.

probably worth experimenting with both methods on the laser cutter

--AndyE 15:20, 12 January 2011 (UTC)

Seems to work pretty darn well. Original Brushedsteeld20.jpg

Result ater rebuilding in GIMP Rebuilt.jpg

If we wanted to get the same resolution we'd need to pick out 256 shades of grey to laser, or better still hack the laser software to allow us to control the burn strength per pixel.


Below is another example. It generates N times a B&W bitmap. Each is then fed to the laser and overlaid. So the darkest point gets N shots; the lightest just one or two. Note the log/lin mappings - which sort of do not apply to all materials!

#!/usr/bin/perl
use GD;

# Number of successive burns
#
my $N = 4; 

die "Syntax: $0 <image.png>"
	unless $#ARGV==0;
my ($file) = @ARGV;

$in = GD::Image->new($file)
	or die "Could not load '$file': $!\n";

$gray = new GD::Image($in->width, $in->height);
my @lin=();
map { $lin[$_] = $gray->colorAllocate($_,$_,$_); } (0 .. 255); ## linear - not eye or laser.

map { 
        # calculate a cutoff point on an exp. scale. as this is what the burn
        # intensity versus grayness sort of is.
        #
	push @coff,int(10 ** log($_)/log($N) + 0.5); 

        # create output layer
	my $layer = GD::Image->newPalette($gray->width, $gray->height);
	$layer->colorAllocate(255,255,255) == 0 or die $.;
	$layer->colorAllocate(0,0,0) == 1 or die $.;
	push @out,$layer;
} (1 .. $N);

my @histogram = ();

for(my $x = 0; $x < $gray->width; $x++) {
	for(my $y = 0; $y < $gray->height; $y++) {
		my ($r,$g,$b) = $in->rgb($in->getPixel($x,$y));
#		my $val = $g;
		my $val = int((2*$r + 4*$g + $b)/7 + 0.5);
		for(my $i = 0; $i < $N; $i++) {
			$gray->setPixel($x,$y,$lin[ $val ]);
			next unless $val > $coff[$i];
			$out[$i]->setPixel($x,$y,1);
			$histogram[$i]++;
		};
	};
};
map {
	print "Layer $_\t$histogram[$_]\n";
} (0 .. $N-1);

open(FH,">gray.png") or die $!; print FH $gray->png; close(FH);
for(my $i = 0; $i < $N; $i++) {
	open(FH,">layer_$i.png") or die $!;
	print FH $out[$i]->png;
	close(FH);
};

exit 0;

use SNMP;

&SNMP::initMib();
foreach my $oid (keys(%SNMP::MIB)) {
	print "OID=", $oid, "\n";
        print "\tTYPE=", $SNMP::MIB{$oid}{'type'}, 
		"\n\tLABEL=",$SNMP::MIB{$oid}{'label'}, "\n";
        print "\tDESCRIPTION=", $SNMP::MIB{$oid}{'description'}, "\n";
    }



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


#!/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;

--AndyE 12:21, 13 January 2011 (UTC)