Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4109802
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:50:55+00:00 2026-05-20T21:50:55+00:00

I have some existing code in C# that I’m trying to move to an

  • 0

I have some existing code in C# that I’m trying to move to an open source scripting language. The general idea is to read in a PNG image, and perform a "custom inversion" color transformation on it.

I want to:

  • simply invert all greyscale (ie R=G=B) pixels (using convert +negate works great for this!)
  • negate the luminance, only, of all non-greyscale pixels (ie, luminance of 1 becomes 0, .2 becomes .8)

Iterating through the pixels and using Graphics::ColorObject to convert the RGB to HSL may be a solution, but the script I wrote to do that is extremely slow, whereas in my C# code (which I would use if I was able to run this in a .NET environment) it only takes a second or two to convert an image.

I’m guessing this is possible with one or more successive convert commands (since convert +negateaccomplishes #1 easily) but am not sure how to do #2.

This is my extremely slow perl code, that I actually haven’t been able to determine if it works because it takes so long. I am NOT much of a perl programmer, so I’m sure this can be optimized several times over.


  #!/usr/local/bin/perl
  use Image::Magick;
  use Graphics::ColorObject;
 my $image;

$image = new Image::Magick;

$image->Read($ARGV[0]);

my $width = $image->Get('width');
my $height = $image->Get('height');
my $w = 0;
my $h = 0;

for($w=0; $w GetPixel(x=>$w,y=>$h);
                #print "checking $w $h: $r $g $b\n";
                if($r == $g && $g == $b) {
                        # grey scale - invert
                        $r = 1 - $r;
                        $g = 1 - $g;
                        $b = 1 - $b;
                } else {
                        # convert only luminance
                        $color = Graphics::ColorObject->new_RGB([$r, $g, $b]);
                        ($h, $s, $l) = @{ $color->as_HSL() };
                        $l = 1 - $l;
                        $newcolor = Graphics::ColorObject->new_HSL([$h, $s, $l]);
                        ($r, $g, $b) = @{ $newcolor->as_RGB() };
                }
                $image->SetPixel(x=>$w,y=>$h,color=>[$r, $g, $b]);
        }
}

$image->Write($ARGV[0] + "_i");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:50:56+00:00Added an answer on May 20, 2026 at 9:50 pm

    I expect this is what you are looking for using convert (done with ImageMagick on Ubuntu)

    ~$ convert rose: -colorspace HSL -channel Luminance -negate rose_out.pnm
    ~$ display rose: &  display rose_out.pnm &
    

    Result is (original left)

    rose / rose_negated_lum

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some existing code that worked fine under Windows 2003, to obtain the
I'm trying to update some existing code that normally runs a Perl script which
I am trying to integrate some pre-existing code that relies on the com.sun.crypto.provider.SunJCE class
I have some existing code that is querying a SQL database repeatedly with different
I have some existing WCF code that accesses SQL Server 2005, but honestly I've
I'm new to StructureMap and have some existing code that I'm working with that
I am developing a C++ MPI application. I have some existing code that is
I have some existing code that retrieves data from a database using ADO.NET that
I have some existing code that I've used to write out an image to
I have some existing code that accepts a java.sql.ResultSet that contains info retrieved from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.