Exporting images

### 28.1.5 Exporting images

The writergb command writes images to PNG files. The image can be either an image object or given by the full Xcas legacy image structure (see Section 28.1.1, this is what is read in with readrgb) or a simplified version of this structure.

- writergb takes two arguments:
  - filename, a file name.
  - image, an image in Xcas format (either the new or legacy one).
- writergb(filename,image) writes the image image to the file filename. If image is an image object, writergb returns 1 if the export was successful, otherwise it returns 0. If image is the image structure (either full or simplified), then writergb returns its graphical representation.

#### Examples

##### With image objects.

Assuming that the img variable contains an image object created with the image command (see Section 28.1.2), input:

writergb("image.png",img)

1 |  |  |  |  |  |  |  |  |  |

##### With the legacy format.

Assume that the variable a represents a legacy image structure loaded from the file image1234.jpg by using readrgb (see the example in Section 28.1.3). Then:

writergb("image2134.png",[a[0],a[2],a[1],a[3],a[4]])

The image file image2134.png is thus created. This image is simply the image1234.png image with the green and red colors switched.

In simpler cases, you can type the Xcas image format in by hand.

writergb("image1.png",[[4,2,2],[[255,0],[0,0]],[[0,255],[0,0]], [[255,125],[255,255]],[[0,0],[255,0]]])

The transparency value of 125 for the upper right point makes it partially transparent and mutes the color. Observe that individual pixels are shown as rectangles due to the automatic scaling.

For larger images, in some cases the matrix operations of Xcas can be used to create the channels. The following command line creates the file image2.png containing the image shown below (the view is orthonormalized).

writergb("image2.png",[[4,30,30],makemat(0,30,30),makemat(0,30,30), makemat(255,30,30),makemat(0,30,30)+idn(30)*255])

##### Writing images with simplified image description.

The simplified version of the Xcas image description does not involve stating the number of channels, the size of the image, or the transparency. There is a full color version of this simplified form and a grayscale version.

- To create a full color image using the simple description, writergb takes four arguments:
  - filename, the name of the file to store the image.
  - R, a matrix for the red channel.
  - G, a matrix for the green channel.
  - B, a matrix for the blue channel.
- writergb(filename,R,G,B) draws the image given by the matrices to the file filename.
- To create a grayscale image using the simple description, writergb takes two arguments:
  - filename, the name of the file to store the image.
  - M, a matrix representing how dark each point is (where 0 is black and 255 is white).
- writergb(filename,M) draws the image given by M to the file filename.

#### Examples

writergb("image3.png",[[255,250],[0,120]],[[0,255],[0,0]],[[0,0],[255,100]])

writergb("image4.png",[[65,125],[185,200]])