4 # Copyright (C) 2006-2007 Antonio Ingargiola <tritemio@gmail.com>
6 # This file is part of Image Analyzer.
8 # Image Analyzer is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
15 from numpy
import fromstring
, resize
, array
, around
, arange
, zeros
17 class WrongImageFormat(Exception):
18 """ Exception for image format not known. """
19 def __init__(self
, value
):
22 return repr(self
.value
)
24 def image2array(image
):
25 """ Converts an image object (PIL) to an array (NumPy). """
26 if image
.mode
== "RGB":
27 dimensions
= (image
.size
[1], image
.size
[0], 3)
29 elif image
.mode
== "RGBA":
30 dimensions
= (image
.size
[1], image
.size
[0], 4)
32 elif image
.mode
== "L":
33 dimensions
= (image
.size
[1], image
.size
[0])
35 elif image
.mode
== "I;16":
36 dimensions
= (image
.size
[1], image
.size
[0])
39 raise WrongImageFormat
, "Image type "+image
.mode
+" unknown."
41 image_string
= image
.tostring()
42 image_array
= fromstring(image_string
, type)
43 return resize(image_array
, dimensions
)
45 def image2array2(image
):
46 """Converts an image object (PIL) into an array (NumPy).
47 Seems slightly slower than image2array()."""
48 if image
.mode
== "RGB":
49 dimensions
= (image
.size
[1], image
.size
[0], 3)
51 elif image
.mode
== "RGBA":
52 dimensions
= (image
.size
[1], image
.size
[0], 4)
54 elif image
.mode
== "L":
55 dimensions
= (image
.size
[1], image
.size
[0])
57 elif image
.mode
== "I;16":
58 dimensions
= (image
.size
[1], image
.size
[0])
61 raise WrongImageFormat
, "Image type "+image
.mode
+" unknown."
63 image_array
= array(image
.getdata(), dtype
='UInt16')
64 return resize(image_array
, dimensions
)
66 def histogram3(image
, nbit
):
68 It is used instead of image.histogram() because the latter does not work
69 with high dynamic images (for example 14 bit).
71 Takes as parameters a PIL image and the number of bit and returns an array
72 2**nbit length containing the image histogram.
74 This function has been written by Fredrik Lundh, in the article:
76 http://effbot.org/zone/image-histogram-optimization.htm
78 This code snipped is public domain as stated at:
80 http://effbot.org/zone/copyright.htm
83 # wait, use a list, but get rid of that getpixel call
84 result
= [0] * 2**nbit
85 for v
in image
.getdata():
86 result
[v
] = result
[v
] + 1
89 def histogramfloat(farray
, nbit
=10, rescale
=True):
91 Calculates (quite) efficiently the histogram of a float array.
93 Returns two arrays of the same length:
94 - an equally spaced array of (quantized) values
95 - the occurrence of each value
97 The optional 'nbit' parameter set the quantization number of bit.
98 The optional 'rescale' flag can inhibit data rescaling. This is useful if
99 your data comes from a source with a known number of bit (for example an
100 image converted to array). In this case you must set the correct 'nbit'
101 and assure that the 'farray' data is contained in the range 0..(2^nbit)-1
110 rarray
= around(farray
*((2.**nbit
)-1)/m
).astype(int)
111 values
= arange(2**nbit
)*m
/(2.**nbit
-1)
113 hist_data
= zeros(2**nbit
)
114 for v
in rarray
.ravel():
116 return values
, hist_data
121 files
= ['test-img.tif']
123 for file_name
in files
:
125 image
= Image
.open(file_name
)
127 print "\n File '"+file_name
+"' cannot be opened.\n"
130 profile
.run('ia = image2array(image)', 'ia')
131 profile
.run('ia2 = image2array2(image)', 'ia2')
133 if __name__
== "__main__":
135 from scipy
import lena
139 v
, h
= histogramfloat(a
, nbit
=8, rescale
=False)
146 bar(v
,h
, width
=step
, bottom
=0.01, edgecolor
='blue')