+-
一个 Python 包,能够将图像(支持 .jpg 和 .png 格式)转换为 ASCII 艺术画。

asciify-them

一个 Python 包,能够将图像(支持.jpg.png格式)转换为 ASCII 艺术画。它受到ascii-view的启发,支持多种功能,包括命令行界面(CLI)操作和作为 Python 库的使用。

Github地址

https://github.com/ndrscalia/asciify-them

一个 Python 包,能够将图像(支持 .jpg 和 .png 格式)转换为 ASCII 艺术画。

功能特点

  • 支持格式:目前支持将.jpg.png格式的图片转换为 ASCII 艺术画。

  • 命令行界面(CLI):可以通过命令行直接使用该工具,也可以作为 Python 库进行调用。

  • 彩色输出:支持使用 ANSI 颜色代码在现代终端中打印彩色 ASCII 艺术画。

  • 尺寸调整灵活性

    • 默认情况下,图片会按比例缩放以适应终端窗口,同时保持原始宽高比。

    • 用户可以选择禁用宽高比保护,或者自定义输出的宽度和高度。

  • 边缘检测:使用 Sobel 和 Canny 算法来突出图片的边缘。

  • 输出灵活性:转换后的 ASCII 艺术画可以保存到文件中,也可以直接在终端中显示。

安装方法

  • 通过 PyPI 安装

    pip install asciify-them

    或者使用uv工具:

    uv tool install asciify-them

  • 从源代码安装

    gitclonehttps://github.com/ndrscalia/asciify-them
    cd<repo-dir>
    python -m venv .venv
    source.venv/bin/activate
    pip install -e .

  • 使用uv工具运行(无需安装)

    uvx --from asciify-them asciify path/to/image [OPTIONS]

使用方法

  • 终端要求:需要一个支持真彩色的终端模拟器(例如 kitty、alacritty、iTerm2)。

  • CLI 使用

    • in_terminal:使输出保持在终端内并保持宽高比。

    • wide:适用于宽高比大于 1 的图片,输出可能超出终端范围,适合转换为.png文件。

    • tall:适用于宽高比小于 1 的图片,输出可能超出终端范围。

    • -bw, --black_white:将输出设置为黑白。

    • -e, --edges:启用边缘检测。

    • -w, --width:自定义宽度(仅在f_type='wide'时可用)。

    • -he, --height:自定义高度(仅在f_type='tall'时可用)。

    • -ar, --no_aspect_ratio:禁用原始宽高比保护。

    • -f, --factor_type:选择缩放因子类型(in_terminalwidetall)。

    • -b, --blur:提供高斯模糊的参数(核大小、x 轴标准差、y 轴标准差)。

    • -ct, --canny_threshold:提供 Canny 边缘检测的阈值。

    • -at, --angles_threshold:提供角度计算的核大小。

    • -o, --output:指定输出文件路径(默认为终端输出)。

    • -A, --aspect_ratio_correction:提供用于校正终端检测到的宽高比的值。

    • 基本命令格式:

      asciify <path/to/image> [OPTIONS]

    • 可用选项:

    • 不同的缩放因子类型:

  • 作为 Python 库使用

    • 最简单的用法:

      fromasciifyimportasciify

      result = asciify("path/to/image")
      print(result)

    • 更高级的用法:

      fromasciifyimportasciify

      result = asciify(
      "path/to/image",
      color_mode="bw",
      edges_detection=True,
      f_type="tall",
      aspect_ratio_correction=1.20
      )

      withopen("output.txt","w")asf:
      f.write(result)

    • 使用核心类:

      fromasciifyimportImgProcessor, Renderer, DEFAULT_CHARSET

      processor = ImgProcessor(image_path)

      ifnotheightandnotwidth:
      term_height, term_width = processor.calculate_print_size()
      else:
      term_height, term_width = height, width

      ds_f = processor.calculate_downsample_factor(
      term_height=term_height,
      term_width=term_width,
      keep_aspect_ratio=keep_aspect_ratio,
      f_type=f_type
      )

      ds_img = processor.downsample_image(
      f=ds_f,
      keep_aspect_ratio=keep_aspect_ratio,
      aspect_ratio_correction=1.10
      )

      img_hsv = processor.convert_to_hsv(image=ds_img)

      angles = processor.calculate_angles(
      image=ds_img,
      k_size=angles_thresh
      )

      edges = processor.detect_edges(
      image=ds_img,
      blur=blur,
      canny_thresh=canny_thresh
      )

      renderer = Renderer(
      color_mode=color_mode,
      charset=DEFAULT_CHARSET
      )

      ifedges_detection:
      returnrenderer.draw_in_ascii_with_edges(img_hsv=img_hsv, angles=angles, edges=edges)
      else:
      returnrenderer.draw_in_ascii(img_hsv=img_hsv)