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

  • SEARCH
  • Home
  • 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 retrieves data from a database using ADO.NET that
I have to analyse some existing Erlang code. Does anybody knows about a tool
Some existing web services I consume have methods that look something like this: List<Employee>
I have an existing database that has some testing data into and I'm interested
I am implementing NHibernate into an existing web application. However, we have some other
I have written a custom ErrorProvider which adds some functionality to the existing ErrorProvider
I have an existing ASP.NET 2.0 website, stored in Team Foundation Server 2005. Some
I have some UI in VB 2005 that looks great in XP Style, but
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some ASP.NET web services which all share a common helper class they

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.