Repository: imageio/imageio

Issue:
When should the Pillow plugin apply gamma correction?
*original title: Reading PNG files differently than scipy.misc*

I've been using `scipy.misc.imread`for reading (tomographic) images for processing.
After a recent upgrade of my system I'm told this is deprecated in favor of  `imageio.imread`.

Unfortunately my images are read differently depending on which I use, namely with a different gray-scale range, as can be seen below.

```
import scipy.misc
import imageio
import matplotlib.pyplot as plt
%matplotlib inline
import numpy
r = 'test.png'  # attached tho this issue
img_scipy = scipy.misc.imread(r)
img_imageio = imageio.imread(r)
plt.subplot(121)
plt.title('scipy min: %s, max: %s' %(numpy.min(img_scipy), numpy.max(img_scipy)))
plt.imshow(img_scipy)
plt.subplot(122)
plt.imshow(img_imageio)
plt.title('imageio min: %s, max: %s' %(numpy.min(img_imageio), numpy.max(img_imageio)))
plt.show()
```
![output](https://user-images.githubusercontent.com/1651235/43787395-2bf373c8-9a6b-11e8-98d4-1c04190ce38c.png)

[ImageJ](http://fiji.sc/) reports a maximum gray value of 162 for the image, which is what I would have expected...

imageio.__version__ =  '2.3.0'
scipy.__version__ = '1.1.0'

![test.png](https://user-images.githubusercontent.com/1651235/43787150-a6cf810a-9a6a-11e8-9a3d-71f06eb6e2db.png)

How can I get my correct gray value range back?

Discussion:

Patch:
diff --git a/imageio/plugins/freeimage.py b/imageio/plugins/freeimage.py
index 2e52c3d..7f93cb6 100644
--- a/imageio/plugins/freeimage.py
+++ b/imageio/plugins/freeimage.py
@@ -195,7 +195,7 @@ class FreeimagePngFormat(FreeimageFormat):
     Parameters for reading
     ----------------------
     ignoregamma : bool
-        Avoid gamma correction. Default False.
+        Avoid gamma correction. Default True.
     
     Parameters for saving
     ---------------------
@@ -212,7 +212,7 @@ class FreeimagePngFormat(FreeimageFormat):
     """
 
     class Reader(FreeimageFormat.Reader):
-        def _open(self, flags=0, ignoregamma=False):
+        def _open(self, flags=0, ignoregamma=True):
             # Build flags from kwargs
             flags = int(flags)
             if ignoregamma:
diff --git a/imageio/plugins/pillow.py b/imageio/plugins/pillow.py
index f863241..ef11c5a 100644
--- a/imageio/plugins/pillow.py
+++ b/imageio/plugins/pillow.py
@@ -212,7 +212,7 @@ class PNGFormat(PillowFormat):
     Parameters for reading
     ----------------------
     ignoregamma : bool
-        Avoid gamma correction. Default False.
+        Avoid gamma correction. Default True.
     pilmode : str
         From the Pillow documentation:
         
@@ -272,12 +272,16 @@ class PNGFormat(PillowFormat):
     """
 
     class Reader(PillowFormat.Reader):
-        def _open(self, pilmode=None, as_gray=False, ignoregamma=False):
+        def _open(self, pilmode=None, as_gray=False, ignoregamma=True):
             return PillowFormat.Reader._open(self, pilmode=pilmode, as_gray=as_gray)
 
         def _get_data(self, index):
             im, info = PillowFormat.Reader._get_data(self, index)
-            if not self.request.kwargs.get("ignoregamma", False):
+            if not self.request.kwargs.get("ignoregamma", True):
+                # The gamma value in the file represents the gamma factor for the
+                # hardware on the system where the file was created, and is meant
+                # to be able to match the colors with the system on which the
+                # image is shown. See also issue #366
                 try:
                     gamma = float(info["gamma"])
                 except (KeyError, ValueError):


Test patch:
diff --git a/tests/test_freeimage.py b/tests/test_freeimage.py
index 93e0d74..537fe76 100644
--- a/tests/test_freeimage.py
+++ b/tests/test_freeimage.py
@@ -493,6 +493,30 @@ def test_other():
     raises(Exception, imageio.imsave, fnamebase + ".jng", im, "JNG")
 
 
+def test_gamma_correction():
+    need_internet()
+
+    fname = get_remote_file("images/kodim03.png")
+
+    # Load image three times
+    im1 = imageio.imread(fname, format="PNG-FI")
+    im2 = imageio.imread(fname, ignoregamma=True, format="PNG-FI")
+    im3 = imageio.imread(fname, ignoregamma=False, format="PNG-FI")
+
+    # Default is to ignore gamma
+    assert np.all(im1 == im2)
+
+    # Test result depending of application of gamma
+    assert im1.mean() == im2.mean()
+
+    # TODO: We have assert im2.mean() == im3.mean()
+    # But this is wrong, we want: assert im2.mean() < im3.mean()
+
+    # test_regression_302
+    for im in (im1, im2, im3):
+        assert im.shape == (512, 768, 3) and im.dtype == "uint8"
+
+
 if __name__ == "__main__":
     # test_animated_gif()
     run_tests_if_main()
diff --git a/tests/test_pillow.py b/tests/test_pillow.py
index 290246d..fed9041 100644
--- a/tests/test_pillow.py
+++ b/tests/test_pillow.py
@@ -332,13 +332,27 @@ def test_images_with_transparency():
     assert im.shape == (24, 30, 4)
 
 
-def test_regression_302():
-    # When using gamma correction, the result should keep the same dtype
+def test_gamma_correction():
     need_internet()
 
     fname = get_remote_file("images/kodim03.png")
-    im = imageio.imread(fname)
-    assert im.shape == (512, 768, 3) and im.dtype == "uint8"
+
+    # Load image three times
+    im1 = imageio.imread(fname)
+    im2 = imageio.imread(fname, ignoregamma=True)
+    im3 = imageio.imread(fname, ignoregamma=False)
+
+    # Default is to ignore gamma
+    assert np.all(im1 == im2)
+
+    # Test result depending of application of gamma
+    assert im1.meta["gamma"] < 1
+    assert im1.mean() == im2.mean()
+    assert im2.mean() < im3.mean()
+
+    # test_regression_302
+    for im in (im1, im2, im3):
+        assert im.shape == (512, 768, 3) and im.dtype == "uint8"
 
 
 def test_inside_zipfile():