python等比缩放图片

import imghdr
import time
from io import BytesIO

import pyarrow.parquet as pq
from PIL import Image


def resize_image(img, max_side_length=768):
    # 打开图像文件
    width, height = img.size
    print("原:宽X高", width, "x", height)
    # 计算缩放后的尺寸
    scale_factor = max_side_length / max(width, height)
    new_width = int(width * scale_factor)
    new_height = int(height * scale_factor)

    # 缩放图像
    resized_img = img.resize((new_width, new_height))
    return resized_img


file1 = "/Users/chennan/Downloads/2.parquet"
df = pq.read_pandas(file1).to_pandas()
select_df = df.loc[:100]
for index, row in select_df.iterrows():
    url = row['URL']
    buff = BytesIO(url)
    img = Image.open(buff)
    width, height = img.size
    if max(width, height) < 768:
        continue
    resize_img = resize_image(img)
    buff.seek(0)
    img_type = imghdr.what(buff)
    print(resize_img.size, img_type)
    timestamp = int(time.time())
    resize_img.save(f"{timestamp}.{img_type}")
    img.save(f"原_{timestamp}.{img_type}")
    time.sleep(0.1)

本文作者:公众号python学习开发

本文链接:https://www.cnblogs.com/c-x-a/p/17195870.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   公众号python学习开发  阅读(133)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
历史上的今天:
2018-03-08 云语音通讯
2018-03-08 微博信息实时监控并发邮件
2018-03-08 微博二级评论爬取
2018-03-08 爬取genome的网页和图片
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起