internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: python-pillow/Pillow

Issue:
Incorrect output when converting greyscale to bilevel
### What did you do?
converted test image to bilevel
### What did you expect to happen?
Most of the image should be black, only the very bottom should be white
### What actually happened?
half is black, half is white
### What are your OS, Python and Pillow versions?

* OS: Windows 10
* Python: 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]
* Pillow: 5.3.0

Documentation states 
If dither is NONE, all non-zero values are set to 255 (white)

included image and code show that this is not the case, instead any value under 128 becomes white while any value over 128 becomes black.
Considering the behavior of other programs, it is likely that the documentation is wrong rather than the behavior.

[graydient.zip](https://github.com/python-pillow/Pillow/files/2441111/graydient.zip)

Files have been zipped due to github not accepting bmp images.

```python
Image.open("input.bmp").convert("1", dither=Image.NONE).save("output.bmp")
```



Discussion:
The [docs](https://pillow.readthedocs.io/en/5.3.x/reference/Image.html?highlight=convert#PIL.Image.Image.convert) say:

> The default method of `convert`ing a greyscale (“L”) or “RGB” image into a bilevel (mode “1”) image uses Floyd-Steinberg dither to approximate the original image luminosity levels. If dither is NONE, all non-zero values are set to 255 (white). 

Looks like the conversion is done in `tobilevel` which indeed has a cutoff at 128:

https://github.com/python-pillow/Pillow/blob/3437d5fcb4880f622f72f313ecc764e140f13dfb/src/libImaging/Convert.c#L1203-L1285

This hasn't really changed since the PIL->Pillow fork:

https://github.com/python-pillow/Pillow/blame/master/src/libImaging/Convert.c#L1203-L1285

Those docs were last edited in https://github.com/python-pillow/Pillow/issues/407 to correct the bit about FLOYDSTEINBERG being used by default, but didn't adjust this bit.

Please could you send a PR to correct the docs? It's from the docstring:

https://github.com/python-pillow/Pillow/blob/master/src/PIL/Image.py#L856

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -873,9 +873,9 @@ def convert(self, mode=None, matrix=None, dither=None,
         The default method of converting a greyscale ("L") or "RGB"
         image into a bilevel (mode "1") image uses Floyd-Steinberg
         dither to approximate the original image luminosity levels. If
-        dither is NONE, all non-zero values are set to 255 (white). To
-        use other thresholds, use the :py:meth:`~PIL.Image.Image.point`
-        method.
+        dither is NONE, all values larger than 128 are set to 255 (white),
+        all other values to 0 (black). To use other thresholds, use the
+        :py:meth:`~PIL.Image.Image.point` method.
 
         When converting from "RGBA" to "P" without a **matrix** argument,
         this passes the operation to :py:meth:`~PIL.Image.Image.quantize`,


Test patch:
Repository: python-pillow/Pillow

Issue:
WebPImagePlugin.py - ResourceWarning: unclosed file
### What did you do?

```console
> python -Wd
Python 3.7.0 (default, Jul 15 2018, 10:44:58)
[GCC 8.1.1 20180531] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> with Image.open('tshirt.webp') as img:
...     img.save('tshirt.png', 'PNG')
...
/home/pajadam/.venv/hype/lib/python3.7/site-packages/PIL/WebPImagePlugin.py:156: ResourceWarning: unclosed file <_io.BufferedReader name='tshirt.webp'>
  self.fp = BytesIO(data)
>>>
```

### What did you expect to happen?

No warnings, especially while using context manager. 

### What actually happened?

Following warning occurs:

```
/home/pajadam/.venv/hype/lib/python3.7/site-packages/PIL/WebPImagePlugin.py:156: ResourceWarning: unclosed file <_io.BufferedReader name='tshirt.webp'>
  self.fp = BytesIO(data)
```

### What versions of Pillow and Python are you using?

- `Pillow==5.2`
- `Python 3.7`

Minimal example:
```python
from PIL import Image

with Image.open('tshirt.webp') as img:
     img.save('tshirt.png', 'PNG')
```



Discussion:
Thanks for reporting this. This can be replicated using transparent.webp from the Pillow test images.

I have created PR #3341
Re-opening as #3341 caused an error and was reverted in #3415.

Resolving patch:
diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py
--- a/src/PIL/WebPImagePlugin.py
+++ b/src/PIL/WebPImagePlugin.py
@@ -163,6 +163,8 @@ def load(self):
                 self.__loaded = self.__logical_frame
 
                 # Set tile
+                if self.fp and self._exclusive_fp:
+                    self.fp.close()
                 self.fp = BytesIO(data)
                 self.tile = [("raw", (0, 0) + self.size, 0, self.rawmode)]
 


Test patch:
diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py
--- a/Tests/test_file_webp.py
+++ b/Tests/test_file_webp.py
@@ -139,7 +139,6 @@ def test_WebPDecode_with_invalid_args(self):
             self.assertRaises(TypeError, _webp.WebPAnimDecoder)
         self.assertRaises(TypeError, _webp.WebPDecode)
 
-    @unittest.skip("Currently is not working")
     def test_no_resource_warning(self):
         file_path = "Tests/images/hopper.webp"
         image = Image.open(file_path)
Repository: python-pillow/Pillow

Issue:
Documentation on readthedocs.io should list functions on the left navigation bar
It's difficult to see an overview of the available functions in a class.
Each function should be listed on the navigation panel.


Discussion:
To be clear, you're suggesting a list of functions instead of 'Image processing', 'Constructing images', 'Registering plugins' in this screenshot, yes?
![readthedocs](https://user-images.githubusercontent.com/3112309/44615900-005e5180-a888-11e8-8e4c-6875d61f0216.png)
Exactly

Resolving patch:
diff --git a/docs/conf.py b/docs/conf.py
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -286,3 +286,6 @@
 
 # If true, do not generate a @detailmenu in the "Top" node's menu.
 # texinfo_no_detailmenu = False
+
+def setup(app):
+    app.add_javascript('js/script.js')


Test patch:
Repository: python-pillow/Pillow

Issue:
Todo: update docs/reference/open_files.rst
This is out of date after the _exclusive_fp changes.


Discussion:

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -823,8 +823,10 @@ def load(self):
         Image class automatically loads an opened image when it is
         accessed for the first time.
 
-        This method will close the file associated with the image. See
-        :ref:`file-handling` for more information.
+        If the file associated with the image was opened by Pillow, then this
+        method will close it. The exception to this is if the image has
+        multiple frames, in which case the file will be left open for seek
+        operations. See :ref:`file-handling` for more information.
 
         :returns: An image access object.
         :rtype: :ref:`PixelAccess` or :py:class:`PIL.PyAccess`


Test patch:
Repository: python-pillow/Pillow

Issue:
Incorrect description of Image.convert() function
### What did you do?


Went to Image Module in official pillow documentation  :- https://pillow.readthedocs.io/en/5.3.x/reference/Image.html

And was looking through the Image.convert() function


### What is stated

When translating a color image to black and white (mode “L”), the library uses the ITU-R 601-2 luma transform:

```
L = R * 299/1000 + G * 587/1000 + B * 114/1000
```

### What is supposed to be stated:-

When translating a color image to Greyscale (mode “L”), the library uses the ITU-R 601-2 luma transform:

```
L = R * 299/1000 + G * 587/1000 + B * 114/1000
```

### Explaination:-

Luma transform takes in RGB input and convert it into a 0 - 255 range single channel output (Greyscale)
given that the values of RGB are in range of 0 - 255.

So according to the luma transform, we will get a result in a range 0-255, but a black and white image could only have 2 discrete pixel values, namely black ( 0 ) or white ( 1 ). 

This states that the above formula provided does not result into a black and white image, but rather in a greyscale image. Even the mode stated inside the parenthesis is for greyscale conversion. 

### Suggestion

An edit on the corresponding lines of the convert function description


### Screenshot of the description :-

![incorrectinfo](https://user-images.githubusercontent.com/46196312/50486769-8f4aba00-099f-11e9-85e9-a920bb727905.png)


Discussion:

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -881,7 +881,7 @@ def convert(self, mode=None, matrix=None, dither=None,
         "L", "RGB" and "CMYK." The **matrix** argument only supports "L"
         and "RGB".
 
-        When translating a color image to black and white (mode "L"),
+        When translating a color image to greyscale (mode "L"),
         the library uses the ITU-R 601-2 luma transform::
 
             L = R * 299/1000 + G * 587/1000 + B * 114/1000


Test patch:
Repository: python-pillow/Pillow

Issue:
Loading pickled images fails for png files with iTXt chunks
Trying to load a pickled image fails if it's a png file with iTXt chunks. The `iTXt.__new__(cls, text, lang, tkey)` method appears to be called with only two arguments when `Image.__setstate__` is called. All of the test_pickle.py tests pass but I don't think any of those images include iTXt chunks. 

Code to reproduce the issue:

``` python
from PIL import Image
import pickle

im = Image.open('example.png')
p = pickle.dumps(im)
new_im = pickle.loads(p)
```

The output of running that code:

```
Traceback (most recent call last):
  File "example.py", line 6, in <module>
    new_im = pickle.loads(p)
TypeError: __new__() missing 2 required positional arguments: 'lang' and 'tkey'
```

The image that was being used:
![example](https://cloud.githubusercontent.com/assets/8236564/9995147/63b1e5b0-6035-11e5-91f5-97ddb9d1e876.png)



Discussion:
The example works as expected for me, on OS X with Python 2.7.9, and the contents of each image look the same using `.show()`.

What Python version and OS are you using?

I get the error on Linux with Python 3.4.3.


Resolving patch:
diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py
--- a/src/PIL/PngImagePlugin.py
+++ b/src/PIL/PngImagePlugin.py
@@ -192,7 +192,7 @@ class iTXt(str):
 
     """
     @staticmethod
-    def __new__(cls, text, lang, tkey):
+    def __new__(cls, text, lang=None, tkey=None):
         """
         :param cls: the class to use when creating the instance
         :param text: value for this key


Test patch:
diff --git a/Tests/images/itxt_chunks.png b/Tests/images/itxt_chunks.png
new file mode 100644
Binary files /dev/null and b/Tests/images/itxt_chunks.png differ
diff --git a/Tests/test_pickle.py b/Tests/test_pickle.py
--- a/Tests/test_pickle.py
+++ b/Tests/test_pickle.py
@@ -67,9 +67,13 @@ def test_pickle_p_mode(self):
                 "Tests/images/non_zero_bb.png",
                 "Tests/images/non_zero_bb_scale2.png",
                 "Tests/images/p_trns_single.png",
-                "Tests/images/pil123p.png"
+                "Tests/images/pil123p.png",
+                "Tests/images/itxt_chunks.png"
         ]:
-            self.helper_pickle_string(pickle, test_file=test_file)
+            for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
+                self.helper_pickle_string(pickle,
+                                          protocol=protocol,
+                                          test_file=test_file)
 
     def test_pickle_l_mode(self):
         # Arrange
Repository: python-pillow/Pillow

Issue:
js/script.js is missing in the source tarball
js/script.js is missing in the 5.4.1 source tarball. Apparently it was added in 5.4.x.  It's needed for the built documentation.



Discussion:
The script was added to 'docs/_static' in #3476. However, [`prune docs/_static` was added to the manifest](https://github.com/python-pillow/Pillow/blob/8e4d547195e76da2005284e546cacb8b8172d3f0/MANIFEST.in#L17) in #1656

@wiredfool what was your thinking behind pruning docs/_static?
Pretty sure that's because the contents were generated from the doc build.

Resolving patch:
diff --git a/docs/conf.py b/docs/conf.py
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -140,7 +140,7 @@
 # Add any paths that contain custom static files (such as style sheets) here,
 # relative to this directory. They are copied after the builtin static files,
 # so a file named "default.css" will overwrite the builtin "default.css".
-html_static_path = ['_static']
+html_static_path = ['_static', 'resources']
 
 # Add any extra paths that contain custom files (such as robots.txt or
 # .htaccess) here, relative to this directory. These files are copied


Test patch:
Repository: python-pillow/Pillow

Issue:
File descriptor closed after with block despite _exclusive_fp being False
### What did you do?
```python
import PIL.Image

file = open('r.png', 'rb')
with PIL.Image.open(file) as image:
    print('Exclusive FP =', image._exclusive_fp)

try:
    file.read()
    print('OK')
except ValueError as e:
    print('BAD', e)
```

### What did you expect to happen?
```
Exclusive FP = False
OK
```

### What actually happened?
```
Exclusive FP = False
BAD read of closed file
```
### What are your OS, Python and Pillow versions?

* OS: Windows 10
* Python: 3.7
* Pillow: 5.4.1

---

I believe this is what's causing https://github.com/LonamiWebs/Telethon/issues/1121. I can work around it by not using the `with` block:

```python
file = open('r.png', 'rb')
image = PIL.Image.open(file)
print('Exclusive FP =', image._exclusive_fp)

try:
    file.read()
    print('OK')
except ValueError as e:
    print('BAD', e)
```
...which shows OK, but it sounds like an issue here. Sorry if it's a duplicate, couldn't find anything similar.


Discussion:

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -578,7 +578,12 @@ def __enter__(self):
         return self
 
     def __exit__(self, *args):
-        self.close()
+        if hasattr(self, "_close__fp"):
+            self._close__fp()
+        if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp')
+           and self.fp and self._exclusive_fp):
+            self.fp.close()
+        self.fp = None
 
     def close(self):
         """
@@ -610,12 +615,7 @@ def close(self):
 
     if sys.version_info.major >= 3:
         def __del__(self):
-            if hasattr(self, "_close__fp"):
-                self._close__fp()
-            if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp')
-               and self.fp and self._exclusive_fp):
-                self.fp.close()
-            self.fp = None
+            self.__exit__()
 
     def _copy(self):
         self.load()


Test patch:
diff --git a/Tests/test_image_load.py b/Tests/test_image_load.py
--- a/Tests/test_image_load.py
+++ b/Tests/test_image_load.py
@@ -28,3 +28,10 @@ def test_contextmanager(self):
             os.fstat(fn)
 
         self.assertRaises(OSError, os.fstat, fn)
+
+    def test_contextmanager_non_exclusive_fp(self):
+        with open("Tests/images/hopper.gif", "rb") as fp:
+            with Image.open(fp):
+                pass
+
+            self.assertFalse(fp.closed)
Repository: python-pillow/Pillow

Issue:
Quantize should update palette
### What did you do?
```python 
# t.png - png image with transparency.
im = Image.open('t.png')
print (im.info['transparency'])
# '\x00\x0f#Af\x99\xce\xfc\xfd\xc1\xdd\x86\xfdk\x94>^#\xcd\xc6\xe2\xb8\xda\xb0\x8c\xaba'
im_rgba = im.convert()
im_p = im_rgba.quantize()
new_im_rgba = im_p.convert()
print (new_im_rgba.mode)
```
### What did you expect to happen?
'RGBA'
### What actually happened?
'RGB'
### What versions of Pillow and Python are you using?
latest master

I noticed [there is code to handle](https://github.com/python-pillow/Pillow/pull/537/files#diff-3fef5f960d5bd8bdfb9c6fadcf96af22R737) that in `convert` for RGB -> P conversion. Looks like it was used for RGBA->P conversion as well, but then RGBA->P got [handled by call to quantize directly](https://github.com/python-pillow/Pillow/pull/573).



Discussion:
@wiredfool should Quantize update palette? :smile:

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -1096,7 +1096,13 @@ def quantize(self, colors=256, method=None, kmeans=0, palette=None, dither=1):
             im = self.im.convert("P", dither, palette.im)
             return self._new(im)
 
-        return self._new(self.im.quantize(colors, method, kmeans))
+        im = self._new(self.im.quantize(colors, method, kmeans))
+
+        from . import ImagePalette
+        mode = im.im.getpalettemode()
+        im.palette = ImagePalette.ImagePalette(mode, im.im.getpalette(mode, mode))
+
+        return im
 
     def copy(self):
         """


Test patch:
diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py
--- a/Tests/test_image_quantize.py
+++ b/Tests/test_image_quantize.py
@@ -38,9 +38,10 @@ def test_octree_quantize(self):
 
     def test_rgba_quantize(self):
         image = hopper('RGBA')
-        image.quantize()
         self.assertRaises(ValueError, image.quantize, method=0)
 
+        self.assertEqual(image.quantize().convert().mode, "RGBA")
+
     def test_quantize(self):
         image = Image.open('Tests/images/caption_6_33_22.png').convert('RGB')
         converted = image.quantize()
Repository: python-pillow/Pillow

Issue:
Image.putalpha fails on P -> PA
Image.putalpha should handle the promotion of `P` to `PA` images similarly to `L` -> `LA`.




Discussion:
I've created PR #3726 to resolve this.

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -247,7 +247,7 @@ def isImageType(t):
     "L": ("L", "L", ("L",)),
     "I": ("L", "I", ("I",)),
     "F": ("L", "F", ("F",)),
-    "P": ("RGB", "L", ("P",)),
+    "P": ("P", "L", ("P",)),
     "RGB": ("RGB", "L", ("R", "G", "B")),
     "RGBX": ("RGB", "L", ("R", "G", "B", "X")),
     "RGBA": ("RGB", "L", ("R", "G", "B", "A")),
@@ -1559,7 +1559,7 @@ def putalpha(self, alpha):
 
         self._ensure_mutable()
 
-        if self.mode not in ("LA", "RGBA"):
+        if self.mode not in ("LA", "PA", "RGBA"):
             # attempt to promote self to a matching alpha mode
             try:
                 mode = getmodebase(self.mode) + "A"
@@ -1568,7 +1568,7 @@ def putalpha(self, alpha):
                 except (AttributeError, ValueError):
                     # do things the hard way
                     im = self.im.convert(mode)
-                    if im.mode not in ("LA", "RGBA"):
+                    if im.mode not in ("LA", "PA", "RGBA"):
                         raise ValueError  # sanity check
                     self.im = im
                 self.pyaccess = None
@@ -1576,7 +1576,7 @@ def putalpha(self, alpha):
             except (KeyError, ValueError):
                 raise ValueError("illegal image mode")
 
-        if self.mode == "LA":
+        if self.mode in ("LA", "PA"):
             band = 1
         else:
             band = 3


Test patch:
diff --git a/Tests/test_image_mode.py b/Tests/test_image_mode.py
--- a/Tests/test_image_mode.py
+++ b/Tests/test_image_mode.py
@@ -42,7 +42,7 @@ def check(mode, *result):
             self.assertEqual(signature, result)
         check("1", "L", "L", 1, ("1",))
         check("L", "L", "L", 1, ("L",))
-        check("P", "RGB", "L", 1, ("P",))
+        check("P", "P", "L", 1, ("P",))
         check("I", "L", "I", 1, ("I",))
         check("F", "L", "F", 1, ("F",))
         check("RGB", "RGB", "L", 3, ("R", "G", "B"))
diff --git a/Tests/test_image_putalpha.py b/Tests/test_image_putalpha.py
--- a/Tests/test_image_putalpha.py
+++ b/Tests/test_image_putalpha.py
@@ -28,6 +28,13 @@ def test_promote(self):
         self.assertEqual(im.mode, 'LA')
         self.assertEqual(im.getpixel((0, 0)), (1, 2))
 
+        im = Image.new("P", (1, 1), 1)
+        self.assertEqual(im.getpixel((0, 0)), 1)
+
+        im.putalpha(2)
+        self.assertEqual(im.mode, 'PA')
+        self.assertEqual(im.getpixel((0, 0)), (1, 2))
+
         im = Image.new("RGB", (1, 1), (1, 2, 3))
         self.assertEqual(im.getpixel((0, 0)), (1, 2, 3))
Repository: python-pillow/Pillow

Issue:
Transforms and enhancements across all frames
It would be nice if transforms and enhancements could be applied on animations.


Discussion:
What sort of transforms and enhancements?
for example.
https://pillow.readthedocs.io/en/stable/handbook/tutorial.html#geometrical-transforms

https://pillow.readthedocs.io/en/stable/handbook/tutorial.html#image-enhancement
So when you say that you'd like them to be applied on animations, what you mean is that you'd like to be able to apply them across all frames of an image at once?
yes. It seems to me that a lot of application would want a functionality like that.
Okay. What about a generic way to apply a method to all frames of an image?

So, for example -

```python
from PIL import Image, ImageSequence
im = Image.open('Tests/images/g4-multi.tiff')
ims = ImageSequence.all_frames(im, lambda im_frame: im_frame.rotate(180))
ims[0].save('out.tiff', save_all=True, append_images=ims[1:])
```

I've created this in PR #3778
lol, i was expecting a solution to this in....~5-10 years XD
@radarhere is that good 😄 

Resolving patch:
diff --git a/src/PIL/ImageSequence.py b/src/PIL/ImageSequence.py
--- a/src/PIL/ImageSequence.py
+++ b/src/PIL/ImageSequence.py
@@ -54,3 +54,25 @@ def __next__(self):
 
     def next(self):
         return self.__next__()
+
+
+def all_frames(im, func=None):
+    """
+    Applies a given function to all frames in an image or a list of images.
+    The frames are returned as a list of separate images.
+
+    :param im: An image, or a list of images.
+    :param func: The function to apply to all of the image frames.
+    :returns: A list of images.
+    """
+    if not isinstance(im, list):
+        im = [im]
+
+    ims = []
+    for imSequence in im:
+        current = imSequence.tell()
+
+        ims += [im_frame.copy() for im_frame in Iterator(imSequence)]
+
+        imSequence.seek(current)
+    return [func(im) for im in ims] if func else ims


Test patch:
diff --git a/Tests/test_imagesequence.py b/Tests/test_imagesequence.py
--- a/Tests/test_imagesequence.py
+++ b/Tests/test_imagesequence.py
@@ -74,3 +74,25 @@ def test_palette_mmap(self):
         im.seek(0)
         color2 = im.getpalette()[0:3]
         self.assertEqual(color1, color2)
+
+    def test_all_frames(self):
+        # Test a single image
+        im = Image.open("Tests/images/iss634.gif")
+        ims = ImageSequence.all_frames(im)
+
+        self.assertEqual(len(ims), 42)
+        for i, im_frame in enumerate(ims):
+            self.assertFalse(im_frame is im)
+
+            im.seek(i)
+            self.assert_image_equal(im, im_frame)
+
+        # Test a series of images
+        ims = ImageSequence.all_frames([im, hopper(), im])
+        self.assertEqual(len(ims), 85)
+
+        # Test an operation
+        ims = ImageSequence.all_frames(im, lambda im_frame: im_frame.rotate(90))
+        for i, im_frame in enumerate(ims):
+            im.seek(i)
+            self.assert_image_equal(im.rotate(90), im_frame)