序言
我正在将 Django 后端的图像显示到 26 x 26px
中大小(css)<img>
标签。
我可以通过两种方式做到这一点:
- 将后端(Pillow)的大小调整为
26x26
(裁剪以保持纵横比) - 发送完整尺寸的图像并让 CSS 调整尺寸
问题
使用LANCZOS
调整大小并将结果保存为 100% 质量 Jpg 的算法,显示的图像看起来比浏览器调整大小的图像差得多。
这是为什么,有什么办法可以解决吗?
编辑:在视网膜显示屏上进行测试
编辑:双三次看起来与 Lanczos 非常相似
最佳答案
根据我的经验,PIL/Pillow(以下简称“Pillow”)对于小图像的表现与对于大图像的表现非常不同 - 不仅在其调整大小操作中,而且在一般情况下 - 但因此您不妨测试所有Pillow 提供的方法,例如:
# q.v. https://gist.github.com/fish2000/d85befaf289c664b6a9f44d1b56e57da#file-asscat-py-L129-L134
from PIL import Image
# q.v. PIL.Image constants of the same (yet uppercased) names:
interpolation_methods = frozenset({
"box",
"bilinear", "bicubic",
"hamming", "lanczos",
"nearest" })
def interpol(name):
""" Return a PIL/Pillow image interpolation method constant by name """
return getattr(Image, name.upper())
size = (26, 26)
avatar = Image.open(…) # load your source avatar image
methods = (interpol(method) for method in interpolation_methods)
scaled = (avatar.resize(size, resample=method) for method in methods)
# you can save these out for more granular inspection:
previews = list(scaled)
for preview in previews:
preview.show()
…请记住,Image.NEAREST
对于小尺寸可以产生令人惊讶的不错的结果 - 而且 Pillow 绝不是 Adobe® Photoshop™,因此不能真正的任务是复制您可能从中得到的结果。
但是,无论是否使用 CSS(或任何其他基于客户端的方法)进行缩放:如果可能的话,通过线路发送更少的字节总是更好 - 但这并不意味着它可以没完没了。就我个人而言,我是一个完美主义者,但如果时间或金钱紧迫,我不会迂腐。
关于python - 浏览器图像调整大小,优于最高 PIL 质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55244653/