Module calmagick :: Class PNMImage
[hide private]
[frames] | no frames]

Class PNMImage

source code

object --+
         |
        PNMImage

class to represent an PNM grayscale image given in P2 format

Instance Methods [hide private]
 
__init__(self, strlist)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
int
_rsum(self, y, x)
running sum with cache
source code
float
block_avg(self, x, y, szx, szy)
returns the average intensity of a block of size (szx,szy) at pos (top-left) (x,y)
source code
(float,(float,float),(int,int),(int,int))
lowest_block_avg(self, szx, szy, at_least=0)
returns the (szx,szy)-sized block with intensity as close to at_least as possible
source code
(float,(float,float),(int,int),(int,int),float)
fit_rect(self, size_range=(0.333, 0.8), at_least=7, relax=0.2, rr=1.0)
find the maximal-area minimal-entropy rectangle within the image
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
  _rsum_cache
used by _rsum() to remember results
  data
image data as 2-dimensional array (list of lists)
  maxval
maximum grayscale value
  size
tuple (width,height) of image dimensions
  xsum
2-dimensional array of running x-sums for each line, used for efficient computation of block averages, resulting in O(H) complexity, instead of O(W*H), where W,H the image dimensions
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, strlist)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

lowest_block_avg(self, szx, szy, at_least=0)

source code 

returns the (szx,szy)-sized block with intensity as close to at_least as possible

Returns: (float,(float,float),(int,int),(int,int))
R=tuple M({avg, (szx_ratio,szy_ratio), (x,y), (szx,szy))}: R[0] is the average intensity of the block found, R[1] is the block size ratio with respect to the whole image, R[2] is the block position (top-left) and R[3] is the block size

fit_rect(self, size_range=(0.333, 0.8), at_least=7, relax=0.2, rr=1.0)

source code 

find the maximal-area minimal-entropy rectangle within the image

Parameters:
  • size_range - tuple of smallest and largest rect/photo size ratio

    size measured on the 'best-fit' dimension' if rectangle and photo ratios differ

  • at_least - early stop of minimization algorithm, when rect of this amount of entropy is found
  • relax - relax minimum entropy by a factor of (1+relax), so that bigger sizes can be tried

    This is because usuallly minimal entropy is achieved at a minimal-area box.

  • rr - ratio of ratios

    Calendar rectangle ratio over Photo ratio. If r>1 then calendar rectangle, when scaled, fits x dimension first. Conversely, if r<1, scaling touches the y dimension first. When r=1, calendar rectangle can fit perfectly within the photo at 100% size.

Returns: (float,(float,float),(int,int),(int,int),float)