1

Objective: To save image in bi-level format as demonstrated at https://memorynotfound.com/convert-image-black-white-java/

Code:

using Images, ImageView;

function save_as_binary_image(img_path::String, threshold::Float16)
    img_binary = load(img_path);
    img_binary = (Gray.(img_binary) .> threshold);
    imshow(img_binary);
    typeof(img_binary);#=>BitArray{2}
    save("/path/to/dest/image.png", img_binary);
    img_saved = load("/path/to/dest/image.png");
    imshow(img_saved);
    typeof(img_saved);#=>Array{Gray{Normed{UInt8,8}},2}
end

save_as_binary_image("/path/to/image/file", convert(Float16, 0.5));

It saves as image of depth 8 but not of depth 1.

Please guide me in saving bi-level image to file!

AVA
  • 2,474
  • 2
  • 26
  • 41

1 Answers1

1

I'm not an Images.jl user yet (soon perhaps) but here's something that works:

using Images, ImageView

function save_binary_image(img_path, threshold)
    img_binary = load(img_path)
    @info size(img_binary)
    tib = Gray.(Gray.(img_binary) .> threshold)
    save("$(img_path)-$(threshold).png", tib)
end

save_binary_image("/tmp/mandrill.png", 0.1)

Perhaps you can slowly modify this to do what you want...

It can be useful to work at the REPL, so that you can see the errors immediately.

daycaster
  • 2,655
  • 2
  • 15
  • 18
  • As, "...tib = Gray.(Gray.(img_binary) .> threshold)...", it seems to be in grayscale format! How to verify the image is in binary(1bit/pixel) format or grayscale(1byte/pixel) format? – AVA Jul 18 '19 at 16:18
  • 1
    `repr(tib[1:50])` shows `"Gray{Bool}[Gray{Bool}(false)...`. So it's an array of Boolean Grays... :) – daycaster Jul 19 '19 at 07:01
  • type is not preserved while saving to file. ie. type of loaded saved image is of type Array{Gray{Normed{UInt8,8}},2} but not of type Array{{Bool},2}. – AVA Jul 19 '19 at 17:56