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
查看 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 内
几个线程怎么跑了这么内存?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.