Package pyvision :: Package types :: Module img :: Class Image
[hide private]
[frames] | no frames]

Class Image

source code

The primary purpose of the image class is to provide a structure that can transform an image back and fourth for different python libraries such as PIL, OpenCV, and Scipy Images. This class also allows some simple operations on the image such as annotation.

Note: When working with images in matrix format, they are transposed such that x = col and y = row. You can therefore still work with coords such that im[x,y] = mat[x,y]. #

Images have the following attributes:

Instance Methods [hide private]
 
__init__(self, data, bw_annotate=False)
Create an image from a file or a PIL Image, OpenCV Image, or numpy array.
source code
 
asBW(self)
Returns: a gray-scale version of this pyvision image
source code
 
asMatrix2D(self)
Returns: the gray-scale image data as a two dimensional numpy array
source code
 
asMatrix3D(self)
Returns: color image data as a 3D array with shape (3(rgb),w,h)
source code
 
asPIL(self)
Returns: image data as a pil image
source code
 
asOpenCV(self)
Returns: the image data in an OpenCV format
source code
 
asOpenCV2(self)
Returns: the image data in an OpenCV format that is a numpy array of shape (h,w,3) of uint8
source code
 
asOpenCV2BW(self)
Returns: the image data in an OpenCV format that is a numpy array of shape (h,w,1) of uint8
source code
 
asOpenCVBW(self)
Returns: the image data in an OpenCV one channel format
source code
 
asThermal(self, clip_negative=False)
Returns: a thermal colored representation of this image.
source code
 
asAnnotated(self, as_type='PIL')
Returns: the PIL image used for annotation.
source code
 
asHSV(self)
Returns: an OpenCV HSV encoded image
source code
 
asLAB(self)
Returns: an OpenCV LAB encoded image
source code
 
getExif(self, output='simple')
This function returns the exif headers for an image.
source code
 
annotateRect(self, rect, color='red', fill_color=None, alpha=1.0)
Draws a rectangle on the annotation image
source code
 
annotateImage(self, im, rect, color='red', fill_color=None)
Draws an image
source code
 
annotateThickRect(self, rect, color='red', width=5)
Draws a rectangle on the annotation image
source code
 
annotateEllipse(self, rect, color='red')
Draws an ellipse on the annotation image
source code
 
annotateLine(self, point1, point2, color='red', width=1)
Draws a line from point1 to point2 on the annotation image
source code
 
annotateLines(self, points, color='red', width=1)
Draws a line from point1 to point2 on the annotation image
source code
 
annotateMask(self, mask, color='red')
Shades the contents of a mask.
source code
 
annotatePolygon(self, points, color='red', width=1, fill=None)
Draws a line from point1 to point2 on the annotation image
source code
 
annotatePoint(self, point, color='red')
Marks a point in the annotation image using a small circle
source code
 
annotatePoints(self, points, color='red')
Marks a point in the annotation image using a small circle
source code
 
annotateCircle(self, point, radius=3, color='red', fill=None)
Marks a circle in the annotation image
source code
 
annotateArc(self, point, radius=3, startangle=0, endangle=360, color='red')
Draws a circular arc on the image.
source code
 
annotateLabel(self, point, label, color='red', mark=False, font=None, background=None)
Marks a point in the image with text
source code
 
annotateDot(self, point, color='red')
Like annotatePoint but only draws a point on the given pixel.
source code
 
valueNormalize(self)
TODO: Deprecated remove this sometime.
source code
 
getType(self)
Return the type of the image.
source code
 
normalize(self)
Equalize and normalize the image.
source code
 
equalize(self, bw=True)
Equalize the image
source code
 
_generateMatrix2D(self)
Create a matrix version of the image.
source code
 
_generateMatrix3D(self)
Create a matrix version of the image.
source code
 
_generatePIL(self)
Create a PIL version of the image
source code
 
_generateOpenCV(self)
Create a color opencv representation of the image.
source code
 
_generateOpenCV2(self)
Create a matrix version of the image compatible with OpenCV 2 (cv2) in BGR format.
source code
 
_generateOpenCV2BW(self)
Create a matrix version of the image compatible with OpenCV 2 (cv2) in BGR format.
source code
 
toBufferGray(self, depth)
Returns: the image data as a binary python string.
source code
 
toBufferRGB(self, depth)
returns the image data as a binary python string.
source code
 
toBufferRGBA(self, depth)
returns the image data as a binary python string.
source code
 
thumbnail(self, newSize)
Returns a resized version of the image that fits in new_size but preserves the aspect ratio.
source code
 
resize(self, newSize, **kwargs)
Returns a resized version of the image.
source code
 
scale(self, scale)
Returns a scaled version of the image.
source code
 
copy(self)
Returns a new pv.Image which is a copy of (only) the current image.
source code
pv.Image
crop(self, rect, size=None, interpolation=None, return_affine=False)
Crops an image to the given rectangle.
source code
 
