加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何使用Perl中的Image :: Magic增加特定像素的RGB值?

发布时间:2020-12-14 01:02:24 所属栏目:Linux 来源:网络整理
导读:我想获得1个像素(x = 3,y = 3)并改变其RGB值(R从100变为101,G从99变为100,B从193变为194). use strict;use Image::Magick;my $p = new Image::Magick; $p-Read( 'myfile.jpg' ); my $pix = $p-GetPixel( width = 1,height = 1,x = 3,y = 3,map = 'RGB',norma
我想获得1个像素(x = 3,y = 3)并改变其RGB值(R从100变为101,G从99变为100,B从193变为194).

enter image description here

use strict;
use Image::Magick;
my $p = new Image::Magick;
   $p->Read( 'myfile.jpg' );

   my $pix = $p->GetPixel(
            width     => 1,height    => 1,x         => 3,y         => 3,map       => 'RGB',normalize => 0
        );

   # in $pix RGB value now?

如何为所有RGB组件添加1?

我可以将十进制RGB分成3个值(r,g,b)并单独递增,
然后将三个R,G,B值合并为一个RGB? :) 我怎么做?

$pix = .... something code here...

   # make changes
   $p->SetPixel(
            x       => 3,y       => 3,channel => 'RGB',color   => [ $pix ]
        );
   $p->Write ('my_new_file.jpg');

解决方法

要弄清楚这有点棘手,但我们走了.我会告诉你我做了什么来得到结果,而不仅仅是它是如何工作的.

我正在使用一个具有起始颜色(100,99,193)的小图像.

starting image and color

在我的程序的顶部,我将始终拥有此代码.

use strict;
use warnings;
use Data::Printer;
use Image::Magick;
my $p = new Image::Magick;
$p->Read('33141038.jpg');

my @pixel = $p->GetPixel(
    x         => 1,y         => 1,normalize => 1,);

我检查了the documentation at imagemagick.org..它在Image::Magick on CPAN链接.在那里我搜索了GetPixel.这产生两个有用的东西.一个是解释,另一个是an example,表示返回一个数组@pixel,而不是你尝试过的标量.

Here we reduce the intensity of the red component at (1,1) by half:

06001

好.让我们用它.我上面的代码中已经有了@pixel.请注意,我还打开了normalize选项.默认情况下,您可以将其保留为打开状态.

p @pixel;

# [
#     [0] 0.392156862745098,#     [1] 0.388235294117647,#     [2] 0.756862745098039
# ]

所以这些都是花车.经过一些谷歌搜索后,我找到了this answer,它处理类似的事情.它看起来像255的一小部分.让我们相乘.我们可以通过在postfix foreach中指定$_来修改@pixel中的内容.那很整齐.

$_ *= 255 foreach @pixel;
p @pixel;

# [
#     [0] 100,#     [1] 99,#     [2] 193
# ]

这就是我们想要的.很容易.我们各添加一个.

$_ = ( $_ * 255 ) + 1 foreach @pixel;
p @pixel;

# [
#     [0] 101,#     [1] 100,#     [2] 194
# ]

还好.但是我们如何重新开始呢?文档在Manipulate section中有关于SetPixel的说法.

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

显然我们需要回到浮动状态.没问题.

$_ = ( ( $_ * 255 ) + 1 ) / 255 foreach  @pixel;
p @pixel;

# [
#     [0] 0.396078431372549,#     [1] 0.392156862745098,#     [2] 0.76078431372549
# ]

尼斯.我们当然也可以让数学更短一些.结果是一样的.

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

现在让我们把它写回图像.

$p->SetPixel(
    x => 1,y => 1,color => @pixel,# need the array ref here
);

$p->Write('my_new_file.jpg');

在屏幕截图中,我将其更改为添加20而不是1,因此更加明显.

New image with +20 including freehand circles

清理后代码看起来像这样.

use strict;
use warnings;
use Data::Printer;
use Image::Magick;

my $p = new Image::Magick;
$p->Read('33141038.jpg');

my @pixel = $p->GetPixel(
    x => 1,);

# increase RGB by 1 each
$_ = $_ + 1 / 255 foerach @pixel;

$p->SetPixel(
    x     => 1,y     => 1,);

$p->Write('my_new_file.jpg');

我已经从GetPixel和SetPixel中删除了地图和通道参数,因为RGB是默认值.标准化相同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读