# Writing Scripts

You can use the built-in script editor to write a script, or you could use another editor (live Visual Studio Code) and copy and paste the code into the Astra Image editor. Please be careful when opening a script from at third party, as Python has the ability to modify your system.

**Example**\
The following example shows the basic flow of using Python with Astra Image. Please note that all image data is normalized (i.e. in the range 0.0 to 1.0). When the image data has been processed in Python, Astra Image expects the result to also be normalized between 0.0 to 1.0.

<mark style="color:orange;">import AstraImage as ai import numpy as np import cv2</mark>

<mark style="color:green;"># Get the image data from Astra Image and put into a numpy array</mark>\ <mark style="color:orange;">data = np.frombuffer(ai.getImageDataFloat(), dtype=np.float32)</mark>

<mark style="color:green;"># You can set the shape of the array if needed.</mark>\ <mark style="color:orange;">data.shape = (ai.getWidth(), ai.getHeight(), 3)</mark>

<mark style="color:green;"># Do something with the data.</mark>\ <mark style="color:orange;">data = (1.0 - data)</mark>

<mark style="color:green;"># Update the image data in Astra Image</mark>\ <mark style="color:orange;">ai.setImageDataFloat(data.tobytes(), ai.getWidth(), ai.getHeight())</mark>

In this example, the image data is retrieved from Astra Image and loaded into a numpy array. In this case, an inverted image is generated with a simple numpy operation:

<mark style="color:orange;">data = (1.0 - data)</mark>

After modifying the image, the data is updated in Astra Image.
