python图像文件压缩使用pil_Python pil 压缩图片内存过高

def resize_image(self, src_path: Path, width=800):

self.echo_sub('开始压缩图片.')

chunk_size = self.config.get('chunk_size') or 5

with futures.ThreadPoolExecutor(10) as executor:

args = ((p, width) for p in src_path.glob('*'))

for result, image_size in executor.map(lambda a: resize_width(*a), args, chunksize=chunk_size):

width, height = image_size

if width > height or result.name == 'cover.jpg':

self.thumbnail.append(result)

self.echo_sub(f'{result} {width}x{height}.', fg='red')

self.echo_sub_done('图片压缩完成.')

def resize_width(src_file: Path, width, new_name=None, quality=95, sample=Image.ANTIALIAS):

dest_file = get_dest_file(src_file, new_name)

with src_file.open('r+b') as f:

with Image.open(f) as image:

img = image.copy()

img_size = img.size

if img_size[0] > width:

new_height = int(math.ceil((width / img_size[0]) * img_size[1]))

img.thumbnail((width, new_height), sample)

img.save(str(dest_file), quality=quality)

return dest_file, img.size

run log

17:52:12 - 开始压缩图片.

Killed

查看 syslog

Out of memory: Killed process 16194 (python3) total-vm:1612348kB, anon-rss:621232kB, file-rss:1664kB, shmem-rss:0kB, UID:0 pgtables:1634304kB oom_score_adj:0

单张图片 5M 内

几个线程怎么跑了这么内存?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值