All pixels betwen 0 and 1 to be Gray(1) in 16bit image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
rullbandspelare
Posts: 8
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

All pixels betwen 0 and 1 to be Gray(1) in 16bit image

Post by rullbandspelare »

Hi!
I would like to change all pixels below 1 and above 0 to be Gray(1) in 16bit .png with gray pixels.
Thanks for any input on how to do this!! :-)


Running : ImageMagick-7.0.9-25-portable-Q16-x64
Now pixels like ( (23,23,23) #001700170017 gray(0.0894942)) is shown as rgb (0,0,0) in, for example IrfanView
and ( (305,305,305) #013101310131 gray(1.18677) ) is shown as rgb(1,1,1)

Code: Select all

C:\ImageMagick-7.0.9-25-portable-Q16-x64\identify -verbose Gray16.png
Image: im_Gray16.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 365x128+0+0
  Units: Undefined
  Colorspace: Gray
  Type: Grayscale
  Base type: Undefined
  Endianess: Undefined
  Depth: 16-bit
  Channel depth:
    Gray: 16-bit
  Channel statistics:
    Pixels: 46720
    Gray:
      min: 0  (0)
      max: 34917 (0.532799)
      mean: 192.599 (0.00293887)
      standard deviation: 2306.39 (0.0351933)
      kurtosis: 179.946
      skewness: 13.2558
      entropy: 0.0394559
  Colors: 257
  Histogram:
     45594: (0,0,0) #000000000000 gray(0)
        64: (10,10,10) #000A000A000A gray(0.0389105)
         6: (16,16,16) #001000100010 gray(0.0622568)
       216: (23,23,23) #001700170017 gray(0.0894942)
         1: (305,305,305) #013101310131 gray(1.18677)
        32: (317,317,317) #013D013D013D gray(1.23346)
...

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: All pixels betwen 0 and 1 to be Gray(1) in 16bit image

Post by snibgo »

rullbandspelare wrote:... all pixels below 1 and above 0 ...
I assume you mean on a scale between 0 and 255.

Code: Select all

magick in.png -evaluate Max %[fx:QuantumRange*1/255] out.png
If using Windows BAT scripts, double the percent %.
snibgo's IM pages: im.snibgo.com

rullbandspelare
Posts: 8
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

Re: All pixels betwen 0 and 1 to be Gray(1) in 16bit image

Post by rullbandspelare »

Thanks a million for that!
Not quite sure on what it does. But it works ...almost
Problem is that background is pixelvalue 0, wich should remain 0.

I.e
This pixel should remain Gray(0) : (0,0,0) #000000000000 gray(0)
This pixel should become Gray(1) : (10,10,10) #000A000A000A gray(0.0389105)

So, how to exclude 0 values.

Thanks!

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: All pixels betwen 0 and 1 to be Gray(1) in 16bit image

Post by snibgo »

Sorry, yes. We can mask the operation so it doesn't happen where the input is black. The mask image mpr:MSK is black where we want the operation, and white where we want no change.

Windows BAT syntax:

Code: Select all

magick ^
  in.png ^
  ( +clone ^
    -fill White +opaque Black ^
    -negate ^
    +write mpr:MSK ^
    +delete ^
  ) ^
  -write-mask mpr:MSK ^
  -evaluate Max %%[fx:QuantumRange*1/255] ^
  +write-mask ^
  out.png
An alternative is with "-fx":

Code: Select all

magick ^
  in.png ^
  -fx "u>0&&u<1/255?1/255:u" ^
  out.png
This code is simpler, but slower when the image is large.

EDIT: Corrected "100" to "1".
snibgo's IM pages: im.snibgo.com

rullbandspelare
Posts: 8
Joined: 2017-11-23T00:45:38-07:00
Authentication code: 1152

Re: All pixels betwen 0 and 1 to be Gray(1) in 16bit image

Post by rullbandspelare »

wow. Cool!
It works . And even better if I change 100 to 1
-fx "u>0&&u<1/255?1/255:u"

Thanks for the help. I would not have figured that one out by my self.



Clarke's Third Law: Any sufficiently advanced technology is indistinguishable from magic

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: All pixels betwen 0 and 1 to be Gray(1) in 16bit image

Post by snibgo »

That's it. Sorry about the "100", I had that for testing. Now corrected above.
snibgo's IM pages: im.snibgo.com

Locked