Repository: python-pillow/Pillow

Issue:
Cannot identify .fits file
### What did you do?
Tried using pillow for opening/handling a .fits file for training a machine learning model. According to the documentation opening/reading fits files should be enabled? Or am I misunderstanding how a fits file should be opened? 


From Issue [4054](https://github.com/python-pillow/Pillow/issues/4054)/ PR 6056

> I've created PR https://github.com/python-pillow/Pillow/pull/6056 to resolve this. If that is merged, you should no longer have to worry about register_handler(), but can instead just Image.open("sample.fits").


### What did you expect to happen?
Not recieving a "cannot identify error" while using Image.open. Expected the function to work as with other supported file formats. The .fits files in question are not corrupted, and can be opened as normal with other software. 

### What happened?
```python
from PIL import Image
with Image.open('example.fits') as im:
    im.verify()
```

```
---------------------------------------------------------------------------
UnidentifiedImageError                    Traceback (most recent call last)
Cell In [38], line 2
      1 from PIL import FitsImagePlugin, ImageFile
----> 2 with Image.open('example.fits') as im:
      3     im.verify()

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PIL\Image.py:3186, in open(fp, mode, formats)
   3184 for message in accept_warnings:
   3185     warnings.warn(message)
-> 3186 raise UnidentifiedImageError(
   3187     "cannot identify image file %r" % (filename if filename else fp)
   3188 )

UnidentifiedImageError: cannot identify image file 'example.fits'
```
### What are your OS, Python and Pillow versions?

* OS: windows 10
* Python: 3.10
* Pillow: 9.3.0

<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->




Discussion:
It should be working as you understand it, yes.

Here's a test FITS image that we have - https://github.com/python-pillow/Pillow/blob/main/Tests/images/hopper.fits. If you download it, are you able to open it with Pillow?
- If so, then there is something different about your file that we will need to check. Are you able to upload a copy of the file in question?
- If not, then there is something odd happening with your installation. Are you quite sure you are running Pillow >= 9.1.0? From within your code, could you run
```python
from PIL import __version__
print(__version__)
```
to check?
The hopper.fits works properly on my end. Here's the example file https://github.com/rychalm/example.fits/blob/main/example.fits  Looks like the header is somewhat different between the files. Maybe there's some issue there?

Using the code provided gives me version 9.3.0

Edit to add: The example file was generated from original raw data, using astropy. That original file does not work either (none of my fits files work with pillow). Cannot really upload it as it is multiple gigabytes.

Resolving patch:
diff --git a/src/PIL/FitsImagePlugin.py b/src/PIL/FitsImagePlugin.py
--- a/src/PIL/FitsImagePlugin.py
+++ b/src/PIL/FitsImagePlugin.py
@@ -32,7 +32,7 @@ def _open(self):
             keyword = header[:8].strip()
             if keyword == b"END":
                 break
-            value = header[8:].strip()
+            value = header[8:].split(b"/")[0].strip()
             if value.startswith(b"="):
                 value = value[1:].strip()
             if not headers and (not _accept(keyword) or value != b"T"):


Test patch:
diff --git a/Tests/test_file_fits.py b/Tests/test_file_fits.py
--- a/Tests/test_file_fits.py
+++ b/Tests/test_file_fits.py
@@ -44,6 +44,12 @@ def test_naxis_zero():
             pass
 
 
+def test_comment():
+    image_data = b"SIMPLE  =                    T / comment string"
+    with pytest.raises(OSError):
+        FitsImagePlugin.FitsImageFile(BytesIO(image_data))
+
+
 def test_stub_deprecated():
     class Handler:
         opened = False