Struggling when converting PNG to ICO file?

Question:

Hi guys please help me i can’t solve this, i have this image

enter image description here

When i try to convert it to ico using

from PIL import Image,ImageTk
png = Image.open("converter.png")
png.save("icon.ico", format='ICO',quality=100)

And i use the icon for a shortcut in the desktop the result is this

enter image description here

It’s stretched to the top and the bottom. How to solve this and keep the same aspect ratio of the image? Thanks

It doesn’t work even with

from PIL import Image,ImageTk
png = Image.open("converter.png")
png.resize((64,64), resample=0)
png.save("icon.ico", format='ICO',quality=100)
Asked By: Forty966699669

||

Answers:

You need to use Image.resize(size, resample=0) function to make .ico file squared (equal width and height.

In your case it would look something like this:

png.resize((64,64), resample=0)

You can fiddle with resample argument to make it look better.

resample – An optional resampling filter.

This can be one of PIL.Image.NEAREST (use nearest neighbour), >PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC (cubic
spline interpolation), or PIL.Image.LANCZOS (a high-quality
downsampling filter).

If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST.

Answered By: LordNani

You need to resize the image to a square, but you don’t want to stretch or compress the image.

You can create a new square image and place your original image in the center.

from PIL import Image
png = Image.open("converter.png")
size = png.size
ico = Image.new(mode="RGBA", size=(max(size), max(size)), color=(0, 0, 0, 0))
ico.paste(png, (int((max(size)-size[0])/2), int((max(size)-size[1])/2)))
ico.save("icon.ico", format='ICO', quality=100)
Answered By: Tim_08
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.