Reading a 8bit Tif image from ArcMAP doesnt read well

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading a 8bit Tif image from ArcMAP doesnt read well

German Ocampo
Good morning

Im reading an 8bit image generated by ArcMap using PIL in windows, the
image load well without error messages but, when I try to extract the
values to a numpy array I got some lines of the image with value of
pixel 127 (greyscale) or rgb(0,0,0). When I open the image using GIMP
or arcmap  I could see that the image is complete.

Any idea of what is happening??

the code that I'm using for read the image is:

     from PIL import Image
     from PIL import TiffImagePlugin
    import numpy as np
    import sys
    im = Image.open(file_tif)
    values=np.zeros((size_y,size_x))
    mode=im.mode
    temp=list(im.getdata())
    cont=-1
    list_colors=[]
    for i in range(0,size_y):
        for j in range(0,size_x):
            cont+=1
            if mode=="RGB":
                color=temp[cont]
                print cont,temp[cont]
                if color in list_colors:
                    index=list_colors.index(color)
                    values[i][j]=index+1
                else:
                    list_colors.append(color)
                    values[i][j]=len(list_colors)
            else:
                values[i][j]=temp[cont]

Best regards

German
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Reading a 8bit Tif image from ArcMAP doesnt read well

Chris Mitchell-2
Why would the image go between greyscale and rgb?  In anycase, perhaps
the easiest solution is use np.resize on temp instead of the for
loops.  Then if you have tuples of rgb versus ints you can use
np.where()

On Thu, Oct 28, 2010 at 4:36 AM, German Ocampo <[hidden email]> wrote:

> Good morning
>
> Im reading an 8bit image generated by ArcMap using PIL in windows, the
> image load well without error messages but, when I try to extract the
> values to a numpy array I got some lines of the image with value of
> pixel 127 (greyscale) or rgb(0,0,0). When I open the image using GIMP
> or arcmap  I could see that the image is complete.
>
> Any idea of what is happening??
>
> the code that I'm using for read the image is:
>
>     from PIL import Image
>     from PIL import TiffImagePlugin
>    import numpy as np
>    import sys
>    im = Image.open(file_tif)
>    values=np.zeros((size_y,size_x))
>    mode=im.mode
>    temp=list(im.getdata())
>    cont=-1
>    list_colors=[]
>    for i in range(0,size_y):
>        for j in range(0,size_x):
>            cont+=1
>            if mode=="RGB":
>                color=temp[cont]
>                print cont,temp[cont]
>                if color in list_colors:
>                    index=list_colors.index(color)
>                    values[i][j]=index+1
>                else:
>                    list_colors.append(color)
>                    values[i][j]=len(list_colors)
>            else:
>                values[i][j]=temp[cont]
>
> Best regards
>
> German
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Reading a 8bit Tif image from ArcMAP doesnt read well

German Ocampo
Chris

Many thanks for your email.I want to read values from images in
grayscale or rgb, because the product that is coming from Arcmap could
be either.

The issue is that using PIL I get pixel values in the greyscale of 127
or rgb(0,0,0), where really I could see in Gimp that these pixels have
a different value. Looks like PIL can decode part of the picture and
another part of the picture not.

Also I tried today to read the pixel values of the picture using
temp=np.array(im) but still I got  some cells with a value of 127
where really the image has different values.

The only way to decode the picture was using freeImagepy, opening the
image with freeImagepy, saving and then open again with PIL to get the
values. It works but not in the way that supose to be. I want to do
all with PIL :-)
 (Attached is the code)

Many thanks

German

    from PIL import Image
    from PIL import TiffImagePlugin
    import numpy as np
    import sys
    import FreeImagePy
    FIPY = FreeImagePy.freeimage()
    image = FIPY.genericLoader(file_tif)
    FIPY.Save(FreeImagePy.FIF_TIFF, image, "temp.tif")

   im = Image.open("temp.tif")
   values=np.zeros((size_y,size_x))
   mode=im.mode
   temp=list(im.getdata())
   cont=-1
   list_colors=[]
   for i in range(0,size_y):
       for j in range(0,size_x):
           cont+=1
           if mode=="RGB":
               color=temp[cont]
               print cont,temp[cont]
               if color in list_colors:
                   index=list_colors.index(color)
                   values[i][j]=index+1
               else:
                   list_colors.append(color)
                   values[i][j]=len(list_colors)
           else:
               values[i][j]=temp[cont]

On Thu, Oct 28, 2010 at 4:26 PM, Chris Mitchell <[hidden email]> wrote:

> Why would the image go between greyscale and rgb?  In anycase, perhaps
> the easiest solution is use np.resize on temp instead of the for
> loops.  Then if you have tuples of rgb versus ints you can use
> np.where()
>
> On Thu, Oct 28, 2010 at 4:36 AM, German Ocampo <[hidden email]> wrote:
>> Good morning
>>
>> Im reading an 8bit image generated by ArcMap using PIL in windows, the
>> image load well without error messages but, when I try to extract the
>> values to a numpy array I got some lines of the image with value of
>> pixel 127 (greyscale) or rgb(0,0,0). When I open the image using GIMP
>> or arcmap  I could see that the image is complete.
>>
>> Any idea of what is happening??
>>
>> the code that I'm using for read the image is:
>>
>>     from PIL import Image
>>     from PIL import TiffImagePlugin
>>    import numpy as np
>>    import sys
>>    im = Image.open(file_tif)
>>    values=np.zeros((size_y,size_x))
>>    mode=im.mode
>>    temp=list(im.getdata())
>>    cont=-1
>>    list_colors=[]
>>    for i in range(0,size_y):
>>        for j in range(0,size_x):
>>            cont+=1
>>            if mode=="RGB":
>>                color=temp[cont]
>>                print cont,temp[cont]
>>                if color in list_colors:
>>                    index=list_colors.index(color)
>>                    values[i][j]=index+1
>>                else:
>>                    list_colors.append(color)
>>                    values[i][j]=len(list_colors)
>>            else:
>>                values[i][j]=temp[cont]
>>
>> Best regards
>>
>> German
>> _______________________________________________
>> Image-SIG maillist  -  [hidden email]
>> http://mail.python.org/mailman/listinfo/image-sig
>>
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Reading a 8bit Tif image from ArcMAP doesnt read well

Chris Mitchell-2
Check the inverse of the numbers, there are a ton of inconsistencies
of x,y and matrix representation.  Basically an image's x axis
progresses left to right, whereas a matrix's x is top to bottom (x,y
versus row,column).  So check 2,3 and 3,2 on GIMP, etc.

On Thu, Oct 28, 2010 at 12:56 PM, German Ocampo <[hidden email]> wrote:

> Chris
>
> Many thanks for your email.I want to read values from images in
> grayscale or rgb, because the product that is coming from Arcmap could
> be either.
>
> The issue is that using PIL I get pixel values in the greyscale of 127
> or rgb(0,0,0), where really I could see in Gimp that these pixels have
> a different value. Looks like PIL can decode part of the picture and
> another part of the picture not.
>
> Also I tried today to read the pixel values of the picture using
> temp=np.array(im) but still I got  some cells with a value of 127
> where really the image has different values.
>
> The only way to decode the picture was using freeImagepy, opening the
> image with freeImagepy, saving and then open again with PIL to get the
> values. It works but not in the way that supose to be. I want to do
> all with PIL :-)
>  (Attached is the code)
>
> Many thanks
>
> German
>
>    from PIL import Image
>    from PIL import TiffImagePlugin
>    import numpy as np
>    import sys
>    import FreeImagePy
>    FIPY = FreeImagePy.freeimage()
>    image = FIPY.genericLoader(file_tif)
>    FIPY.Save(FreeImagePy.FIF_TIFF, image, "temp.tif")
>
>   im = Image.open("temp.tif")
>   values=np.zeros((size_y,size_x))
>   mode=im.mode
>   temp=list(im.getdata())
>   cont=-1
>   list_colors=[]
>   for i in range(0,size_y):
>       for j in range(0,size_x):
>           cont+=1
>           if mode=="RGB":
>               color=temp[cont]
>               print cont,temp[cont]
>               if color in list_colors:
>                   index=list_colors.index(color)
>                   values[i][j]=index+1
>               else:
>                   list_colors.append(color)
>                   values[i][j]=len(list_colors)
>           else:
>               values[i][j]=temp[cont]
>
> On Thu, Oct 28, 2010 at 4:26 PM, Chris Mitchell <[hidden email]> wrote:
>> Why would the image go between greyscale and rgb?  In anycase, perhaps
>> the easiest solution is use np.resize on temp instead of the for
>> loops.  Then if you have tuples of rgb versus ints you can use
>> np.where()
>>
>> On Thu, Oct 28, 2010 at 4:36 AM, German Ocampo <[hidden email]> wrote:
>>> Good morning
>>>
>>> Im reading an 8bit image generated by ArcMap using PIL in windows, the
>>> image load well without error messages but, when I try to extract the
>>> values to a numpy array I got some lines of the image with value of
>>> pixel 127 (greyscale) or rgb(0,0,0). When I open the image using GIMP
>>> or arcmap  I could see that the image is complete.
>>>
>>> Any idea of what is happening??
>>>
>>> the code that I'm using for read the image is:
>>>
>>>     from PIL import Image
>>>     from PIL import TiffImagePlugin
>>>    import numpy as np
>>>    import sys
>>>    im = Image.open(file_tif)
>>>    values=np.zeros((size_y,size_x))
>>>    mode=im.mode
>>>    temp=list(im.getdata())
>>>    cont=-1
>>>    list_colors=[]
>>>    for i in range(0,size_y):
>>>        for j in range(0,size_x):
>>>            cont+=1
>>>            if mode=="RGB":
>>>                color=temp[cont]
>>>                print cont,temp[cont]
>>>                if color in list_colors:
>>>                    index=list_colors.index(color)
>>>                    values[i][j]=index+1
>>>                else:
>>>                    list_colors.append(color)
>>>                    values[i][j]=len(list_colors)
>>>            else:
>>>                values[i][j]=temp[cont]
>>>
>>> Best regards
>>>
>>> German
>>> _______________________________________________
>>> Image-SIG maillist  -  [hidden email]
>>> http://mail.python.org/mailman/listinfo/image-sig
>>>
>>
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Reading a 8bit Tif image from ArcMAP doesnt read well