save(self, filename, annotations=False)
Save the image to a file.
source code
 
show(self, window=None, pos=None, delay=0, size=None)
Displays the annotated version of the image using OpenCV highgui
source code
 
__repr__(self) source code
Method Details [hide private]

__init__(self, data, bw_annotate=False)
(Constructor)

source code 

Create an image from a file or a PIL Image, OpenCV Image, or numpy array.

Parameters:
  • data - this can be a numpy array, PIL image, or opencv image.
  • bw_annotate - generate a black and white image to make color annotations show up better
Returns:
an Image object instance

asBW(self)

source code 
Returns:
a gray-scale version of this pyvision image

asMatrix2D(self)

source code 
Returns:
the gray-scale image data as a two dimensional numpy array

asMatrix3D(self)

source code 
Returns:
color image data as a 3D array with shape (3(rgb),w,h)

asPIL(self)

source code 
Returns:
image data as a pil image

asOpenCV(self)

source code 
Returns:
the image data in an OpenCV format

asOpenCV2(self)

source code 
Returns:
the image data in an OpenCV format that is a numpy array of shape (h,w,3) of uint8

asOpenCV2BW(self)

source code 
Returns:
the image data in an OpenCV format that is a numpy array of shape (h,w,1) of uint8

asOpenCVBW(self)

source code 
Returns:
the image data in an OpenCV one channel format

asThermal(self, clip_negative=False)

source code 
Returns:
a thermal colored representation of this image.

asAnnotated(self, as_type='PIL')

source code 
Parameters:
  • as_type - Specify either "PIL" or "PV". If "PIL" (default) then the return type is a PIL image. If "PV", then the return type is a pyvision image, where the annotations have been 'flattened' onto the original source image.
Returns:
the PIL image used for annotation.

asHSV(self)

source code 
Returns:
an OpenCV HSV encoded image

asLAB(self)

source code 
Returns:
an OpenCV LAB encoded image

getExif(self, output='simple')

source code 

This function returns the exif headers for an image. This only works for images that have been read from disk.

Parameters:
  • output - select 'simple' or 'full'. 'full' output contains additional metadata.
Returns:
a dictionary of EXIF data.

annotateRect(self, rect, color='red', fill_color=None, alpha=1.0)

source code 

Draws a rectangle on the annotation image

Parameters:
  • rect - a rectangle of type Rect
  • color - defined as ('#rrggbb' or 'name')
  • fill_color - defined as per color, but indicates the color used to fill the rectangle. Specify None for no fill.
  • alpha - Ignored if no fill. Otherwise, this value controls how opaque the fill is. Specify 1.0 (default) for a fully opaque fill, or 0.0 for fully transparent. A value of 0.3, for example, would show a partially transparent filled rectangle over the background image.

annotateImage(self, im, rect, color='red', fill_color=None)

source code 

Draws an image

Parameters:
  • im - the image to render
  • rect - a rectangle of type Rect
  • color - defined as ('#rrggbb' or 'name')
  • fill_color - defined as per color, but indicates the color used to fill the rectangle. Specify None for no fill.

annotateThickRect(self, rect, color='red', width=5)

source code 

Draws a rectangle on the annotation image

Parameters:
  • rect - a rectangle of type Rect
  • color - defined as ('#rrggbb' or 'name')

annotateEllipse(self, rect, color='red')

source code 

Draws an ellipse on the annotation image

Parameters:
  • rect - the bounding box of the elipse of type Rect
  • color - defined as ('#rrggbb' or 'name')

annotateLine(self, point1, point2, color='red', width=1)

source code 

Draws a line from point1 to point2 on the annotation image

Parameters:
  • point1 - the starting point as type Point
  • point2 - the ending point as type Point
  • color - defined as ('#rrggbb' or 'name')

annotateLines(self, points, color='red', width=1)

source code 

Draws a line from point1 to point2 on the annotation image

Parameters:
  • point1 - the starting point as type Point
  • point2 - the ending point as type Point
  • color - defined as ('#rrggbb' or 'name')

annotateMask(self, mask, color='red')

source code 

Shades the contents of a mask.

Parameters:
  • mask - a numpy array showing the mask.
  • color - defined as ('#rrggbb' or 'name')

annotatePolygon(self, points, color='red', width=1, fill=None)

source code 

Draws a line from point1 to point2 on the annotation image

Parameters:
  • points - a list of pv points to be plotted
  • color - defined as ('#rrggbb' or 'name')
  • width - the line width

annotatePoint(self, point, color='red')

source code 

Marks a point in the annotation image using a small circle

Parameters:
  • point - the point to mark as type Point
  • color - defined as ('#rrggbb' or 'name')

annotatePoints(self, points, color='red')

source code 

Marks a point in the annotation image using a small circle

Parameters:
  • point - the point to mark as type Point
  • color - defined as ('#rrggbb' or 'name')

annotateCircle(self, point, radius=3, color='red', fill=None)

