You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何加速Docker环境中ImageMagick的-kmeans运算?

ImageMagick 7 Kmeans在Docker环境中运行速度极慢的问题排查

我正在使用ImageMagick 7的kmeans功能,执行以下命令:

time convert in.png -kmeans 20 out.png

该命令在本地机器上仅耗时1.7秒,但在Docker环境(本地Docker和AWS Lambda Docker均测试过)中运行时速度大幅变慢,耗时长达48.8秒。无论使用哪种镜像,Docker环境下的运行速度始终很慢。

我使用的Docker基础镜像是FROM amazon/aws-lambda-nodejs:20.2024.02.07.18 as build,由于AmazonLinux2023通过dnf只能获取到ImageMagick 6版本的包,所以我通过源码编译安装了ImageMagick 7。

想请教是不是编译过程中出现了错误?比如缺失某些依赖库或者未开启性能优化选项?

Docker环境的ImageMagick版本信息

bash-5.2# convert -version
Version: ImageMagick 7.1.1-29 Q16-HDRI x86_64 cfc71f0aa:20240225 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI 
Delegates (built-in): bzlib fontconfig freetype jng jpeg lzma png xml zlib
Compiler: gcc (11.4)

本地机器的ImageMagick版本信息

Version: ImageMagick 7.1.1-25 Q16-HDRI aarch64 21883 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(5.0) 
Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib zstd
Compiler: gcc (4.2)

Docker编译步骤

Dockerfile内容

FROM amazon/aws-lambda-nodejs:20.2024.02.07.18 as build
RUN dnf -y update && dnf install -y g++ gdb ninja-build gcc cpp cmake make automake autoconf chkconfig pkgconfig clang clang-libs gcc-c++ dos2unix bzip2 bzip2-libs zlib-devel libpng libpng-tools libpng-devel openjpeg2 openjpeg2-tools openjpeg2-devel libwebp-devel giflib-devel fftw-libs fontconfig-devel libtool xz xz-libs tar bc && dnf clean all
ADD build /root/build
RUN /root/build/imagemagick.sh

编译脚本imagemagick.sh内容

#!/usr/bin/env bash
set -e

# libjpg

cd /root
curl https://github.com/winlibs/libjpeg/archive/refs/tags/libjpeg-9c.tar.gz -L -o tmp-libjpeg.tar.gz
tar xf tmp-libjpeg.tar.gz
cd libjpeg*

dos2unix *
dos2unix -f configure
chmod +x configure

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

dos2unix -f libtool

make
make install

# libpng

cd /root
curl http://prdownloads.sourceforge.net/libpng/libpng-1.6.37.tar.xz -L -o tmp-libpng.tar.xz
tar xf tmp-libpng.tar.xz
cd libpng*

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS=-L/root/build/cache/lib \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/build/cache

make
make install

# imagemagick

cd /root
curl https://github.com/ImageMagick/ImageMagick/archive/refs/tags/7.1.1-29.tar.gz -L -o tmp-imagemagick.tar.gz
tar xf tmp-imagemagick.tar.gz
cd ImageMagick*

PKG_CONFIG_PATH=/root/build/cache/lib/pkgconfig \
  ./configure \
    CPPFLAGS=-I/root/build/cache/include \
    LDFLAGS="-L/root/build/cache/lib -lstdc++" \
    --disable-dependency-tracking \
    --disable-shared \
    --enable-static \
    --prefix=/root/result \
    --enable-delegate-build \
    --disable-installed \
    --without-modules \
    --disable-docs \
    --without-magick-plus-plus \
    --without-perl \
    --without-x \
    --disable-openmp \
    --with-openjp2=no \
    --with-webp=no \
    --with-tiff=no

make clean
make all
make install

编辑补充:了解到OpenMP可提升性能后,我尝试移除--disable-openmp重新编译,但有无该选项的运行耗时完全一致。


内容的提问来源于stack exchange,提问作者dtbaker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 09:36:04