尝试在AWS Lambda上运行Wand以进行一些简单的图像处理。
到目前为止我已经:
从源代码
安装了imagemagickwget ImageMagick
cd ImageMagick-...
./configure --prefix=/home/ec2-user/im --without-x
make && make install
将MAGICK_HOME设置为/home/ec2-user/im/
pip install wand
但是当我尝试导入wand.image时,我得到了:
import wand.image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ec2-user/cv/local/lib64/python2.7/site-packages/wand/image.py", line 20, in <module>
from .api import MagickPixelPacket, libc, libmagick, library
File "/home/ec2-user/cv/local/lib64/python2.7/site-packages/wand/api.py", line 206, in <module>
'Try to install:\n ' + msg)
ImportError: MagickWand shared library not found.
You probably had not installed ImageMagick library.
Try to install:
http://docs.wand-py.org/en/latest/guide/install.html
当我执行yum install ImageMagick
时它工作正常,但由于我想捆绑它,我认为我需要包含二进制文件并将魔杖安装指向它。
有什么想法?
答案 0 :(得分:2)
这可能对所问的问题没有帮助,但是如果期望的目标是进行一些简单的文件转换(将pdf转换为jpg / png等),则可以绕过魔杖并直接调用ImageMagick / GhostScript(这些文件与Python3.6 AWS环境)。
例如
# converts local pdf to jpg using ImageMagick
os.system("convert -density 200x200 /tmp/in_pdf.pdf /tmp/out_jpg.jpg")
# converts local pdf to jpg using GhostScript
os.system(
"gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT "
"-dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "
"-sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150 "
"-sOutputFile=/tmp/out_jpg-%d.jpg /tmp/in_pdf.pdf"
)
我使用/tmp
作为文件位置的原因是因为lambda允许您在运行lambda函数的过程中将最大500MB的文件存储在那里。
我希望这对某人有帮助!
答案 1 :(得分:1)
由于其他人现在感兴趣,我解决了这个问题,目前正在管理我的lambda项目,类似于下面的内容。
文件夹结构
-project
-init_libs.py
-lambda_function.py
-libs
-libMagickWand-6.Q16.so
-libMagickCore-6.Q16.so
-libMagick++-6.Q16.so
-site-packages
-wand
-other_modules
init_libs.py
加载Wand等本机库。
import sys
import os
from ctypes import cdll
from os.path import join
# manually load libraries and set paths so native libraries can be used.
exec_dir = os.getcwd()
sys.path.append(join(exec_dir, 'site-packages'))
# required for Wand
os.environ['MAGICK_HOME'] = exec_dir
lib_dir = join(exec_dir, 'lib')
# add lib to sys path for general loading
sys.path.append(lib_dir)
lambda_function.py
现在可以导入魔杖
from wand.image import Image
print Image
答案 2 :(得分:0)
受到 https://gist.github.com/bensie/56f51bc33d4a55e2fc9a#gistcomment-3133859 的启发,我能够成功地将 ImageMagick 和 Ghostscript 打包到 Python 3.8 兼容的 AWS Lambda 层中,并按照以下说明使用 wand
将 PDF 中的页面转换为 PNG:< /p>
amzn2-ami-hvm-2.0.20210126.0-x86_64-gp2
。wget https://download.imagemagick.org/ImageMagick/download/ImageMagick-6.9.11-60.tar.gz
tar zxvf ImageMagick-6.9.11-60.tar.gz
cd
进入解压后的文件夹。
cd ImageMagick-6.9.11-60
policy.xml
文件以允许将 PDF 转换为 PNG。
nano config/policy.xml
我复制粘贴了以下内容,但您可以根据需要进行修改。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
<!ELEMENT policymap (policy)+>
<!ELEMENT policy (#PCDATA)>
<!ATTLIST policy domain (delegate|coder|filter|path|resource) #IMPLIED>
<!ATTLIST policy name CDATA #IMPLIED>
<!ATTLIST policy rights CDATA #IMPLIED>
<!ATTLIST policy pattern CDATA #IMPLIED>
<!ATTLIST policy value CDATA #IMPLIED>
]>
<!--
Configure ImageMagick policies.
Domains include system, delegate, coder, filter, path, or resource.
Rights include none, read, write, and execute. Use | to combine them,
for example: "read | write" to permit read from, or write to, a path.
Use a glob expression as a pattern.
Suppose we do not want users to process MPEG video images:
<policy domain="delegate" rights="none" pattern="mpeg:decode" />
Here we do not want users reading images from HTTP:
<policy domain="coder" rights="none" pattern="HTTP" />
Lets prevent users from executing any image filters:
<policy domain="filter" rights="none" pattern="*" />
The /repository file system is restricted to read only. We use a glob
expression to match all paths that start with /repository:
<policy domain="path" rights="read" pattern="/repository/*" />
Let's prevent possible exploits by removing the right to use indirect reads.
<policy domain="path" rights="none" pattern="@*" />
Any large image is cached to disk rather than memory:
<policy domain="resource" name="area" value="1GB"/>
Define arguments for the memory, map, area, width, height, and disk resources
with SI prefixes (.e.g 100MB). In addition, resource policies are maximums
for each instance of ImageMagick (e.g. policy memory limit 1GB, -limit 2GB
exceeds policy maximum so memory limit is 1GB).
-->
<policymap>
<!-- <policy domain="resource" name="temporary-path" value="/tmp"/> -->
<policy domain="resource" name="memory" value="256MiB"/>
<policy domain="resource" name="map" value="512MiB"/>
<policy domain="resource" name="width" value="16KP"/>
<policy domain="resource" name="height" value="16KP"/>
<policy domain="resource" name="area" value="128MB"/>
<policy domain="resource" name="disk" value="1GiB"/>
<!-- <policy domain="resource" name="file" value="768"/> -->
<!-- <policy domain="resource" name="thread" value="4"/> -->
<!-- <policy domain="resource" name="throttle" value="0"/> -->
<!-- <policy domain="resource" name="time" value="3600"/> -->
<!-- <policy domain="system" name="precision" value="6"/> -->
<!-- not needed due to the need to use explicitly by mvg: -->
<!-- <policy domain="delegate" rights="none" pattern="MVG" /> -->
<!-- use curl -->
<policy domain="delegate" rights="none" pattern="URL" />
<policy domain="delegate" rights="none" pattern="HTTPS" />
<policy domain="delegate" rights="none" pattern="HTTP" />
<!-- in order to avoid to get image with password text -->
<policy domain="path" rights="none" pattern="@*"/>
<policy domain="cache" name="shared-secret" value="passphrase" stealth="true"/>
<!-- disable ghostscript format types -->
<policy domain="coder" rights="none" pattern="PS" />
<policy domain="coder" rights="none" pattern="EPI" />
<policy domain="coder" rights="read|write" pattern="PDF" />
<policy domain="coder" rights="none" pattern="XPS" />
<policy domain="coder" rights="read|write" pattern="LABEL" />
</policymap>
./configure --prefix=/var/task/imagemagick --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared=no --enable-static=yes --with-modules --with-perl=no --with-x=no --with-gslib=no --with-lcms --without-rsvg --with-xml --without-dps --disable-hdri --with-quantum-depth=8 --disable-openmp
make
sudo make install
.so
文件。
mkdir lib
cd /usr/lib64/
cp -L libbz2.so.1 libexpat.so.1 libfontconfig.so.1 libfreetype.so.6 libgs.so.9 libjbig.so.2.0 libjpeg.so.62 liblcms2.so.2 liblzma.so.5 libpng15.so.15 libtiff.so.5 libxml2.so.2 libMagickCore-6.Q16.so.6 libMagickWand-6.Q16.so.6 libXext.so.6 libXt.so.6 libltdl.so.7 libSM.so.6 libICE.so.6 libX11.so.6 libgomp.so.1 libuuid.so.1 libxcb.so.1 libXau.so.6 libMagickCore-6.Q8.so.6 libMagickWand-6.Q8.so.6 libm.so.6 libz.so.1 libjasper.so.1 /home/ec2-user/lib/
cp -r ImageMagick-6.9.10/ ImageMagick-6.9.11/ /home/ec2-user/lib/
cd /home/ec2-user
tar zcf lib.tar.gz lib/
将 lib.tar.gz
文件从服务器复制到您的本地计算机。cd /var/task/imagemagick
sudo tar zcf bin.tar.gz bin/
cp bin.tar.gz /home/ec2-user/bin.tar.gz
将 bin.tar.gz
文件从服务器复制到您的本地计算机。cd /etc/
sudo tar zcf etc.tar.gz ImageMagick-6/
cp etc.tar.gz /home/ec2-user/etc.tar.gz
将 etc.tar.gz
文件从服务器复制到您的本地计算机。*.tar.gz
文件的内容。bin/
文件夹中,并将其重命名为 gs
。运行 chmod +x bin/gs
使其可执行。lib
、bin
和 etc
- 文件夹压缩为 ZIP 文件。 ZIP 文件的树结构看起来像
file.zip/
|-- bin
| |-- convert
| |-- ...
| `-- gs
|-- etc
| `-- ImageMagick-6
| |-- coder.xml
| |-- ...
| `-- type.xml
`-- lib
|-- ImageMagick-6.9.10
| |-- config-Q16
| | `-- configure.xml
| `-- modules-Q16
| |-- coders
| | |-- aai.la
| | |-- ...
| | `-- yuv.so
| `-- filters
| |-- analyze.la
| `-- analyze.so
|-- ImageMagick-6.9.11
| |-- config-Q8
| | `-- configure.xml
| `-- modules-Q8
| |-- coders
| | |-- aai.la
| | |-- ...
| | `-- yuv.so
| `-- filters
| |-- analyze.la
| `-- analyze.so
|-- libICE.so.6
|-- ...
`-- libz.so.1
我在文件夹包含 2 个以上文件的地方使用了 ...
来表示存在更多文件。wand
可以找到 ImageMagick。
import os
os.environ["PATH"] = f"/opt/bin:{os.environ['PATH']}"
os.environ["LD_LIBRARY_PATH"] = f"/opt/lib:{os.environ['LD_LIBRARY_PATH']}"
os.environ["MAGICK_HOME"] = "/opt/"
os.environ["WAND_MAGICK_LIBRARY_SUFFIX"] = "-6.Q8"
os.environ["MAGICK_CONFIGURE_PATH"] = "/opt/etc/ImageMagick-6/"
os.environ["MAGICK_CODER_MODULE_PATH"] = "/opt/lib/ImageMagick-6.9.11/modules-Q8/coders/"
注意:如果未压缩的 ZIP 文件过大并且您达到 AWS Lambda 大小限制,请从 bin/
文件夹中删除您不需要的二进制文件。就我而言,我只保留了 Magick-config
、MagickCore-config
、MagickWand-config
、Wand-config
、convert
和 gs
,并删除了其他人。