How to save greyscale image

1,665 views
Skip to first unread message

NewPy

unread,
Jun 13, 2013, 1:22:01 AM6/13/13
to python...@googlegroups.com
# read in a grey scale image, imsave convert it to color image
s_file='Nuts.png'
img = mahotas.imread(s_file, as_grey=True)
mahotas.imsave('test.png', img)
exit()

# example 2, instead of getting a black and white image, I got red and blue strips
import numpy as np
import mahotas
img = np.ones([10,10])
img[::2,:]=0
mahotas.imsave('test.png', img) 

Thanks!

Luis Pedro Coelho

unread,
Jun 13, 2013, 5:48:57 AM6/13/13
to python...@googlegroups.com
Hello,

Are you sure that you are not just seeing the grayscale/bw image using
the default matplotlib colorschemes, which are false colour schemes?

What does it look like if you open the image on gimp or another image
software?

HTH
Luis
> --
> You received this message because you are subscribed to the Google
> Groups "pythonvision" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to pythonvision...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


--
Luis Pedro Coelho | EMBL | http://luispedro.org

NewPy

unread,
Jun 15, 2013, 12:57:32 AM6/15/13
to python...@googlegroups.com
Attached are the input and output files for the script below.  I checked with GIMP, it's colored. Thanks

#!/usr/bin/env python
import mahotas
s_file='Nuts.png'
img = mahotas.imread(s_file, as_grey=True)
mahotas.imsave('Nuts_saved.png', img)
exit()
Nuts.png
Nuts_saved.png

Luis Pedro Coelho

unread,
Jun 15, 2013, 9:21:40 AM6/15/13
to python...@googlegroups.com
Ah, you're right!

This is actually matplotlib.pyplot.imsave's implementation.

Mahotas does not implement IO, it always just exposes one of

1. imread (http://luispedro.org/software/imread)
2. freeimage
3. matplotlib

(It tries these in order 'til it finds one that works).

*

I have just committed a fix so that the imsave for matplotlib at leasts
works around this and tells the user what's happening.

Thanks
Luis

Thouis (Ray) Jones

unread,
Jun 15, 2013, 11:14:17 AM6/15/13
to python...@googlegroups.com
You might add cmap=matplotlib.cm.gray (and maybe vmin=0, vmax=1, if the incoming image is within [0,1]) to the matplotlib.imsave call, to better approximate what freeimage would do.

Ray Jones




For more options, visit https://groups.google.com/groups/opt_out.


--
Luis Pedro Coelho | EMBL | http://luispedro.org

--
You received this message because you are subscribed to the Google Groups "pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonvision+unsubscribe@googlegroups.com.

NewPy

unread,
Jun 15, 2013, 11:45:04 PM6/15/13
to python...@googlegroups.com
Thanks for the tips, it works for me now with matplotlib.

I also install FreeImage and add its .so to my LD_LIBRARY_PATH, but mahotas.imsave still tries to use matplotlib.  How do I use enable mahotas to use freeimage to save greyscale image?
Thanks!


For more options, visit https://groups.google.com/groups/opt_out.


--
Luis Pedro Coelho | EMBL | http://luispedro.org

--
You received this message because you are subscribed to the Google Groups "pythonvision" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonvision...@googlegroups.com.

Luis Pedro Coelho

unread,
Jun 16, 2013, 10:34:11 AM6/16/13
to python...@googlegroups.com
Try:

import mahotas.io.freeimage

This should give you a descriptive error message.

HTH,
Luis

On 06/16/2013 05:45 AM, NewPy wrote:
> Thanks for the tips, it works for me now with matplotlib.
>
> I also install FreeImage and add its .so to my LD_LIBRARY_PATH, but
> mahotas.imsave still tries to use matplotlib. How do I use enable
> mahotas to use freeimage to save greyscale image?
> Thanks!
>
>
> On Saturday, June 15, 2013 8:14:17 AM UTC-7, Thouis (Ray) Jones wrote:
>
> You might add cmap=matplotlib.cm.gray (and maybe vmin=0, vmax=1, if
> the incoming image is within [0,1]) to the matplotlib.imsave call,
> to better approximate what freeimage would do.
>
> Ray Jones
>
>
>
> On Sat, Jun 15, 2013 at 9:21 AM, Luis Pedro Coelho
> <lu...@luispedro.org <javascript:>> wrote:
>
> Ah, you're right!
>
> This is actually matplotlib.pyplot.imsave's implementation.
>
> Mahotas does not implement IO, it always just exposes one of
>
> 1. imread (http://luispedro.org/__software/imread
> <http://luispedro.org/software/imread>)
> 2. freeimage
> 3. matplotlib
>
> (It tries these in order 'til it finds one that works).
>
> *
>
> I have just committed a fix so that the imsave for matplotlib at
> leasts works around this and tells the user what's happening.
>
> Thanks
> Luis
>
>
> On 06/15/2013 06:57 AM, NewPy wrote:
>
> Attached are the input and output files for the script
> below. I checked
> with GIMP, it's colored. Thanks
>
> #!/usr/bin/env python
> import mahotas
> s_file='Nuts.png'
> img = mahotas.imread(s_file, as_grey=True)
> mahotas.imsave('Nuts_saved.__png', img)
> exit()
>
>
> On Wednesday, June 12, 2013 10:22:01 PM UTC-7, NewPy wrote:
>
> # read in a grey scale image, imsave convert it to
> color image
> s_file='Nuts.png'
> img = mahotas.imread(s_file, as_grey=True)
> mahotas.imsave('test.png', img)
> exit()
>
> # example 2, instead of getting a black and white
> image, I got red
> and blue strips
> import numpy as np
> import mahotas
> img = np.ones([10,10])
> img[::2,:]=0
> mahotas.imsave('test.png', img)
>
> Thanks!
>
> --
> You received this message because you are subscribed to the
> Google
> Groups "pythonvision" group.
> To unsubscribe from this group and stop receiving emails
> from it, send
> an email to pythonvision...@__googlegroups.com <javascript:>.
> For more options, visit
> https://groups.google.com/__groups/opt_out
> <https://groups.google.com/groups/opt_out>.
>
>
>
>
> --
> Luis Pedro Coelho | EMBL | http://luispedro.org
>
> --
> You received this message because you are subscribed to the
> Google Groups "pythonvision" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to pythonvision...@__googlegroups.com
> <javascript:>.
> For more options, visit
> https://groups.google.com/__groups/opt_out
> <https://groups.google.com/groups/opt_out>.
Reply all
Reply to author
Forward
0 new messages