如何转换 base64 图像?
How do I convert a base64 image?
我正在尝试使用 ImageMagick 的 "convert" 命令行工具。我有一个 base64 编码的 png 文件,我需要将它转换为另一种格式。我正在查看 documentation and a forum discussion,这表明我应该能够使用以下语法:
convert inline:file.txt file.jpg
但是当我这样做时,我收到了这个错误信息:
convert: corrupt image `file.txt' @ error/constitute.c/ReadInlineImage/910.
我做错了什么?如何转换为读取 base64 图像文件?
更新答案
来自 ImageMagick format docs...
The inline image look similar to inline:data:;base64,/9j/4AAQSk...knrn//2Q==
. If the inline image exceeds 5000 characters, reference it from a file (e.g. inline:inline.txt
).
这暗示在使用内联格式时有两个 "gotcha"。首先,应删除任何标准的 base64 空格(unix 换行符),以便所有信息都在一行上。其次,任何超过 5000 个字符的数据都应该从文件缓冲区中读取。
# Copy data to new file, striping line-breaks & adding INLINE header. (Please advise better sed/awk.)
cat file.txt | tr -d "\r\n" | awk '{print "data:image/png;base64,"}' > file.inline
# Read file as expected
convert inline:file.inline file.jpg
原始(不是很正确)答案
"corrupt image" 消息告诉我 base64 文件中可能有空格。如果是这样,tr 实用程序将起作用。
# For example
convert rose: rose.png
# Create base64 file
openssl enc -base64 -in rose.png -out rose.txt
# Read inline & data from stdin -- after stripping whitespace
cat rose.txt | tr -d "\r\n" | convert inline:data:- out.jpg
更新的答案 - 现在我自己理解得更好了:-)
基本上,您可以像这样使用 openssl
对图像进行 base64 编码:
openssl enc -base64 -in image.png > image.b64
但是,如果您希望 ImageMagick
能够阅读它,您需要在开头添加一个小的 header,以告诉 ImageMagick
接下来的内容。 header 必须包含:
data:image/png;base64,
后跟使用上面的 openssl
命令生成的 base64 编码数据。因此,根据您的 shell 具有的功能,您可以使用 bash
:
中的复合语句来执行此操作
{ echo "data:image/png;base64,"; openssl enc -base64 -in input.png; } > image.b64
或者在Windows中像这样:
echo data:image/png;base64, > image.b64
openssl enc -base64 -in image.png >> image.b64
获得该格式的图像后,您可以继续使用 ImageMagick
处理它,如下所示:
convert inline:image.b64 result.png
对于在css中使用这个的人,添加-A
标志以在一行中输出
openssl enc -base64 -A -in image.png > image.b64
原答案
经过多次试验,我可以做到!!! :-)
从 Eric (@emcconville) 的设置开始:
# For example
convert rose: rose.png
# Create base64 file
openssl enc -base64 -in rose.png -out rose.txt
现在把这个乱七八糟的东西添加到最后一行:
{ echo "data:image/png;base64,"; cat rose.txt; } | convert inline:- out.jpg
我猜 data:image/png;base64,
不存在于由 openssl
创建的 base64 文件中,所以我创建了一个复合语句,将它和文件一起发送到 stdin
of ImageMagick
.
我正在尝试使用 ImageMagick 的 "convert" 命令行工具。我有一个 base64 编码的 png 文件,我需要将它转换为另一种格式。我正在查看 documentation and a forum discussion,这表明我应该能够使用以下语法:
convert inline:file.txt file.jpg
但是当我这样做时,我收到了这个错误信息:
convert: corrupt image `file.txt' @ error/constitute.c/ReadInlineImage/910.
我做错了什么?如何转换为读取 base64 图像文件?
更新答案
来自 ImageMagick format docs...
The inline image look similar to
inline:data:;base64,/9j/4AAQSk...knrn//2Q==
. If the inline image exceeds 5000 characters, reference it from a file (e.g.inline:inline.txt
).
这暗示在使用内联格式时有两个 "gotcha"。首先,应删除任何标准的 base64 空格(unix 换行符),以便所有信息都在一行上。其次,任何超过 5000 个字符的数据都应该从文件缓冲区中读取。
# Copy data to new file, striping line-breaks & adding INLINE header. (Please advise better sed/awk.)
cat file.txt | tr -d "\r\n" | awk '{print "data:image/png;base64,"}' > file.inline
# Read file as expected
convert inline:file.inline file.jpg
原始(不是很正确)答案
"corrupt image" 消息告诉我 base64 文件中可能有空格。如果是这样,tr 实用程序将起作用。
# For example
convert rose: rose.png
# Create base64 file
openssl enc -base64 -in rose.png -out rose.txt
# Read inline & data from stdin -- after stripping whitespace
cat rose.txt | tr -d "\r\n" | convert inline:data:- out.jpg
更新的答案 - 现在我自己理解得更好了:-)
基本上,您可以像这样使用 openssl
对图像进行 base64 编码:
openssl enc -base64 -in image.png > image.b64
但是,如果您希望 ImageMagick
能够阅读它,您需要在开头添加一个小的 header,以告诉 ImageMagick
接下来的内容。 header 必须包含:
data:image/png;base64,
后跟使用上面的 openssl
命令生成的 base64 编码数据。因此,根据您的 shell 具有的功能,您可以使用 bash
:
{ echo "data:image/png;base64,"; openssl enc -base64 -in input.png; } > image.b64
或者在Windows中像这样:
echo data:image/png;base64, > image.b64
openssl enc -base64 -in image.png >> image.b64
获得该格式的图像后,您可以继续使用 ImageMagick
处理它,如下所示:
convert inline:image.b64 result.png
对于在css中使用这个的人,添加-A
标志以在一行中输出
openssl enc -base64 -A -in image.png > image.b64
原答案
经过多次试验,我可以做到!!! :-)
从 Eric (@emcconville) 的设置开始:
# For example
convert rose: rose.png
# Create base64 file
openssl enc -base64 -in rose.png -out rose.txt
现在把这个乱七八糟的东西添加到最后一行:
{ echo "data:image/png;base64,"; cat rose.txt; } | convert inline:- out.jpg
我猜 data:image/png;base64,
不存在于由 openssl
创建的 base64 文件中,所以我创建了一个复合语句,将它和文件一起发送到 stdin
of ImageMagick
.