Task
Write a program or function that takes an image of width w
and height h
, and two integers a <= w/2
and b <= h/2
, and outputs an ascii art representation of the image, with the dimensions a
x b
.
The ascii art should be generated through the following steps (or another process with the same output):
Resize the
w
xh
image to2a
x2b
. The color of the pixel(x, y)
in the resized image should be equal to the average of the colors of all the pixels from the original image inside the rectangle with the following corners:(floor[x * w/2a], floor[y * h/2b]) (ceil[(x+1) * w/2a], ceil[(y+1) * h/2b])
The resized image should be desaturated by taking the average of the red, green, and blue components of the color, and setting each component to this average.
A character should be outputted for each 2x2 block of pixels, following the rules below:
- Let
e = 3 - floor[color/64]
, where color refers to the red component of the color of the top-left pixel (in the range 0..255). Letf
,g
, andh
refer to this value, but for the top-right, bottom-left, and bottom-right pixels respectively. - Let
z
be the average ofe
,f
,g
,h
. - Output the character corresponding to the following (white represents a pixel where
3 - floor[color/64]
is zero, and grey represents nonzero). = Output a space (
)
or
= Output
"
ife (or f) >= 2
and'
otherwise.or
= Output
,
ifg (or h) >= 2
and.
otherwise.or
= Output
-
or
= Output
;
= Output
\
= Output
/
= Output
J
= Output
L
= Output
7
= Output
P
z = 1
: output*
.z = 2
: outputC
.z = 3
: output#
.
- Let
Other notes: All averages taken in the above steps should use integer division (i.e. rounding towards 0).
You may use a library to read images, but libraries may not be used for the resizing and desaturation steps.
Input
Your program will take in three pieces of data:
- An image. This image may be in any format of your choice (e.g. PNG, PPM)
- The width of the ascii art (in number of characters)
- The height of the ascii art (in number of characters)
Input may be read through STDIN, passed as an command-line argument, stored as a variable, etc.
Output
Your program will output the ascii art generated from the image and through the process described above. Each line must be the same width (the width passed as input), and no extra spaces may be added or removed. The last line may have a trailing newline, but this is not mandatory. Blank lines (lines with only spaces) must not be omitted.
Examples
All images were taken from Wikipedia and released into public domain. Your program is expected to work for all images and valid inputs.
Width = 52, Height = 25:
.--***-- ----. -**********L.-- .--7----P- .J****************. .*********\. ,******************L \J**********'. -********************. J***********L/ J*********************; ./*************.L ;*********************P J**************;7; .**********************' .***************;;* ;********************** ;***************;J*. *********************** *******P********'**; C*********************C *******P;*******.**; C********************** J***************;**; C*********************; ****************.**; **********************' ***************P;** J*********************P ***************/*** .********************** 7*************'J**; ;********************C" 'P***********PJ***' "7******************C" '**********P.**C; '*C*************' ;*********-J**CP '*C**********P 7**7**P/-****P -*CCCCC*P- '7********P' '--- '---'
Width: 70, Height: 3:
---------------------------. .---------------- J*****************************P -J********************/J**; ----------*********P---------' ----------------------'
Width: 50, Height: 25:
.J--- .J*P ---J*L--. J***' L -J***********- J**** ;****************L ;*****' .J******************** J****7 ************************P **CC* ; .*********LJ***********P- **C*P ' J**********************- 7*C*L **********************; J*C**; **********************; JC**C**. ; ;********CCC*C**CCCC** "7*****L . 7*****CCCCCC****CC**' -*****. -J -**C*C*CC******CC*' ;**CC***; '**CCC*****CCCC*; '-****- --***CCCCC***- '------'
Width: 26, Height: 17:
- ,L J-**L .C# J';*P-L ,C#" .P P 7. JC7P ;; * J ;; ,C'C; J; C C ;L C" # *L 7-P ;* ;P C 7CL***J*P C' # '7#C*C#P' ;P JC#C- J#CCC#L .JC#CJCC##C; ;#- -#, CCC#CCCCCCCC ;*.- -.C; ;CCCCCCCCCCCC ;****J**L ;CCCCCCCCCCCC ;*******; 'CCCCCCCCCCCP '*******' CCCCCCCCCCC' '*P-7*' "CCCCP '-- '---
This is code-golf, so the shortest answer wins.