Repository: python-pillow/Pillow

Issue:
 No preview of .ico file on Windows 10
### What did you do and what did you expect to happen?
I ran this code and tried to preview the `.ico` file created in Windows folder explorer, Windows Photo Viewer and  Windows Photo: 
```python
from PIL import Image
img = Image.new('RGB', (50, 50), color = (130, 173, 242))  
img.save('pillow.ico')
```
### What actually happened?
Although I can preview others `.ico` files on my computer, I can't preview the  `.ico` created by Pillow  (the file on the left is an `.ico` created by pillow, the one on the right is "genuine" `ico`)

- Windows folder explorer
![2019-04-05_18-02-08_explorer](https://user-images.githubusercontent.com/15843700/55640942-f6f97800-57cc-11e9-8488-d56a95b3d90c.png)


- Windows Photo Viewer  
![2019-04-05_17-55-58_explorer](https://user-images.githubusercontent.com/15843700/55640583-3b384880-57cc-11e9-9ad6-f29ef65db6e2.png)

- Windows Photo
![2019-04-05_17-55-09_explorer](https://user-images.githubusercontent.com/15843700/55640593-40959300-57cc-11e9-97db-89748cef4d82.png)

It was tested on 2 computers (same OS, Python and Pillow versions)
I can preview the `.ico` if I drag them in chrome. I can also preview them if I load them as an icon for a systray app. 


### What are your OS, Python and Pillow versions?

* OS: Windows 10
* Python:  3.6
* Pillow:   5.1.0



Discussion:
Could you upload an ICO file that previews correctly, and the Pillow generated one that doesn't?
Here is a [link ](https://drive.google.com/open?id=1_ppImgx4S7riMuBfNpWUJmBwF4J5z6U9)to the `.ico`.
 
 `pillow.ico` is the one that isn't working, `190.ico` is the one that is working (it comes from Windows shell32.dll) 

I can see the preview of the ico in google drive but I am still unable to see them in Windows folder explorer, Windows Photo Viewer, Windows Photo.


Thanks. Looking at the two files, pillow.ico has the sizes (16, 16), (24, 24), (32, 32) and (48, 48), while 190.ico just has (40, 40).

My guess is that the problem you are seeing is due to the lack of 40x40 size in the Pillow image. I have generated two images, one with an additional (40, 40), and one without. Could you test and report back if 'pillow_without40x40.ico' fails and 'pillow_with40x40.ico' previews correctly?

[40x40.zip](https://github.com/python-pillow/Pillow/files/3167927/40x40.zip)

If I'm right, then your immediate workaround for this problem is just specifying this as a size when saving.

```python
from PIL import Image
original = Image.new('RGB', (50, 50), color = (130, 173, 242))  

original.save('pillow.ico')
reloaded = Image.open('pillow.ico')
print(reloaded.info['sizes'])  # The default {(16, 16), (24, 24), (48, 48), (32, 32)}

original.save('pillow.ico', sizes=[(40, 40)])
reloaded = Image.open('pillow.ico')
print(reloaded.info['sizes'])  # {(40, 40)}
```
I downloaded your images and I can't preview any of them. I also tried your code, the image are created, the printed size are correct but I still can't preview them. 

When I create `.ico` with several size in 1 file on this [website](https://icoconvert.com/image_to_icon_converter/), I don't have any problem previewing the file. So I think the problem comes from somewhere else. 
Using your original code, I can see a preview in Windows Explorer, but not in Windows Photo Viewer.

Using mode `RGBA` I can't reproduce this (the icon is always visible):

```python
from PIL import Image
original = Image.new('RGBA', (50, 50), color = (130, 173, 242))  
original.save('pillow.ico')
```
Yes this is working! Why does changing the color mode fix this?
Windows has very specific requirements on ICO files, one of them being the
RGBA color mode.
Would it be possible for Pillow to automatically convert `ICO` file to `RGBA`? (or at least to add a comment in the console when we uses `RGB` with `ICO` so that we know it's better to use `RGBA`)
I wasn't home when you asked earlier, so here's a quote from [Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/uxguide/vis-icons) (scroll almost to the end, under step 5 of subheading Icon Development):

> Icon (.ico format) files must contain the 4- and 8-bit versions, as well as the 24-bit + alpha.

This to me suggests that RGB is a perfectly valid mode for image frames, but Windows will ignore it and look for a different frame.

Pillow currently doesn't support saving a single ICO file with multiple different image modes. Perhaps it would be a good idea to add automatic conversion to RGBA.
The Microsoft document you linked to talks about creating icons for Windows applications, but this isn't the only application of the ICO format - there are also favicons. If there are users happily creating ICO files for favicons, I don't think their result needs to be changed - is a good alternative for a note to be made in the Pillow documentation instead?
I just tried this in Internet Explorer 11 and Microsoft Edge and both just show an error box for RGB, but render correctly with RGBA.

Whether or not automatic mode conversion is added, I think there should definitely be a note in the docs; it should also mention that the current Pillow implementation saves ICOs internally as PNGs, not DIBs (support was only added in Windows Vista).

Resolving patch:
diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py
--- a/src/PIL/IcoImagePlugin.py
+++ b/src/PIL/IcoImagePlugin.py
@@ -267,7 +267,8 @@ class IcoImageFile(ImageFile.ImageFile):
     Handles classic, XP and Vista icon formats.
 
     When saving, PNG compression is used. Support for this was only added in
-    Windows Vista.
+    Windows Vista. If you are unable to view the icon in Windows, convert the
+    image to "RGBA" mode before saving.
 
     This plugin is a refactored version of Win32IconImagePlugin by Bryan Davis
     <casadebender@gmail.com>.


Test patch: