How to convert JPEG images to ASCII using the 'jp2a' command (with examples)
The ‘jp2a’ command is a tool that allows you to convert JPEG images into ASCII art. It takes an input JPEG file and generates an ASCII representation of the image, which can be viewed in the terminal or saved into a file. This command provides various options to customize the output, such as colorizing the ASCII art or modifying the characters used to represent the image.
Use case 1: Read JPEG image from a file and print in ASCII
Code:
jp2a path/to/image.jpeg
Motivation:
Printing a JPEG image in ASCII art can be a fun way to view and share images in a different format. By using the ‘jp2a’ command, you can easily convert the image into ASCII and display it directly in the terminal.
Explanation:
- jp2a: The command name.
- path/to/image.jpeg: The path to the input JPEG file.
Example OUTPUT:
,@
@` $*p
;@ @@ $
,@@* :8' ^@ `@ `
8@ @* $' @$ .@
$' *@ $- @$ @$
%@ @$ $` $ @@; @@ -@
;@ '8..... '@ + `@@@. @@ ;@
,@ @@@@+$@@+++*@@;+++++;: `@ '@*@@ @' @$
,@ @@ ,;:. +@ :@@ @' @$
,@ @' ,@@@; @@ .@' @$
,@ `$@- ,+@@+ @@.@# .@' @$
-@ `@@@@@#;+*#%- ;@- ;@ @$
-@ *$@;` `@` ;@ @$
-@ -#+. +@@:$@ ;@ @$
-@ ;+@;:;:::;+#+: @$
-@ . ;$ ``
-@ `$:
-@ ,$
Use case 2: Read JPEG image from a URL and print in ASCII
Code:
jp2a www.example.com/image.jpeg
Motivation:
Reading a JPEG image from a URL and converting it into ASCII art can be useful when you want to convert remote images on the web to ASCII without having to download them first.
Explanation:
- jp2a: The command name.
- www.example.com/image.jpeg : The URL of the input JPEG file.
Example OUTPUT:
,@
@` $*p
;@ @@ $
,@@* :8' ^@ `@ `
8@ @* $' @$ .@
$' *@ $- @$ @$
%@ @$ $` $ @@; @@ -@
;@ '8..... '@ + `@@@. @@ ;@
,@ @@@@+$@@+++*@@;+++++;: `@ '@*@@ @' @$
,@ @@ ,;:. +@ :@@ @' @$
,@ @' ,@@@; @@ .@' @$
,@ `$@- ,+@@+ @@.@# .@' @$
-@ `@@@@@#;+*#%- ;@- ;@ @$
-@ *$@;` `@` ;@ @$
-@ -#+. +@@:$@ ;@ @$
-@ ;+@;:;:::;+#+: @$
-@ . ;$ ``
-@ `$:
-@ ,$
Use case 3: Colorize the ASCII output
Code:
jp2a --colors path/to/image.jpeg
Motivation:
Colorizing the ASCII output can add visual appeal to the converted image, making it more interesting and closer to the original.
Explanation:
- jp2a: The command name.
- –colors: The option to enable colorization.
Example OUTPUT:
▄█
█ █
▄█ ██ █▄
█ █ █ █ ██ ██ █
█▄ █▄ █ ███ ██
█▄ ██ █ █ █▄ ██ █▄
█▄ █▄..... ██ █ ███ ██ ██
█ ██ █ ..... █ ███ █ █ ███
█ ██ █ █ █ ██ █ ██
█ ███ █▄ ▄█ ███ ███ ██ ██
██ ██████ ███ █▄
██ ▀▀▀▀ ███:$ ███ ██
██ ▀▀ ▀▀
██ $$
Use case 4: Specify characters to be used for the ASCII output
Code:
jp2a --chars='..-ooxx@@' path/to/image.jpeg
Motivation:
By specifying custom characters to be used for the ASCII output, you can create unique representations of the image according to your preferences.
Explanation:
- jp2a: The command name.
- –chars=’..-ooxx@@’: The option to specify the characters used for the ASCII output.
Example OUTPUT:
-o
@. ox@
@o x@ d@@
!@o .# @@ @# "
o@ @ o+ @% m@
+! x! *- @% %@
d" @+ + % o@; @@ &@
!@ /o..... x : x@@@. @@ %@
%; ;""""@@@x@@@@@&@@@@@@@@@@x@ @@@*@@ @' %@
o@ @@ ,(>! %x @! @' %@
!@ 8@ ]@: @@ @@' %@
!@ %xo! ^+@x! @@:#x @#.@@ %@
%@ `@x@@@@@@*!!!!!!$$@@+ @; &@ %@
%@ xx%$?` %' o@ %@
%@ +#/. &@@:$@ o@ %@
%@ ;`@;!::::%@%: %@
%@ . /$ "
%@ P!
Use case 5: Write the ASCII output into a file
Code:
jp2a --output=path/to/output_file.txt path/to/image.jpeg
Motivation:
Saving the ASCII output into a file allows you to preserve the converted image for future use or share it with others.
Explanation:
- jp2a: The command name.
- –output=path/to/output_file.txt: The option to specify the output file path.
Example OUTPUT:
Content of output_file.txt
:
,@
@` $*p
;@ @@ $
,@@* :8' ^@ `@ `
8@ @* $' @$ .@
$' *@ $- @$ @$
%@ @$ $` $ @@; @@ -@
;@ '8..... '@ + `@@@. @@ ;@
,@ @@@@+$@@+++*@@;+++++;: `@ '@*@@ @' @$
,@ @@ ,;:. +@ :@@ @' @$
,@ @' ,@@@; @@ .@' @$
,@ `$@- ,+@@+ @@.@# .@' @$
-@ `@@@@@#;+*#%- ;@- ;@ @$
-@ *$@;` `@` ;@ @$
-@ -#+. +@@:$@ ;@ @$
-@ ;+@;:;:::;+#+: @$
-@ . ;$ ``
-@ `$:
-@ ,$
Use case 6: Write the ASCII output in HTML file format, suitable for viewing in web browsers
Code:
jp2a --html --output=path/to/output_file.html path/to/image.jpeg
Motivation:
Converting the ASCII output into HTML format allows you to view the ASCII art in a web browser, enabling easier sharing and embedding in web pages.
Explanation:
- jp2a: The command name.
- –html: The option to enable HTML output.
- –output=path/to/output_file.html: The option to specify the output file path.
Example OUTPUT:
Content of output_file.html
:
<!DOCTYPE html>
<html>
<head>
<title>ASCII to HTML conversion</title>
</head>
<body>
<pre>