Fredrik Lundh
In reply to this post by German Ocampo
On Thu, Oct 28, 2010 at 6:56 PM, German Ocampo <[hidden email]> wrote:

> The issue is that using PIL I get pixel values in the greyscale of 127
> or rgb(0,0,0), where really I could see in Gimp that these pixels have
> a different value. Looks like PIL can decode part of the picture and
> another part of the picture not.

Chris already asked you about this, but I'm asking again -- what are
you doing that returns both greyscale and rgb values from the same
image?

What's the mode of this image, btw?  Maybe the confusion here is that
it's a palette image (i.e. each 8-bit pixel value is an index into a
colour table), and you're working on the index values instead of the
colour values.

Try replacing your code with

   im = Image.open(file_tif)
   im = im.convert("L") # or "RGB" if you want full color
   values = numpy.asarray(im)

and see if the resulting array makes more sense.

</F>

> Also I tried today to read the pixel values of the picture using
> temp=np.array(im) but still I got  some cells with a value of 127
> where really the image has different values.
>
> The only way to decode the picture was using freeImagepy, opening the
> image with freeImagepy, saving and then open again with PIL to get the
> values. It works but not in the way that supose to be. I want to do
> all with PIL :-)
>  (Attached is the code)
>
> Many thanks
>
> German
>
>    from PIL import Image
>    from PIL import TiffImagePlugin
>    import numpy as np
>    import sys
>    import FreeImagePy
>    FIPY = FreeImagePy.freeimage()
>    image = FIPY.genericLoader(file_tif)
>    FIPY.Save(FreeImagePy.FIF_TIFF, image, "temp.tif")
>
>   im = Image.open("temp.tif")
>   values=np.zeros((size_y,size_x))
>   mode=im.mode
>   temp=list(im.getdata())
>   cont=-1
>   list_colors=[]
>   for i in range(0,size_y):
>       for j in range(0,size_x):
>           cont+=1
>           if mode=="RGB":
>               color=temp[cont]
>               print cont,temp[cont]
>               if color in list_colors:
>                   index=list_colors.index(color)
>                   values[i][j]=index+1
>               else:
>                   list_colors.append(color)
>                   values[i][j]=len(list_colors)
>           else:
>               values[i][j]=temp[cont]
>
> On Thu, Oct 28, 2010 at 4:26 PM, Chris Mitchell <[hidden email]> wrote:
>> Why would the image go between greyscale and rgb?  In anycase, perhaps
>> the easiest solution is use np.resize on temp instead of the for
>> loops.  Then if you have tuples of rgb versus ints you can use
>> np.where()
>>
>> On Thu, Oct 28, 2010 at 4:36 AM, German Ocampo <[hidden email]> wrote:
>>> Good morning
>>>
>>> Im reading an 8bit image generated by ArcMap using PIL in windows, the
>>> image load well without error messages but, when I try to extract the
>>> values to a numpy array I got some lines of the image with value of
>>> pixel 127 (greyscale) or rgb(0,0,0). When I open the image using GIMP
>>> or arcmap  I could see that the image is complete.
>>>
>>> Any idea of what is happening??
>>>
>>> the code that I'm using for read the image is:
>>>
>>>     from PIL import Image
>>>     from PIL import TiffImagePlugin
>>>    import numpy as np
>>>    import sys
>>>    im = Image.open(file_tif)
>>>    values=np.zeros((size_y,size_x))
>>>    mode=im.mode
>>>    temp=list(im.getdata())
>>>    cont=-1
>>>    list_colors=[]
>>>    for i in range(0,size_y):
>>>        for j in range(0,size_x):
>>>            cont+=1
>>>            if mode=="RGB":
>>>                color=temp[cont]
>>>                print cont,temp[cont]
>>>                if color in list_colors:
>>>                    index=list_colors.index(color)
>>>                    values[i][j]=index+1
>>>                else:
>>>                    list_colors.append(color)
>>>                    values[i][j]=len(list_colors)
>>>            else:
>>>                values[i][j]=temp[cont]
>>>
>>> Best regards
>>>
>>> German
>>> _______________________________________________
>>> Image-SIG maillist  -  [hidden email]
>>> http://mail.python.org/mailman/listinfo/image-sig
>>>
>>
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig