Re: What does the 'F' mode mean in PIL?
Christopher Barker <[email protected]>
| Newsgroups | gmane.comp.python.image |
|---|---|
| Message-ID | <[email protected]> |
A followup, trying to do something real
It seems that the raw decoder doesn't understand all the modes listed in:
http://www.pythonware.com/library/pil/handbook/decoder.htm
indeed, hardly any of them ('F' only for the "floating point modes")
Enclosed is some sample code I've got that attempts to convert a
unsigned 16 bit grayscale image to RGB. I can't get it to do anything,
as I get:
ValueError: unknown raw mode
for any floating point mode other than 'F'
What am I missing here?
-Chirs
Christopher Barker wrote:
> Hi folks,
>
> I've been looking through the docs on File Decoders (or, in my case for
> decoding binary data with fromstring() or frombuffer()):
>
> http://www.pythonware.com/library/pil/handbook/decoder.htm
>
> With the raw decoder, most of the modes are clear to me, but the 'F'
> mode has me confused:
>
> What is it really storing, working with?
> - one floating point value per pixel?
> - one floating point value per colorband?
> - but then, how are they arranged?
> - Is it single precision (32 bit) floats only?
>
>
> What does it mean if you use one of the modified 'F' modes, like
> "F;16N"? I know what an 16 bit native unsigned integer is, but I don't
> understand:
>
> - What happens if I do fromstring() with a mode f "F;16N" ?
> - is the data converted to a float (32bit)?
> - if so, do you just lose information if you have higher precision
> types being passed in?
> - is the raw data stored in 16 bit (two byte) chunks?
>
>
> - How is 'F' mode interpreted if you convert to, say 'RGB' mode?
> -grayscale?
> - something else?
>
>
> Any enlightenment would be great.
>
> -Chris
>
>
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
[email protected]
_______________________________________________
Image-SIG maillist - [email protected]
http://mail.python.org/mailman/listinfo/image-sig
PIL_u16.py
(application/x-python, 812 B)
#!/usr/bin/env python
from PIL import Image
import numpy as np
width, height = (20, 30)
max_value = np.iinfo(np.uint16).max
raw_data = np.random.randint(0, max_value, (width, height)).astype(np.uint16)
def rgb_from_int16_PIL(arr):
"""
converts a 2-d array of uint16 grayscale values to a 24 bit RGB array,
using PIL
"""
#i = Image.fromstring("F;8", (width, height), arr)
i = Image.fromstring('RGB', # mode
(width, height), # size
arr.tostring(), # data
"raw",
'F;16', # raw mode
# stride
# orientation
)
return np.fromstring(i.tostring(), dtype=uint8).reshape((width, height, 3))
rgb_data = rgb_from_int16_PIL(raw_data)