Greyscale shift or Interpolation

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Greyscale shift or Interpolation

Post by mjamal »

Hi Team,

Can anyone have any idea how we can get the image lowest and highest pixel in an image. And also what I need to do is that we need to get the images lighter value and darker values and also need to adjust the low as well as high values to user defined.
Like we have an image with a color range of 0-235 and we need to narrowed that range to 50-200. It would take our darkest color (0) and bump it to 50, and proportionally adjust the balance of the colors up, with the consideration that the colors from 255 were also being adjusted to 200.

How can I do this by using ImageMagick commands? I am using OS as CentOS and my ImageMagick version is 7.0.8.
Please provide your suggestions.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Greyscale shift or Interpolation

Post by snibgo »

You can get the minimum and maximum pixel values, on a scale from 0.0 to 1.0, like this (bash syntax):

Code: Select all

$ magick toes.png -format "%[fx:minima]\n%[fx:maxima]\n" info:
0
1
If you prefer a scale from 0.0 to 255.0, you can multiply:

Code: Select all

$ magick toes.png -format "%[fx:minima*255]\n%[fx:maxima*255]\n" info:
0
255
My input already spans the full range. If it didn't, I could "-auto-level" first.

To reduce the range, use "+level". See http://www.imagemagick.org/script/comma ... .php#level . I generally use this with percentages, like this:

Code: Select all

$ magick toes.png +level 25,75% out.png
It can be used with "%[fx:]" expressions, for example:

Code: Select all

$ magick toes.png +level "%[fx:50/255*100],%[fx:200/255*100]%" -format "%[fx:minima*255]\n%[fx:maxima*255]\n" info:
49.9999
200
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: Greyscale shift or Interpolation

Post by mjamal »

Here what we are trying to achieve is to convert all the pixels from an image below color value 50 to 50 and the pixel with color value above 200 to 200. For example, is a pixel has color value 30, it should be automatically converted to 50. Similarly, if a pixel has color value 250, it should be converted to 200.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Greyscale shift or Interpolation

Post by snibgo »

I showed you one method to do that. It also adjusts values that were between 50 and 200.

Perhaps you want those values unchanged. In that case, "-evaluate Max 20%" will change any values that were less than 20% to be 20%, without changing other values.

Similarly "-evaluate Min 80%" will change values that are above 80% to be 80%.

As above, you can use %[fx:] expressions.
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: Greyscale shift or Interpolation

Post by mjamal »

Hello Snibgo,

Thank you for your reply.
Can you please provide any sample command of ImageMagick for the pixel range need to change which is less then 20 & greater then 200?
I need the IM command and will check at my end with the different range of pixels.

Thanks in advanced.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Greyscale shift or Interpolation

Post by snibgo »

Code: Select all

magick in.png -evaluate Max %[fx:20/255*100]% -evaluate Min %[fx:200/255*100]% out.png
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: Greyscale shift or Interpolation

Post by mjamal »

Thanks Snibgo.

One point I need to confirm in above sample command.
The value 20 and 200 are the pixel color values that will be matching in an image to replace with less & greater values respectively.
Also I did checked with the PNG transparent image and found that the effect is also applying on transparent area of an image. Please see the source and output image and below is the command which I have used.

Command -
magick finalOP-20190219143847.png -evaluate Max %[fx:50/255*100]% -evaluate Min %[fx:180/255*100]% out25_1.png

Source Image -
https://www.dropbox.com/s/j98tcf3ifd5sp ... 7.png?dl=0

Output Image -
https://www.dropbox.com/s/na8l66yremg90 ... 1.png?dl=0

Please review at your end and provide your feedback.
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: Greyscale shift or Interpolation

Post by mjamal »

Hello Everyone,

Can I have any further updates for my above query on transparent image? Please provide your valuable feedback ASAP.

Thanks in advanced
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Greyscale shift or Interpolation

Post by snibgo »

You can restrict the operation to just the RGB channels:

Code: Select all

magick finalOP-20190219143847.png -channel RGB -evaluate Max %[fx:50/255*100]% -evaluate Min %[fx:180/255*100]% +channel out25_1.png
I get a warning about your input file:

Code: Select all

magick: invalid profile length `finalOP-20190219143847.png' @ warning/png.c/MagickPNGWarningHandler/1744.
snibgo's IM pages: im.snibgo.com
Post Reply