diff --git a/PIL/PpmImagePlugin.py b/PIL/PpmImagePlugin.py
--- a/PIL/PpmImagePlugin.py
+++ b/PIL/PpmImagePlugin.py
@@ -93,6 +93,8 @@ def _open(self):
                     s = self.fp.read(1)
                     if s not in b_whitespace:
                         break
+                    if s == b"":
+                        raise ValueError("File does not extend beyond magic number")
                 if s != b"#":
                     break
                 s = self.fp.readline()

diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py
--- a/Tests/test_file_ppm.py
+++ b/Tests/test_file_ppm.py
@@ -35,6 +35,14 @@ def test_16bit_pgm_write(self):
         reloaded = Image.open(f)
         self.assert_image_equal(im, reloaded)
 
+    def test_truncated_file(self):
+        path = self.tempfile('temp.pgm')
+        f = open(path, 'w')
+        f.write('P6')
+        f.close()
+
+        self.assertRaises(ValueError, lambda: Image.open(path))
+
 
 if __name__ == '__main__':
     unittest.main()

Three "images" that cause PIL.Image.open() to hang
Hello,

Here's what I found with my early afl-fuzz fuzzing:

```
(virtualenv)[1/6][14:45:24][~/workspace/Pillow/pillow-virtualenv/o/hangs]$ for file in *; do hexdump -C $file; done
00000000  50 36                                             |P6|
00000002
00000000  50 35                                             |P5|
00000002
00000000  50 79 50                                          |PyP|
00000003
```


Hi. Thanks for reporting, but unfortunately I don't quite follow. Would you be able to post some Python code that reproduces the problem?
