> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/modularity/image-manipulation/reference/built-in-functions/imagewrite.md).

# ImageWrite

## Syntax

```
ImageWrite( name [, path] )
```

Or as a member:

```
someImage.write( [path] )
```

## Arguments

| Name | Type   | Required | Description                                                                                                                           |
| ---- | ------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| name | any    | Yes      | The image to write. Can be a `BoxImage` object or image name.                                                                         |
| path | String | No       | The file path to write the image to. If omitted, writes back to the image's original source path (only for images loaded from files). |

## Returns

`BoxImage` — The image object after writing to disk.

## Description

Writes the specified image to disk. The function has two modes:

* **With path parameter**: Writes the image to the specified file location. The output format is **auto-detected from the file extension** (e.g., `.png` → PNG, `.jpg` → JPEG, `.webp` → WebP, `.gif` → GIF, `.bmp` → BMP, `.tiff` → TIFF).
* **Without path parameter**: Writes the image back to its original source location (requires the image was loaded from a file).

Parent directories are automatically created if they don't exist. The file stream is properly closed after writing, so the file can be safely deleted or moved immediately after writing.

**Alpha Channel Handling:** When writing to formats that do not support transparency (JPEG, BMP), images with alpha channels are automatically composited onto a white background before encoding, preventing write failures.

## Example

```boxlang
// Write image to a specific file (format auto-detected from extension)
ImageWrite( myImage, "output/photo.png" );
ImageWrite( myImage, "output/photo.jpg" );   // → JPEG
ImageWrite( myImage, "output/photo.webp" );  // → WebP
ImageWrite( myImage, "output/photo.gif" );   // → GIF

// Write image back to its original location (modify in place)
img = ImageRead("photo.jpg");
img.blur(5);
img.write();  // Writes back to photo.jpg

// Write to a new location with directory creation
ImageWrite( myImage, "deep/nested/path/photo.png" );  // Creates directories automatically

// As a member function (chainable)
myImage.resize(800, 600)
       .blur(2)
       .write("output/edited.png");
```

## Related BIFs

* ImageRead
* ImageNew
* ImageGetBlob
* ImageWriteBase64

## Notes

* The `name` argument can be a `BoxImage` object or the name of an image variable in the current context.
* If no path is provided, the image must have been loaded from a file (via `ImageRead` or `ImageNew` with a file path).
* Images created from scratch (blank canvases) or from Base64 cannot use the parameterless `write()` - they require an explicit path.
* Parent directories in the path are created automatically if they don't exist.
* The operation returns the image object for chaining or further processing.
* File handles are properly managed - files can be deleted immediately after writing (no Windows file lock issues).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/modularity/image-manipulation/reference/built-in-functions/imagewrite.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
