寻找有关如何使用 PNGlitch 的解释

Looking for an explanation of how to use PNGlitch

我已经安装了 Ruby 环境管理器 rbenv, Ruby, RubyGems, and PNGlitch(在 macOS 上)。现在我该如何使用 PNGlitch?

我能找到的最好的文档在 this page 上,下面是给出的语法示例:

如何使用这个库:简单的方法

png = PNGlitch.open '/path/to/your/image.png'
png.glitch do |data|
  data.gsub /\d/, 'x'
end
png.save '/path/to/broken/image.png'
png.close
    

好的,很好。当我插入我的文件路径,将该代码保存为 .rb 文件并打开它时,我只得到:

test.rb: command not found

如果我将它直接粘贴到终端,我会得到:

png = PNGlitch.open '/Users/username/Documents/testimage.png'
-bash: png: command not found
ComputerName:~ username$ png.glitch do |data|
>   data.gsub /\d/, 'x'
-bash: png.glitch: command not found
-bash: data: command not found
-bash: data.gsub: command not found
ComputerName:~ username$ end
-bash: end: command not found
ComputerName:~ username$ png.save '/Users/username/Documents/testimage_glitched.png'
-bash: png.save: command not found
ComputerName:~ username$ png.close

我也尝试了 this page 上给出的语法并输入:

pnglitch /Users/username/Documents/testimage.png –filter=Sub /Users/username/Documents/testimage_glitched.png

...这导致收到以下消息:

tried to create Proc object without a block

Usage:
  pnglitch <infile> [--filter=<n>] <outfile>

Options:
  -f, --filter=<n>  Fix all filter types as passed value before glitching.
                    A number (0..4) or a type name (none|sub|up|average|paeth).
  --version         Show version.
  -h, --help        Show this screen.

↑ 我猜这是开发者对文档的想法。

好吧,为了仿效这个例子,我也这样做了:

pnglitch </Users/username/Documents/testimage.png> [--filter=<2>] </Users/username/Documents/testimage_glitched.png>

...但这只会导致:

-bash: syntax error near unexpected token 2

(我选择了 2,因为显然 corresponds to the "Sub" filter。)

我也尝试了这种语法的变体,包括省略字符 <>[].

这里一定有一些我没有的假定知识。所以我想知道的是:

  1. 我怎样才能真正使用 PNGlitch 对 PNG 图像进行故障处理?
  2. 如何使用 PNGlitch 对文件夹中的所有 PNG 图像进行故障处理?

任何关于使用不同过滤器的额外建议也将不胜感激。

谢谢。

这里有很多事情需要清理。

Ruby 脚本 需要 Ruby 到 运行

您不能只是将这些粘贴到 bash 中并期望发生任何有用的事情。

通常的程序是两种变体之一。或者:

  1. 创建一个 .rb 脚本,例如 example.rb
  2. 运行 ruby example.rb 最后是你的脚本名称。

或使用“hash-bang”方法:

  1. 创建一个以 #!/usr/bin/env ruby 作为第一行的脚本。
  2. 使用 chmod +x example.rb
  3. 使此脚本可执行
  4. 运行 直接使用此脚本,./example.rb 或它具有的任何路径。

请注意,example.rb 本身将不起作用,除非它在您的路径中,因此 ./ 是必需的。

命令行示例语法

这里的 <name> 有特殊的含义,它只是 name 的一种表达方式,就好像它有斜体或特殊格式一样。在文本模式终端上,添加这样的语法是不切实际的,在大多数情况下它仅限于 ASCII,因此这种传统得到了发展。

Within the POSIX shell > and < have special meaning, they're used to, respectively, redirect input to or from a file. For example, ls > ls.txt dumps the output of ls into a file called ls.txt, while cat < ls.txt reads in the contents of ls.txt and displays it.

[name]这样的东西意味着可选参数,像[--help]意味着--help参数是可选的。

Within the POSIX shell [ and ] have special meaning. They can be used in an if construct, but more commonly in file wildcards, like l[abc].txt means any of la.txt, lb.txt or lc.txt.

把这些放在一起就可以理解这里使用的符号了:

pnglitch <infile> [--filter=<n>] <outfile>

这意味着 infile 是您的“输入文件”参数,outfile 是您的“输出文件”参数,--filter 是一个可选参数,采用 n 作为输入。

所以你这样称呼它:

pnglitch input.png output.png

或者像你一样有一个选项:

pnglitch testimage.png --filter=sub testimage_glitched.png

不过请注意,我使用了小写字母 sub,因为这正是帮助输出中的内容,并且遵循大小写约定通常很重要。