1
votes

I need to read an image, manipulate it a bit and then save it as image again. To do that, I've found the excellent Images.jl package in julia. I was able to read image, convert it to Floating point array and then manipulate it (cropping image and changing some values on the image). However, I could not find a way to store it as a jpg. file again. Here is the process I apply to manipulate the data. For the code below, let's assume I have an dog.jpg file in the same directory.

Using Images,Colors
averageImage = zeros(1,1,3)
averageImage[1,1,:] = [123.68 116.779 103.779]
function data(img, averageImage)
           a0 = load(img)
           new_size = ntuple(i->div(size(a0,i)*224,minimum(size(a0))),2)
           a1 = Images.imresize(a0, new_size)
           i1 = div(size(a1,1)-224,2)
           j1 = div(size(a1,2)-224,2)
           b1 = a1[i1+1:i1+224,j1+1:j1+224]
           c1 = separate(b1)
           d1 = convert(Array{Float32}, c1)
           e1 = reshape(d1[:,:,1:3], (224,224,3,1))
           f1 = (255 * e1 .- averageImage)
           g1 = permutedims(f1, [2,1,3,4])
           g1 = g1[:,:,:]
           # here type of g1 is : Array{Float64,3}
       end

A = data("dog.jpg",averageImage)

Here, I was able to get A. Now, I need to save that A array as image. To to that, I try the following :

save("modified_dog.jpg",A) 

I got the following error:

ERROR: ArgumentError: FixedPointNumbers.UFixed{UInt8,8} is an 8-bit type representing 256 values from 0.0 to 1.0; cannot represent -79.68

Unfortunatelly, I do not know to do that conversion.

Is there anyone to help me to save the mentioned A array ? Thanks in advance.

2
As the error indicates, Images takes floating point values in [0,1], not values in the range [0,255]. If you adjust your code to that effect, it should work. - StefanKarpinski

2 Answers

1
votes

I haven't looked at the bulk of your function, but at the end you could try:

result = convert(Image, map(ScaleMinMax(Float64, 0.0, 256.0), g1))
save("/tmp/test.png", result)

which might convert it.

The documentation has a mysterious section entitled MapInfo (not the GIS system) which sheds a flickering light on the subject...

0
votes

The NRRD format is a reasonable choice for floating-point images, though be aware that it doesn't have widespread support in external 2d graphics programs (it's more widely used for 3d images). If you just use a file name like "test.nrrd" it should just work.