Repository: python-pillow/Pillow

Issue:
 python setup.py install: Jpeg2KDecode.c: undefined reference to `opj_stream_create'
### What did you do?

Attempted to install Pillow on Windows using `setup.py` in a MSYS2 (MINGW32) environment
```
$ python setup.py install
```
### What actually happened?
Compilation failed with:
```
build/temp.mingw-2.7/libImaging/Jpeg2KEncode.o:Jpeg2KEncode.c:(.text+0xfd9): undefined reference to `opj_end_compress'
collect2.exe: error: ld returned 1 exit status
error: command 'C:\\msys32\\mingw32\\bin/i686-w64-mingw32-gcc.exe' failed with exit status 1
```
[pillow-compilation.log](https://github.com/python-pillow/Pillow/files/1464226/pillow-compilation.log)

### What versions of Pillow and Python are you using?
* Pillow - `git clone https://github.com/python-pillow/Pillow`
* Python 2.7.13



Discussion:
I have managed to build and install the latest pillow (4.3.0) in the MSYS2 (MINGW32) environment using `pacman`, and  https://github.com/Alexpux/MINGW-packages/pull/3138
We've only got the basics of msys2 tested, with none of the extra libraries.  What packaging of openjpeg were you using?  I suspect that it's notfinding the shared library at the compile time, even though the shared library was found in the setup file.
Hi @wiredfool,

I am not familiar with openjpeg or packaging of openjpeg. I was just trying to install the latest pillow in MSYS2 (MINGW32) environment.

Pillow is required by https://github.com/blockdiag/sphinxcontrib-blockdiag, but sphinxcontrib-blockdiag failed to install it correctly for me in MSYS2.

The workaround was to install pillow before installing sphinxcontrib-blockdiag.

If you didn't explicitly install openjpeg from source, it's likely from the msys2 package system. 

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -711,7 +711,7 @@ def build_extensions(self):
         if feature.jpeg2000:
             libs.append(feature.jpeg2000)
             defs.append(("HAVE_OPENJPEG", None))
-            if sys.platform == "win32":
+            if sys.platform == "win32" and not PLATFORM_MINGW:
                 defs.append(("OPJ_STATIC", None))
         if feature.zlib:
             libs.append(feature.zlib)


Test patch:
diff --git a/Tests/helper.py b/Tests/helper.py
--- a/Tests/helper.py
+++ b/Tests/helper.py
@@ -6,6 +6,7 @@
 import os
 import shutil
 import sys
+import sysconfig
 import tempfile
 from io import BytesIO
 
@@ -288,6 +289,10 @@ def is_pypy():
     return hasattr(sys, "pypy_translation_info")
 
 
+def is_mingw():
+    return sysconfig.get_platform() == "mingw"
+
+
 if sys.platform == "win32":
     IMCONVERT = os.environ.get("MAGICK_HOME", "")
     if IMCONVERT:
diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py
--- a/Tests/test_imagefont.py
+++ b/Tests/test_imagefont.py
@@ -13,6 +13,7 @@
     assert_image_equal,
     assert_image_similar,
     assert_image_similar_tofile,
+    is_mingw,
     is_pypy,
     is_win32,
     skip_unless_feature,
@@ -660,6 +661,7 @@ def test_variation_get(self):
             {"name": b"Size", "minimum": 0, "maximum": 300, "default": 0}
         ]
 
+    @pytest.mark.skipif(is_mingw(), reason="epsilon too high for meaningful test")
     def test_variation_set_by_name(self):
         font = self.get_font()
 
@@ -692,6 +694,7 @@ def _check_text(font, path, epsilon):
             font.set_variation_by_name(name)
         _check_text(font, "Tests/images/variation_tiny_name.png", 40)
 
+    @pytest.mark.skipif(is_mingw(), reason="epsilon too high for meaningful test")
     def test_variation_set_by_axes(self):
         font = self.get_font()
 
diff --git a/Tests/test_imagefontctl.py b/Tests/test_imagefontctl.py
--- a/Tests/test_imagefontctl.py
+++ b/Tests/test_imagefontctl.py
@@ -60,7 +60,7 @@ def test_complex_unicode_text():
 
     target = "Tests/images/test_complex_unicode_text2.png"
     with Image.open(target) as target_img:
-        assert_image_similar(im, target_img, 2.3)
+        assert_image_similar(im, target_img, 2.33)
 
 
 def test_text_direction_rtl():