I think you might be able to make this code a bit clearer/more robust by doing something like:

var base = path.basename(img.source, path.extname(img.srouce));
if (base.endsWith("_o")) imageSource = img.