Modifying images

### 27.1.7 Modifying images

##### Setting pixel data explicitly.

Existing image objects can be modified by using the set_channel_data command.

- set_channel_data takes four or five arguments:
  - img, an image object.
  - chn, a channel index, which should be either a nonnegative integer or red for the red channel, green for the green channel, blue for the blue channel, grey for the grayscale channel resp. tran for the transparency channel.

    For grayscale images without a transparency channel, this argument should be omitted since there is only one channel.

  - x, an integer.
  - y, an integer.
  - data, a matrix of integers between 0 and 255.
- set_channel_data(img ⟨,chn ⟩,x,y,data) returns a modified copy of img in which the indicated channel components of pixels in the rectangle with upper left corner (x,y) and lower right corner (x+w−1,y+h−1), where w resp. h is the number of columns resp. rows of the matrix data, are set to the values in data; i.e. the chn component in the pixel (x+j,y+i) in is set to dataij for i=0,…,h−1 and j=0,…,w−1.

##### Inverting pixel data.

To produce a negative of an image, i.e. to apply the function p↦ 255−p to all channel data entries, use the unary operator - or the command inv.