blob: 8ced5bd5bbfb424e1239752e97db04335011e1c2 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`imageop` --- Manipulate raw image data
3============================================
4
5.. module:: imageop
6 :synopsis: Manipulate raw image data.
7
8
9The :mod:`imageop` module contains some useful operations on images. It operates
10on images consisting of 8 or 32 bit pixels stored in Python strings. This is
11the same format as used by :func:`gl.lrectwrite` and the :mod:`imgfile` module.
12
13The module defines the following variables and functions:
14
15
16.. exception:: error
17
18 This exception is raised on all errors, such as unknown number of bits per
19 pixel, etc.
20
21
22.. function:: crop(image, psize, width, height, x0, y0, x1, y1)
23
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.
30
31
32.. function:: scale(image, psize, width, height, newwidth, newheight)
33
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.
37
38
39.. function:: tovideo(image, psize, width, height)
40
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.
45
46
47.. function:: grey2mono(image, width, height, threshold)
48
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`.
52
53
54.. function:: dither2mono(image, width, height)
55
56 Convert an 8-bit greyscale image to a 1-bit monochrome image using a
57 (simple-minded) dithering algorithm.
58
59
60.. function:: mono2grey(image, width, height, p0, p1)
61
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.
66
67
68.. function:: grey2grey4(image, width, height)
69
70 Convert an 8-bit greyscale image to a 4-bit greyscale image without dithering.
71
72
73.. function:: grey2grey2(image, width, height)
74
75 Convert an 8-bit greyscale image to a 2-bit greyscale image without dithering.
76
77
78.. function:: dither2grey2(image, width, height)
79
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.
82
83
84.. function:: grey42grey(image, width, height)
85
86 Convert a 4-bit greyscale image to an 8-bit greyscale image.
87
88
89.. function:: grey22grey(image, width, height)
90
91 Convert a 2-bit greyscale image to an 8-bit greyscale image.
92
93
94.. data:: backward_compatible
95
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.
104