diff --git a/wordcloud/wordcloud_cli.py b/wordcloud/wordcloud_cli.py
index <HASH>..<HASH> 100644
--- a/wordcloud/wordcloud_cli.py
+++ b/wordcloud/wordcloud_cli.py
@@ -90,8 +90,11 @@ def main(args, text, imagefile):
     wordcloud.generate(text)
     image = wordcloud.to_image()
 
-    with imagefile:
+    try:
         image.save(imagefile, format='png', optimize=True)
+    finally:
+        if imagefile != sys.stdout.buffer:
+            imagefile.close()
 
 
 def make_parser():
Replaces the imagefile context manager with try/finally.