-4

It is a black-and-white image. Its palette consist from only two colors.

Black-and-white image

When I try to threshold this image in ImageJ (Fiji), it cann't do this because of the image has only two colors. (I don't know why it cann't do this.)

So, the questions is: how to increase the number of image colors (number of colors in image palette) automatically using ImageMagick?

Update 1

One of the posible solution is to compose the image with a grayscale box which includes 256 colors. But is there another solution?

Update 2

The color palette of the image contains only two colors. The question is how to increase the size of the color palette (adding< for example, random colors)?

The palette of the image contains only two colors

Update 3

This is a Threshold dialog of ImageJ.

The thresholding dialog of ImageJ

Ivan Z
  • 1,517
  • 1
  • 16
  • 25
  • 1
    Thresholding involves choosing a value, called the *"threshold"*, and assigning all values above that threshold to 1 and all values equal or below the threshold to 0. Given that all the values in your binary image are already either 0 or 1, what value do you envisage using as a threshold? – Mark Setchell Apr 09 '23 at 10:43
  • I understand what is threshold and how to choose its value. But when I try to apply thresholding to this image in ImageJ, it cann't do thresholding (I do not know why). That is why I want to increase the size of image palette. If I change a colorspace to gray using ImageMagick, the size of pallette still the same - 2 colors. – Ivan Z Apr 09 '23 at 10:47
  • 3
    Ok. What threshold value do you want to use? Remember all your pixels are currently zero or 1, so if you choose 0, or 0.1, or 0.2, or 0.5, or 0.99 you will image will be unchanged, and if you choose 1 it will be all black. The only possible outcomes from thresholding a binary image are 1) all black, 2) the original image or 3) all white. – Mark Setchell Apr 09 '23 at 11:09
  • Is it possible to increase the number of colors or the size of image palette (to 16 or 256 colors) using IM? The comand `-colorspace gray` does not do that. – Ivan Z Apr 09 '23 at 12:57
  • no, impossible. I hope the previous explanations you received, including my disappeared one mentioning "the information isn't there", will explain the reason for this fact. – Christoph Rackwitz Apr 09 '23 at 12:59
  • I try to make thresholding in the other porgram, in ImageJ. IJ do not allow to do thresholding of binary images. Only images with 16, or 256, or bigger number of colors (I mean the color palette associated with images). Because of that I'd like to increase the palette size of b/w image. – Ivan Z Apr 09 '23 at 13:00
  • Yes, and want to know how to include more colors in image palette without touching information content. Can you explain this? – Ivan Z Apr 09 '23 at 13:13
  • —1— Please tell us which are the values your image is showing. Are they 0 and 255 or 0 and 1. —2— How do you determine that the image is strictly binary-valued? (The sample image above is a color image.) – Herbie Apr 09 '23 at 13:32
  • The image is 0 and 1. I've determined this loading image in a graphic editor and look at the number of colors. – Ivan Z Apr 09 '23 at 13:41
  • Actually, I don't encounter any problems when trying to threshold a binary image in ImageJ. Please tell us which kind of warning you get, if you try it. – Herbie Apr 09 '23 at 14:50
  • unfortunately, someone keeps disappearing my comments arbitrarily, perhaps due to wrong flags. I really wish that came with an explanation. – Christoph Rackwitz Apr 09 '23 at 16:15
  • 1
    Same here! (I would say the wrongly posed topic isn't worth any further efforts.) – Herbie Apr 09 '23 at 17:05

3 Answers3

2

You can put arbitrary entries into a PNG image palette, but it won't help you for all the reasons you have been given in the comments.

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open your image and ensure it is in Palette mode
im = Image.open('P4mFP.png').convert('P')

# Check first 2 RGB palette entries are black and white
print(im.getpalette()[:6])            # prints [0, 0, 0, 255, 255, 255]

# Stuff in new palette with 2 original values plus 254 other, random RGB values
im.putpalette([0, 0, 0, 255, 255, 255] + list(np.random.randint(0,256,254*3)))

# Save the new image
im.save('result.png')

enter image description here

Check the palette with ImageMagick:

identify -verbose result.png                                
Image:
  Filename: result.png
  Permissions: rw-r--r--
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 1920x1080+0+0
  Units: Undefined
  Colorspace: sRGB
  Type: Bilevel
  Base type: Palette
  Endianness: Undefined
  Depth: 8-bit
  ...
  ...
  Colors: 2
  Histogram:
       2005891: (0,0,0) #000000 black
         67709: (255,255,255) #FFFFFF white
  Colormap entries: 256
  Colormap:
    0: (0,0,0,1) #000000FF black
    1: (255,255,255,1) #FFFFFFFF white
    2: (102,218,69,1) #66DA45FF srgba(102,218,69,1)
    3: (76,252,109,1) #4CFC6DFF srgba(76,252,109,1)
    4: (141,189,112,1) #8DBD70FF srgba(141,189,112,1)
    5: (98,185,56,1) #62B938FF srgba(98,185,56,1)
    ...
    ...
    252: (44,1,152,1) #2C0198FF srgba(44,1,152,1)
    253: (244,134,106,1) #F4866AFF srgba(244,134,106,1)
    254: (254,129,16,1) #FE8110FF srgba(254,129,16,1)
    255: (41,166,211,1) #29A6D3FF srgba(41,166,211,1)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

I try different command-line options of ImageMagick and it looks that there is no simple command that can increase the number of colors used in image palette.

The -colors option and the -depth option doesn't help. The palette size stays the same.

So the only way I've found is to compose image with thin grayscale line that contains 256 levels (colors) of gray. It is the example of such line.

enter image description here

The following ImageMagick command composes this line with the original image and this command can be automatized.

magick bw-image.png gray-line.png -compose plus -composite composition.png

The palette of the composite image contains 256 colors nad the added line does not interfere with further image processing.

Image composed with gray line

Ivan Z
  • 1,517
  • 1
  • 16
  • 25
  • 1
    Again, why should such detours be necessary. Try to learn how to use ImageJ and all will turn out being quite easy … – Herbie Apr 09 '23 at 17:00
  • You can increase the number of colours by adding random noise, but I fail to see how that might help. – Mark Setchell Apr 09 '23 at 19:08
-1

Here is a binary test image showing values 0 and 1 (see histogram to the right) that can be thresholded by ImageJ (see below). enter image description here

Herbie
  • 143
  • 5
  • The pallette of your image contains 256 color and the image uses only two of them: black and white. Because of that IJ can threshold it. The palette of my image contains only two colors (see question update), because of that IJ cann't threshold it. – Ivan Z Apr 09 '23 at 15:42
  • That's why I was asking what kind of format your image file has and how you open your image in ImageJ. ImageJ always displays binary images as 8bit images. My binary test image is definitely not a color image. The red part only shows the result of the thresholding (it is an overlay). – Herbie Apr 09 '23 at 15:45
  • The image in PNG format. I'd added a screenshort of IJ thresholding menu. – Ivan Z Apr 09 '23 at 15:47
  • Check "Dark background", then press the "Auto"-button (you may also select "Red" as indicator). Please consult and study the ImageJ-UserGuide . – Herbie Apr 09 '23 at 15:52
  • Download image from here and try to work with it in IJ. https://disk.yandex.ru/i/DyUgG90HhiYaaw – Ivan Z Apr 09 '23 at 16:00
  • Thanks! Open the image in ImageJ, then save it in TIF-format and open it again. The white parts now show value 255. You should now be able to apply a threshold (although I don't know why you should do that). – Herbie Apr 09 '23 at 16:06
  • There are some thousand of such binary images with two-colors-palette. I need a script that can do this automatically. – Ivan Z Apr 09 '23 at 16:08
  • 1
    So why not write a simple ImageJ-macro? But more importantly, you should explain, why you want to threshold binary images. – Herbie Apr 09 '23 at 16:09
  • Because finally these images are converted into multylayered TIFF (using IM) and than processed by IJ plugin that cann't correctly work with binary images. – Ivan Z Apr 09 '23 at 16:34
  • If these "multylayered TIFFs" are in fact stacks, you could do so in ImageJ as well, no matter if the initial images are binary or not. I don't understand the role of thresholding in your processing. You may need the images in a format that ImageJ accepts but I see nor reason for thresholding. – Herbie Apr 09 '23 at 16:38
  • Multylayered TIFFs should be made automatically using script. Can we automatically run IJ script (or plugin) from batch-file? If yes, it is excelent. – Ivan Z Apr 09 '23 at 16:43
  • On the basis of the provided insights this should easyily be possible with an ImageJ-macro. I strongly recommend to study ImageJ, the macro language and the many documents that are available for most aspects of this fabulous piece of software. – Herbie Apr 09 '23 at 16:50
  • ImageJ opens the binary PNG-image directly and without problems when using the Bio-Formats Importer. (Thresholding is possible but doesn't make sense.) – Herbie Apr 10 '23 at 08:34