Setting Account Picture/JPEGPhoto with dscl in terminal
dscl

It seems that the most promising method is to modify the JPEGPhoto attribute for the user. The problem is, while the JPEG image can be converted very simply to a hex string, the value is too long to just pass at the command-line. This attribute seems to be what the GUI writes to when you drag in an image. Recovering the image from this variable when set is as simple as:

dscl . read /Users/username JPEGPhoto | xxd -r -p &gt; ./username.jpg


The second common way mentioned on the forums is to:

dscl . delete /Users/username JPEGPhoto
dscl . delete /Users/username Picture
dscl . delete /Users/username Picture "/Library/User Pictures/username.jpg"


This only changes the 'login' icon and not the icon seen in the User detail page in System Preferences.

Using sysadminctl

When creating or managing users prefer sysadminctl over the list of dscl commands that are commonly mentioned in forums. sysadminctl sets the user up correctly with a SECURE TOKEN which is important if you want to use FileVault the user should be able to unlock the disk at boot. It also offers the ability to set the user image when created. Unfortunately it doesn't offer the ability to change user images.

Using dsimport

What does work for changing the user image is dsimport.

The script usage is:

./change_userpic.sh USERNAME /path/to/image/jpg


GIST

#!/bin/bash

declare -xr AWK_CMD="/usr/bin/awk"
declare -xr SW_VERS_CMD="/usr/bin/sw_vers"
declare -xr DSIMPORT_CMD="/usr/bin/dsimport"
declare -xr ID_CMD="/usr/bin/id"

USERNAME="$1"; export USERNAME
USERPIC="$2"; export USERPIC

OSVERSION=$(${SW_VERS_CMD} -productVersion | ${AWK_CMD} -F"." '{print $2;exit}'); export OSVERSION
# Add the LDAP picture to the user record if dsimport is avaiable 10.6+
if [ -f "${USERPIC}" ] ; then
    # On 10.6 and higher this works
    if [ "${OSVERSION}" -ge "6" ] ; then
        declare -x MAPPINGS='0x0A 0x5C 0x3A 0x2C'
        declare -x ATTRS='dsRecTypeStandard:Users 2 dsAttrTypeStandard:RecordName externalbinary:dsAttrTypeStandard:JPEGPhoto'
        declare -x PICIMPORT="/Library/Caches/${USERNAME}.picture.dsimport"
        printf "%s %s \n%s:%s" "${MAPPINGS}" "${ATTRS}" "${USERNAME}" "${USERPIC}" &gt;"${PICIMPORT}"
        # Check to see if the username is correct and import picture
        if ${ID_CMD} "${USERNAME}" &amp;&gt;/dev/null ; then
            # No credentials passed as we are running as root
            ${DSIMPORT_CMD} "${PICIMPORT}" /Local/Default M &amp;&amp;
                echo "Successfully imported users picture."
        fi
    fi
fi