Repository: python-pillow/Pillow

Issue:
ValueError: unknown raw mode for given image mode
### What did you do?
I tried to save a BMP Image 

### What did you expect to happen?
Have the image saved

### What actually happened?
I got a `ValueError: unknown raw mode for given image mode` error 
### What are your OS, Python and Pillow versions?

* OS:  Ubuntu 22.04 LTS
* Python:  3.10.4
* Pillow:  9.1.1

Here is the link to the [stackoverflow ](https://stackoverflow.com/questions/72648813/pillow-throws-valueerror-unknown-raw-mode-for-given-image-mode/72650168?noredirect=1#comment128353267_72650168) post with stuff i tried

```python
from io import BytesIO

from PIL import Image


def get_img_base64(filename):
    # Open Image
    image = Image.open(filename)
    # Saving the format
    format = image.format
    buffered = BytesIO()
    image.save(buffered, format)  # CRASH HERE
    img_str = base64.b64encode(buffered.getvalue())
    return "data:image/" + format + ";base64," + img_str.decode()


get_img_base64("IMG.BMP")
```


Traceback of the error : 
```python
Traceback (most recent call last):
  File "test.py", line XXX, in XXX
    imgBase64 = get_img_base64(path)
  File "test.py", line XXX, in get_img_base64
    image.save(buffered, format)
  File "python3.10/site-packages/PIL/Image.py", line 2264, in save
    self._ensure_mutable()
  File "python3.10/site-packages/PIL/Image.py", line 626, in _ensure_mutable
    self._copy()
  File "python3.10/site-packages/PIL/Image.py", line 619, in _copy
    self.load()
  File "python3.10/site-packages/PIL/ImageFile.py", line 234, in load
    err_code = decoder.decode(b"")[1]
  File "python3.10/site-packages/PIL/BmpImagePlugin.py", line 326, in decode
    self.set_as_raw(bytes(data), ("P", 0, self.args[-1]))
  File "python3.10/site-packages/PIL/ImageFile.py", line 684, in set_as_raw
    d = Image._getdecoder(self.mode, "raw", (rawmode))
  File "python3.10/site-packages/PIL/Image.py", line 435, in _getdecoder
    return decoder(mode, *args + extra)
ValueError: unknown raw mode for given image mode
```

[Link to the image to reproduce ](https://drive.google.com/file/d/19PfOdObWoxlxJZur2jBgchgGnjeOt7X1/view)

Just doing `Image.open('YOURIMAGE').convert('RGB')` causes the error



Discussion:

Resolving patch:
diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py
--- a/src/PIL/BmpImagePlugin.py
+++ b/src/PIL/BmpImagePlugin.py
@@ -321,7 +321,8 @@ def decode(self, buffer):
                     # align to 16-bit word boundary
                     if self.fd.tell() % 2 != 0:
                         self.fd.seek(1, os.SEEK_CUR)
-        self.set_as_raw(bytes(data), ("P", 0, self.args[-1]))
+        rawmode = "L" if self.mode == "L" else "P"
+        self.set_as_raw(bytes(data), (rawmode, 0, self.args[-1]))
         return -1, 0
 
 


Test patch:
diff --git a/Tests/images/hopper_rle8_greyscale.bmp b/Tests/images/hopper_rle8_greyscale.bmp
new file mode 100644
Binary files /dev/null and b/Tests/images/hopper_rle8_greyscale.bmp differ
diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py
--- a/Tests/test_file_bmp.py
+++ b/Tests/test_file_bmp.py
@@ -134,6 +134,9 @@ def test_rle8():
     with Image.open("Tests/images/hopper_rle8.bmp") as im:
         assert_image_similar_tofile(im.convert("RGB"), "Tests/images/hopper.bmp", 12)
 
+    with Image.open("Tests/images/hopper_rle8_greyscale.bmp") as im:
+        assert_image_equal_tofile(im, "Tests/images/bw_gradient.png")
+
     # This test image has been manually hexedited
     # to have rows with too much data
     with Image.open("Tests/images/hopper_rle8_row_overflow.bmp") as im: