## What can I do with an image

Once you have your image in wxPython, you can manipulate it in numerous useful ways, and use it to write some powerful image adjustment scripts.

You can query the size of the image with the methods GetWidth() and Get-Height(). You can also recover the color value at any pixel with the methods GetRed(x, y), GetGreen(x, y), and GetBlue(x, y). The return value of all these color methods is an integer with a value between 0 and 255 (in C terms, it's an unsigned int, but that distinction is not particularly meaningful in Python). Sim-ilary, you can set an individual pixel with SetRGB(x, y, red, green, blue), where x and y are the pixel coordinates, and the color values are between 0 and 255.

You can get all the data in one enormous lump with the method GetData(). The return value of the GetData() method is a big string where each character represents a member of an RGB triple, and each character can be considered an integer value between 0 and 255. The values are in order, with the first one being the red value of the pixel at (0, 0), the next one being the green value at (0, 0), and the next being the blue value at (0, 0). The next three are the color values at (0, 1) and so on. Algorithmically, it could be defined using the following Python pseudocode.

def GetData(self): result = ""

for y in range(self.GetHeight()):

for x in range(self.GetWidth()):

result.append(chr(self.GetRed(x,y))) result.append(chr(self.GetGreen(x,y))) result.append(chr(self.GetBlue(x,y))) return result

There are two things to be aware of when using the corresponding SetData(data) method that reads in similarly formatted string of RGB values. First, the Set-Data() method does not perform range or bounds checking to determine if your incoming string has values in the right range or if it is the correct length given the size of the image. If your values are incorrect, the behavior is undefined. Secondly, due to the way the underlying C + + code manages memory, it's a bad idea to pass a string returned by GetData() right back into SetData()—you should make a fresh string.

The image data string can easily be converted to and from other Python types that make it easier to access and manipulate the RGB values as integers, such as an array or a numeric type. For example, to make something that hurts the eyes if you stare at it too long, try this import array img = wx.EmptyImage(100,100) a = array.array('B', img.GetData()) for i in range(len(a)): a[i] = (2 5 + i) % 256 img.SetData(a.tostring())

Table 12.2 defines the many methods of wx.lmage that perform simple image manipulations.

Those methods represent the starter set of image manipulation. In the next section we'll show you two ways to manage the more complex topic of a transparent or semitransparent image.

 Method Description ConvertToMono(r, g, b) Returns a wx.Image the same size as the original, where all pixels with the exact r, g, b color value are white, and all others are black. The original image is untouched. Mirror(horizontally=True) Returns a mirror image of the original. If the horizontally parameter is True, then the image is flipped around the horizontal axis, otherwise around vertical. The original image is unchanged. Replace(r1, g1, b1, r2, g2, b2) Changes the called image in-place. Every pixel with a color value of r1, g1, b1 is reset to the color value r2, g2, b2. Rescale(width, height) Changes the size of the image to the new width and height. The original image is changed in-place, with pixel colors scaled to fit the new size. Rotate(angle, rotationCentre, interpolating= True, offestAfterRotation = None) Returns a new image created by rotating the original image. The angle parameter is a floating point indicating the amount of rotation in radians. The rotationCentre is a wx.Point around which the rotation occurs. If interpolating is True, a slower, more accurate algorithm is used. The offsetAfterRotation is a point indicating how much the image should be shifted after the rotation. Any blank pixels uncovered by the rotation will be set to black or to the mask color if the image has a mask. Rotate90(clockwise=True) Rotates the image 90 degrees in the direction governed by the Boolean clockwise parameter. Scale(width, height) Returns a copy of the orginal image scaled to the new width and height.

Setting image masks to specify a transparent image