bash 在 Mac 上使用命令行创建 PNG/JPG 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12499728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create PNG/JPG file with commandline on Mac
提问by user1683774
is there a command to create a simple PNG/JPG picture file, can be run in terminal/script command on Mac?
有没有一个命令可以创建一个简单的 PNG/JPG 图片文件,可以在 Mac 上的终端/脚本命令中运行?
For now, I'm going to create a simple one color PNG picture file, with parameter to specify wide and length. if it can put text on it, is much better.
现在,我将创建一个简单的单色 PNG 图片文件,使用参数指定宽度和长度。如果它可以在上面放文字,那就更好了。
回答by user4815162342
The command-line convertutility in the ImageMagicksuite can easily create an image of specified dimensions:
ImageMagick套件中的命令行convert实用程序可以轻松创建指定尺寸的图像:
convert -size 200x200 xc:white canvas.png
To create an image with text, use additional options:
要创建带有文本的图像,请使用其他选项:
convert -size 200x200 -gravity center -background white -fill black \
label:"text goes here" canvas.png
The ImageMagick site has many more examplesof usage.
To get ImageMagick on OSX, you can either use an automated build system such as MacPortsor run a pre-made installer package.
回答by abarnert
OS X doesn't come with a shell command for this. But you have three options.
OS X 没有为此提供 shell 命令。但是你有三个选择。
OS X does come with a way to do this in Applescript: Image Events. You could write a three-liner in Applescript—or Python or Ruby with appscript or ScriptingBridge—that does what you want, and then run that from the shell.
OS X 确实在 Applescript: Image Events 中提供了一种方法来做到这一点。您可以使用 Applescript 或 Python 或 Ruby 和 appscript 或 ScriptingBridge 编写一个三行代码来执行您想要的操作,然后从 shell 运行它。
OS X also has some pretty high-level image support built into Cocoa, which you can access from Python, Ruby, ObjC, Applescript, etc., so you could write a five-liner that way that does what you want.
OS X 还在 Cocoa 中内置了一些非常高级的图像支持,您可以从 Python、Ruby、ObjC、Applescript 等访问这些图像,因此您可以按照自己的意愿编写一个五行代码。
Or you could use a third-party tool that gives you what you want. ImageMagick, as suggested by user4815162342, is probably the best one.
或者您可以使用第三方工具来满足您的需求。正如 user4815162342 所建议的那样,ImageMagick 可能是最好的。
(I wouldn't even mention the first two possibilities if you posted on SU rather than SO.)
(如果你在 SU 而不是 SO 上发帖,我什至不会提到前两种可能性。)

