It depends whether you can ask 'wo' or 'wohin', thus whether a place is referred to or direction or movement.
Unter dem Bild schrieb er --> the image was held above him while he wrote (whatever)
Unter das Bild schrieb er --> he wrote something onto a place below the image
One can even combine it of course:
Unter dem Bild schrieb er etwas unter das Bild. --> he wrote something under the image while being below the image.
This sense of direction is not always very clear, and you can think of the text being moved from a mental place to the position under the image. Other examples might make it slightly clearer:
Wir gehen unter der Brücke --> we are walking under the bridge (but already are there)
Wir gehen unter die Brücke --> we (are going to) walk under that bridge (over there), thus need to still get there
Wir sehen unter dem Baum eine Eule --> we see a owl under that tree (the owl is under the tree and we see her with her place fixed)
Wir setzen uns unter den Baum zum Picknick --> We will (go and) sit under that tree for a picknick. Thus a sense of direction or movement is implied.