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.

Table 12.2 Image manipulation methods of wx.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

An image mask is a special color set in the image which is rendered as transparent when the image is displayed on top of some other part of your display. A virtual greenscreen, so to speak, but you can set it to any color you want. You can set an image mask with the method SetMaskColor(red, green, blue), where red, green, and blue define the colors of the mask for the image. If you want to turn off the mask, use SetMask(False), and reset it with SetMask(True). The method Has-Mask() returns a Boolean with the current state of the mask. You can also set the mask from another image of the same size using the method SetMaskFrom-Image(mask, mr, mg, mb)—in this case the mask is defined to be all the pixels in the mask wx.Image that have the color mr, mg, mb, regardless of what color those pixels are in the main image. This gives you a lot of flexibility in creating a mask, since you no longer have to worry about the color of the pixels in your original image. You can retrieve the mask color using the methods GetMaskRed(), Get-MaskGreen(), and GetMaskBlue(). If an image with a mask is converted to a wx.Bitmap then the mask is automatically converted to a wx.Mask object and assigned to the bitmap.

Setting alpha values to specify a transparent image

An alpha value is another way to specify a transparent or partially transparent image. Each pixel has an alpha value, which is between 0 (if the image is totally transparent at that pixel) and 255 (if the image is completely opaque at that pixel). You set alpha values with the SetAlphaData(data) method, which takes a string of byte values similar to SetData(), but with only one value per pixel. Like SetData(), SetAlphaData() does no range checking. You can see if an alpha is set using HasAlpha() and you can recover the entire data set with GetAlphaData(). You can set the alpha value of a specific pixel with SetAlpha(x, y, alpha), and recover that value with GetAlpha(x, y).

In contrast to the image manipulation capabilities of wx.lmage, you can do relatively little with a wx.Bitmap. Nearly all of the methods of the wx.Bitmap are simple getters of properties such as width, height, and color depth.

Was this article helpful?

+2 -1

Post a comment