Converting Pandoc Markdown images from captioned to inline
How about a regular expression?

def convert(s):
  return re.sub(r"((?:\A|^ *\n)!\[.*\]\(.*\))\n(^ *\n|\Z)", r"\1\\\2", s, 0, re.M)

def test1():
  print convert("""![foo](bar)\n\nthis is a test\n""")

def test2():
  print convert("""line 1\n\n![foo](asd)\n\nanother test\n""")

def test3():
  print convert("""line 1\n\n![foo](asd)\n""")

def test4():
  print convert("""line 1\n\n![foo](asd)\nNot blank\n""")


Note: I am using ^\s*\n to match a blank line - i.e. it can also contain spaces.