source code 

Marks a circle in the annotation image

Parameters:
  • point - the center of the circle as type Point
  • radius - the radius of the circle
  • color - defined as ('#rrggbb' or 'name')

annotateArc(self, point, radius=3, startangle=0, endangle=360, color='red')

source code 

Draws a circular arc on the image.

Parameters:
  • point - the center of the circle as type Point
  • radius - the radius of the circle
  • startangle - the starting angle of the arc segment to be drawn, in degrees
  • endangle - the ending angle in degrees. Arc will be drawn clockwise from starting angle to ending angle.
  • color - defined as ('#rrggbb' or 'name')

annotateLabel(self, point, label, color='red', mark=False, font=None, background=None)

source code 

Marks a point in the image with text

Parameters:
  • point - the point to mark as type Point
  • label - the text to use as a string
  • color - defined as ('#rrggbb' or 'name')
  • mark - of True or ['right', 'left', 'below', or 'above','centered'] then also mark the point with a small circle
  • font - An optional PIL.ImageFont font object to use. Alternatively, specify an integer and the label will use Arial font of that size. If None, then the default is used.
  • background - An optional color that will be used to draw a rectangular background underneath the text.

annotateDot(self, point, color='red')

source code 

Like annotatePoint but only draws a point on the given pixel. This is useful to avoid clutter if many points are being annotated.

Parameters:
  • point - the point to mark as type Point
  • color - defined as ('#rrggbb' or 'name')

_generateOpenCV(self)

source code 

Create a color opencv representation of the image. TODO: The OpenCV databuffer seems to be automatically swapped from RGB to BGR. This is counter intuitive.

toBufferGray(self, depth)

source code 
Parameters:
  • depth - Use 8, 32, or 64, to specify the bit depth of the pixels.
Returns:
the image data as a binary python string.

toBufferRGBA(self, depth)

source code 

returns the image data as a binary python string. TODO: Not yet implemented

thumbnail(self, newSize)

source code 

Returns a resized version of the image that fits in new_size but preserves the aspect ratio.

Parameters:
  • newSize - tuple (new_width, new_height)
Returns:
a new pyvision image that is the resized version of this image.

resize(self, newSize, **kwargs)

source code 

Returns a resized version of the image. This is a convenience function. For more control, look at the Affine class for arbitrary transformations.

Parameters:
  • newSize - tuple (new_width, new_height)
Returns:
a new pyvision image that is the resized version of this image.

scale(self, scale)

source code 

Returns a scaled version of the image. This is a convenience function. For more control, look at the Affine class for arbitrary transformations.

Parameters:
  • scale - a float indicating the scale factor
Returns:
a new pyvision image that is the scaled version of this image.

copy(self)

source code 

Returns a new pv.Image which is a copy of (only) the current image. Other internal data stored by the current pv.Image will NOT be copied. This method uses cv.CloneImage so that the underlying image data will be disconnected from the original data. (Deep copy)

crop(self, rect, size=None, interpolation=None, return_affine=False)

source code 

Crops an image to the given rectangle. Rectangle parameters are rounded to nearest integer values. High quality resampling. The default behavior is to use cv.GetSubRect to crop the image. This returns a slice the OpenCV image so modifying the resulting image data will also modify the data in this image. If a size is provide a new OpenCV image is created for that size and cv.Resize is used to copy the image data. If the bounds of the rectangle are outside the image, an affine transform (pv.AffineFromRect) is used to produce the croped image to properly handle regions outside the image. In this case the downsampling quality may not be as good. #

Parameters:
  • rect - a Rectangle defining the region to be cropped.
  • size - a new size for the returned image. If None the result is not resized.
  • interpolation - None = Autoselect or one of CV_INTER_AREA, CV_INTER_NN, CV_INTER_LINEAR, CV_INTER_BICUBIC
  • return_affine - If True, also return an affine transform that can be used to transform points.
Returns: pv.Image
a cropped version of the image or if return affine a tuple of (image,affine)

save(self, filename, annotations=False)

source code 

Save the image to a file. This is performed by converting to PIL and then saving to a file based on on the extension.

show(self, window=None, pos=None, delay=0, size=None)

source code 

Displays the annotated version of the image using OpenCV highgui

Parameters:
  • window - the name of the highgui window to use, if one already exists by this name, or it will create a new highgui window with this name.
  • pos - if a new window is being created, the (x,y) coordinate for the new window
  • delay - A delay in milliseconds to wait for keyboard input (passed to cv.WaitKey). 0 delays indefinitely, 30 is good for presenting a series of images like a video. For performance reasons, namely when using the same window to display successive frames of video, we don't want to tear-down and re-create the window each time. Thus the window frame will persist beyond the scope of the call to img.show(). The window will disappear after the program exits, or it can be destroyed with a call to cv.DestroyWindow.
  • size - Optional output size for image, None=native size.
Returns:
the return value of the cv.WaitKey call.