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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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的网页和图片