欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Python生成缩略图

Python生成缩略图

2025/5/22 10:41:57 来源:https://blog.csdn.net/qq_35204012/article/details/141171211  浏览:    关键词:Python生成缩略图

1.安装库

pip install Pillow

2.代码实现

from PIL import Image  def generate_thumbnail(input_image_path, output_image_path, thumbnail_size=(128, 128)):  """  生成图片的缩略图  :param input_image_path: 原始图片的路径  :param output_image_path: 生成的缩略图保存的路径  :param thumbnail_size: 缩略图的大小,格式为(width, height)  """  # 打开原始图片  with Image.open(input_image_path) as img:  # 使用ANTIALIAS来优化缩略图的图片质量  img.thumbnail(thumbnail_size, Image.ANTIALIAS)  # 保存缩略图  img.save(output_image_path)  # 使用示例  
input_image = 'path/to/your/image.jpg'  # 替换为你的图片路径  
output_thumbnail = 'path/to/save/thumbnail.jpg'  # 替换为你想要保存缩略图的路径  
generate_thumbnail(input_image, output_thumbnail)

3.其他说明:

默认是128 * 128的缩略图,像电脑使用的宽图一般是 1920 * 1080 这种的,一个常见的尺寸是300x169(保持16:9的宽高比),或者更小的尺寸如200x113,甚至更小到100x56。这样的尺寸既能在网页上快速加载,又能提供足够的预览信息。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词