Repository: python-pillow/Pillow

Issue:
Request: EPS file processing without ghostscript popup
Feature request if possible. Sorry if I am overlooking a solution to this.
Option to "silent mode" or suppress ghostscript command window for eps processing. If possible this should likely be the default behavior.

This code works correctly but causes a command window to pop up then quickly close for ghostscript. Makes my system a bit unusable for a while when it is processing lots of files.

```python
from PIL import Image
epsfile = 'file.eps'
img_obj=Image.open(epsfile)
img_obj.save('temp_test_output.jpg')
```

* OS: Windows 10
* Python: 3.6.0
* Pillow: 5.2.0
* Ghostscript: 9.25 (behavior also happened with 9.23)

Hide the Ghostscript progress dialog popup on Windows
Fixes #3371.

Changes proposed in this pull request:

 * Use the `-dNoCancel` on Windows

The docs say:

> The mswinpr2 device supports a limited number of command-line parameters (e.g. it does not support setting the printer resolution). The recognized parameters are the following:
...
> `-dNoCancel`
> Hides the progress dialog, which shows the percent of the document page already processed and also provides a _cancel_ button. This option is useful if GS is intended to print pages in the background, without any user intervention.

https://www.ghostscript.com/doc/9.23/Devices.htm#Win


Discussion:
Please can you try out PR #3373?

Thanks.
That unfortunately did not work. However, I think I found a solution. I am really new to this and don't know my way around pulls/github. Could you check and see if this looks right/could be considered for adding into EpsImagePlugin.py?

Starting at line 143
```python
with open(os.devnull, 'w+b') as devnull:
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    subprocess.check_call(command, startupinfo=startupinfo, stdin=devnull, stdout=devnull)
```
A little further research and it appears this may break things on non-windows systems requiring a little more code
https://stackoverflow.com/questions/1016384/cross-platform-subprocess-with-hidden-window

```python
with open(os.devnull, 'w+b') as devnull:
    if os.name == 'nt':
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        subprocess.check_call(command, startupinfo=startupinfo, stdin=devnull, stdout=devnull)
    else:
        subprocess.check_call(command, stdin=devnull, stdout=devnull)
```
edit - sorry bad code indents


Resolving patch:
diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py
--- a/src/PIL/EpsImagePlugin.py
+++ b/src/PIL/EpsImagePlugin.py
@@ -41,7 +41,7 @@
     if hasattr(shutil, 'which'):
         which = shutil.which
     else:
-        # Python < 3.3
+        # Python 2
         import distutils.spawn
         which = distutils.spawn.find_executable
     for binary in ('gswin32c', 'gswin64c', 'gs'):
@@ -62,7 +62,7 @@ def has_ghostscript():
                 subprocess.check_call(['gs', '--version'], stdout=devnull)
             return True
         except OSError:
-            # no ghostscript
+            # No Ghostscript
             pass
     return False
 
@@ -97,9 +97,9 @@ def Ghostscript(tile, size, fp, scale=1):
         os.close(in_fd)
         infile = infile_temp
 
-        # ignore length and offset!
-        # ghostscript can read it
-        # copy whole file to read in ghostscript
+        # Ignore length and offset!
+        # Ghostscript can read it
+        # Copy whole file to read in Ghostscript
         with open(infile_temp, 'wb') as f:
             # fetch length of fp
             fp.seek(0, 2)
@@ -115,13 +115,13 @@ def Ghostscript(tile, size, fp, scale=1):
                 lengthfile -= len(s)
                 f.write(s)
 
-    # Build ghostscript command
+    # Build Ghostscript command
     command = ["gs",
                "-q",                         # quiet mode
                "-g%dx%d" % size,             # set output geometry (pixels)
                "-r%fx%f" % res,              # set input DPI (dots per inch)
                "-dBATCH",                    # exit after processing
-               "-dNOPAUSE",                  # don't pause between pages,
+               "-dNOPAUSE",                  # don't pause between pages
                "-dSAFER",                    # safe mode
                "-sDEVICE=ppmraw",            # ppm driver
                "-sOutputFile=%s" % outfile,  # output file
@@ -136,10 +136,15 @@ def Ghostscript(tile, size, fp, scale=1):
             raise WindowsError('Unable to locate Ghostscript on paths')
         command[0] = gs_windows_binary
 
-    # push data through ghostscript
+    # push data through Ghostscript
     try:
         with open(os.devnull, 'w+b') as devnull:
-            subprocess.check_call(command, stdin=devnull, stdout=devnull)
+            startupinfo = None
+            if sys.platform.startswith('win'):
+                startupinfo = subprocess.STARTUPINFO()
+                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
+            subprocess.check_call(command, stdin=devnull, stdout=devnull,
+                                  startupinfo=startupinfo)
         im = Image.open(outfile)
         im.load()
     finally:
@@ -227,7 +232,7 @@ def _open(self):
 
                 try:
                     m = split.match(s)
-                except re.error as v:
+                except re.error:
                     raise SyntaxError("not an EPS file")
 
                 if m:
@@ -242,7 +247,7 @@ def _open(self):
                             self.size = box[2] - box[0], box[3] - box[1]
                             self.tile = [("eps", (0, 0) + self.size, offset,
                                           (length, box))]
-                        except:
+                        except Exception:
                             pass
 
                 else:


Test patch: