linux - How do I increase the RGB value of a specific pixel with Image::Magic in Perl? -


i want 1 pixel (x=3, y=3) , change rgb values (r 100 101, g 99 100, b 193 194).

enter image description here

use strict; use image::magick; $p = new image::magick;    $p->read( 'myfile.jpg' );     $pix = $p->getpixel(             width     => 1,             height    => 1,             x         => 3,             y         => 3,             map       => 'rgb',             normalize => 0         );     # in $pix rgb value now?  

how add 1 rgb components?

can split decimal rgb 3 values (r,g,b) , increment separately, , merge 3 r,g,b values 1 rgb? :) how do that?

   $pix = .... code here...     # make changes    $p->setpixel(             x       => 3,             y       => 3,             channel => 'rgb',             color   => [ $pix ]         );    $p->write ('my_new_file.jpg'); 

this bit tricky figure out, here go. i'll show did result, not how works.

i'm using small image has starting color (100, 99, 193).

starting image , color

at top of program have code.

use strict; use warnings; use data::printer; use image::magick; $p = new image::magick; $p->read('33141038.jpg');  @pixel = $p->getpixel(     x         => 1,     y         => 1,     map       => 'rgb',     normalize => 1, ); 

i checked the documentation @ imagemagick.org.. linked in image::magick on cpan. there searched getpixel. yields 2 helpful things. 1 explanation, other 1 an example shows array @pixel returned, , not scalar tried.

here reduce intensity of red component @ (1,1) half:

@pixels = $image->getpixel(x=>1,y=>1); 

ok. let's use that. i've got @pixel in code above. note turned on normalize option. can leave out it's on default.

p @pixel;  # [ #     [0] 0.392156862745098, #     [1] 0.388235294117647, #     [2] 0.756862745098039 # ] 

so floats. after googling found this answer, deals similar. looks fraction of 255. let's multiply. can modify things in @pixel assigning $_ in postfix foreach. that's neat.

$_ *= 255 foreach @pixel; p @pixel;  # [ #     [0] 100, #     [1] 99, #     [2] 193 # ] 

that's wanted. easy enough. let's add 1 each.

$_ = ( $_ * 255 ) + 1 foreach @pixel; p @pixel;  # [ #     [0] 101, #     [1] 100, #     [2] 194 # ] 

still good. how in? docs have setpixel in manipulate section.

color=>array of float values
[...]
set single pixel. default normalized pixel values expected.

so apparently need go float. no problem.

$_ = ( ( $_ * 255 ) + 1 ) / 255 foreach  @pixel; p @pixel;  # [ #     [0] 0.396078431372549, #     [1] 0.392156862745098, #     [2] 0.76078431372549 # ] 

nice. can of course make math bit shorter. result same.

$_ = $_ + 1 / 255 foreach @pixel; 

now let's write image.

$p->setpixel(     x => 1,     y => 1,     color => \@pixel, # need array ref here );  $p->write('my_new_file.jpg'); 

in screenshot, changed add 20 instead of 1 it's more visible.

new image +20 including freehand circles

after cleaning code looks this.

use strict; use warnings; use data::printer; use image::magick;  $p = new image::magick; $p->read('33141038.jpg');  @pixel = $p->getpixel(     x => 1,     y => 1, );  # increase rgb 1 each $_ = $_ + 1 / 255 foerach @pixel;  $p->setpixel(     x     => 1,     y     => 1,     color => \@pixel, );  $p->write('my_new_file.jpg'); 

i've removed map , channel arguments getpixel , setpixel rgb default. same normalize.


Comments

Popular posts from this blog

r - how do you merge two data frames the best way? -

How to debug "expected android.widget.TextView but found java.lang.string" in Android? -

php - mySQL problems with this code? -