2 :mod:`imageop` --- Manipulate raw image data
3 ============================================
6 :synopsis: Manipulate raw image data.
9 The :mod:`imageop` module contains some useful operations on images. It operates
10 on images consisting of 8 or 32 bit pixels stored in Python strings. This is
11 the same format as used by :func:`gl.lrectwrite` and the :mod:`imgfile` module.
13 The module defines the following variables and functions:
18 This exception is raised on all errors, such as unknown number of bits per
22 .. function:: crop(image, psize, width, height, x0, y0, x1, y1)
24 Return the selected part of *image*, which should be *width* by *height* in size
25 and consist of pixels of *psize* bytes. *x0*, *y0*, *x1* and *y1* are like the
26 :func:`gl.lrectread` parameters, i.e. the boundary is included in the new image.
27 The new boundaries need not be inside the picture. Pixels that fall outside the
28 old image will have their value set to zero. If *x0* is bigger than *x1* the
29 new image is mirrored. The same holds for the y coordinates.
32 .. function:: scale(image, psize, width, height, newwidth, newheight)
34 Return *image* scaled to size *newwidth* by *newheight*. No interpolation is
35 done, scaling is done by simple-minded pixel duplication or removal. Therefore,
36 computer-generated images or dithered images will not look nice after scaling.
39 .. function:: tovideo(image, psize, width, height)
41 Run a vertical low-pass filter over an image. It does so by computing each
42 destination pixel as the average of two vertically-aligned source pixels. The
43 main use of this routine is to forestall excessive flicker if the image is
44 displayed on a video device that uses interlacing, hence the name.
47 .. function:: grey2mono(image, width, height, threshold)
49 Convert a 8-bit deep greyscale image to a 1-bit deep image by thresholding all
50 the pixels. The resulting image is tightly packed and is probably only useful
51 as an argument to :func:`mono2grey`.
54 .. function:: dither2mono(image, width, height)
56 Convert an 8-bit greyscale image to a 1-bit monochrome image using a
57 (simple-minded) dithering algorithm.
60 .. function:: mono2grey(image, width, height, p0, p1)
62 Convert a 1-bit monochrome image to an 8 bit greyscale or color image. All
63 pixels that are zero-valued on input get value *p0* on output and all one-value
64 input pixels get value *p1* on output. To convert a monochrome black-and-white
65 image to greyscale pass the values ``0`` and ``255`` respectively.
68 .. function:: grey2grey4(image, width, height)
70 Convert an 8-bit greyscale image to a 4-bit greyscale image without dithering.
73 .. function:: grey2grey2(image, width, height)
75 Convert an 8-bit greyscale image to a 2-bit greyscale image without dithering.
78 .. function:: dither2grey2(image, width, height)
80 Convert an 8-bit greyscale image to a 2-bit greyscale image with dithering. As
81 for :func:`dither2mono`, the dithering algorithm is currently very simple.
84 .. function:: grey42grey(image, width, height)
86 Convert a 4-bit greyscale image to an 8-bit greyscale image.
89 .. function:: grey22grey(image, width, height)
91 Convert a 2-bit greyscale image to an 8-bit greyscale image.
94 .. data:: backward_compatible
96 If set to 0, the functions in this module use a non-backward compatible way
97 of representing multi-byte pixels on little-endian systems. The SGI for
98 which this module was originally written is a big-endian system, so setting
99 this variable will have no effect. However, the code wasn't originally
100 intended to run on anything else, so it made assumptions about byte order
101 which are not universal. Setting this variable to 0 will cause the byte
102 order to be reversed on little-endian systems, so that it then is the same as
103 on big-endian systems.