Node gallery: display image count in a template file
One answer:

&lt;?php
  $results = db_select('field_data_node_gallery_ref_1')
  -&gt;fields(NULL, array('entity_id'))
  -&gt;condition('node_gallery_ref_1_target_id', $node-&gt;nid)
  -&gt;execute()-&gt;fetchAll();

  $pcount = count($results);
?&gt;


Explanation:

The field_data_node_gallery_ref_1 table contains one row for each picture in the gallery. I filter it by the nid of the gallery (node_gallery_ref_1_target_id) in order to get only pictures in that gallery, then count how many pictures exist on the gallery by counting the results of the query array.