本指南介绍如何在 Linux 中将图像转换为 ASCII 格式。 出于此任务的目的,我们将使用 JP2a. Jp2a 是一个命令行工具,可帮助您将给定的图像转换为 ascii 字符格式。 您可以指定图像文件和 URL 的混合,Jp2a 工具会立即将它们全部转换为 ascii 字符。 Jp2a 有许多有用的选项来处理图像。 以下是 Jp2a 可以为您做的一些事情!
- 从标准输入读取图像,
- 将背景模式设置为浅色或深色,
- 设置边框,
- 设置输出高度和宽度,
- 为输出图像设置自定义尺寸,
- 垂直或水平翻转输出图像,
- 在生成输出 ASCII 图像时使用特定字符,
- 反转图像,
- 从网上下载图像并转换它们,
- 还有很多。
内容
在 Linux 中安装 Jp2a
Jp2a 被打包用于许多 Linux 发行版,并且在默认存储库中可用。
要在 Arch Linux 及其变体如 Manjaro Linux 上安装 Jp2a,请运行:
$ sudo pacman -S jp2a
在 Debian、Ubuntu、Linux mint 上:
$ sudo apt install jp2a
在 Fedora:
$ sudo dnf install jp2a
在 CentOS / RHEL 上:
$ sudo yum install epel-release
$ sudo yum install jp2a
在 openSUSE 上:
$ sudo zypper install jp2a
在 Linux 中将图像转换为 ASCII 格式
要将给定的 JPG/JPEG 图像转换为 ASCII 字符格式,只需运行:
jp2a <path-to-image>
例子:
$ jp2a arch.jpg
示例输出:
MWXMWXWWXWWXWWXWWXWMXWMXNMNNMNNMNNMNNMNXWNXMWXWWXWWXWWXWMXWMXNMXNMNNMNNMNNMNXMNXMW NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWdxWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWNkccxWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW WWNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNW0ccccOWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWWNWW NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWKlccccc0WNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNXlggggggl0WWNWWNWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW WWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNoggggggcclXWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWWNWW NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNdggggggccccoXWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNkggggggggggggoXWWNWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW WWNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNkggggggggggggccdNWNWWNWWNWWNWWNWWNNWNNWNNWNNWWNWW NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNkggggggggggggccccdNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWXxggggggggggggccccdXWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW WWNWWNWWNWWNWWNWWNWWNWWNWWNNWNOoOXNklggggggggggggccdNWNWWNWWNWWNNWNNWNNWNNWNNWWNWW NWWNNWNNWNNWNNWNNWWNWWNWWNWWNOccccok0kdggggggggggggcoXWNNWWNWWNWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNWWNWWNWWNNOggggggcccodoggggggggggggoXWWNWWNWWNNWNNWNNWNNWNNWNNWW WWNWWNWWNWWNWWNWWNWWNWWNWWNkggggggggggggggggggggggggccoNWNWWNWWNWWNNWNNWNNWNNWWNWW NWWNWWNNWNNWNNWNNWWNWWNWWNkggggggggggggggggggggggggccccoXWWNWWNWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNWWNWWNNkggggggggggggggggggggggggggggggoKWWNWWNNWNNWNNWNNWNNWWNWW WWNWWNWWNWWNWWNWWNWWNWWNxggggggggggggggggggggggggggggggcclXWNNWNNWNNWNNWNNWNNWNNWW NWWNNWNNWNNWNNWNNWWNWWXdggggggggggggggggggggggggggggggcccclKWWNWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNWWNNdggggggggggggggggggggggggggggggggggggl0NWNNWNNWNNWNNWNNWNNWW WWNWWNWWNWWNWWNWWNWWXoggggggggggggccccdx0Oxdlggggggggggggcccl0WNWWNNWNNWNNWNNWWNWW NWWNWWNNWNNWNNWNNWWKlggggggggggggcclkXWWNWWNNOlggggggggggggcccOWWNWWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNWWNKlggggggggggggcclKNWWNWWNWWNXoggggggggggggcccONWNNWNNWNNWNNWWNWW WWNWWNWWNWWNWWNWW0lggggggggggggcclXNNWNNWWNWWNWNoggggggggggggccckWNNWNNWNNWNNWNNWW NWWNNWNNWNNWNNWNkggggggggggggccccONWWNWWNWWNNWNNXggggggggggggccccdNWNWWNWWNWWNWWNN WWNWWNWWNWWNWWNkggggggggggggcccclNWNNWWNWWNWWNWWNdggggggcccloolcccdNWNNWNNWNNWWNWW WWNWWNWWNWWNWWxggggggggggggcccccoNWNNWWNWWNWWNWWNkggggggcccccoxO0OddNNNWNNWNNWNNWW NWWNNWNNWNNWXdggggggggggggggggggoWNWWNWWNWWNNWNNWkggggggggggggcclxKNXWWNWWNWWNWWNN WWNWWNWWNWWXoggggggggggggggggggccXWNNWWNWWNWWNWWNdggggggggggggcccccoOXNWNNWNNWWNWW WWNWWNWWNWXoggggggggggggcccldxk00XWNNWNNWWNWWNWWNK0kxdlggggggggggggcccxKNNWNNWNNWW NWWNNWNNWKlggggggccccldxO0NWNWWNWWNWWNWWNWWNNWNNWNNWNNWXK0xdlggggggccccl0WNWWMMMMM WWNWWNWWOggggggccoxOXNWNWWNWWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNKOxoggggggccOWNWMMMMM WWNWWNWOcccccox0NNWWNWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNNWNNN0xoccccckNNMMMMM NWWNNWkccok0XNWNNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWWNWWNWWNWWNN0xocckWNWWNW WWNWNxdOXWWNWWNWWNWWNWWNWWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNXOxxNNNWW NWWNXNNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWNXWWNN
很简单,不是吗?
您可以指定多个图像并一次性转换所有图像。
$ jp2a img1.jpg img2.jpg
此命令将给定的图像转换为 ASCII 并将它们一个接一个地显示出来。
就像我已经说过的,Jp2a 有很多选项和标志来操作输入图像。 让我们通过示例来看看一些选项的用法。
从标准输入读取图像
要从标准输入读取图像并在标准输出中打印 ASCII 字符,请使用 管道 命令如下。
$ cat arch.jpg | jp2a -
注意最后的连字符 (-)。
将输出写入文件
您可以将其写入文件,而不是在标准输出中显示 ASCII 图像,如下所示。
$ jp2a arch.jpg --output=arch_ascii
此命令会将 ASCII 输出附加到名为的文本文件中 arch_ascii. 您可以使用“cat”命令查看文件的内容:
$ cat arch_ascii