def extension_replace(path,extension): for i in range(1,len(path)): if (path[-i] == '.'): new_path = path.replace(path[-i+1:],extension) break return new_path path = '1.jpg' img = PIL.Image.open(path) size = (128,128) icon = img.resize(size,PIL.Image.LANCZOS) #提取像素 name = extension_replace(path,'ico') #替换文件后缀名 icon.save(name)