With a little Python code and a Raspberry Pi, this Gonzo ASCII tribute came to life.
About ASCII artASCII art uses American Standard Code For Information Interchange characters to produce low fi images. It was kind of common during old computer days due to lack of graphics in text based terminals and printers.
Hunter S. Thompson (July 18, 1937 – February 20, 2005) was a great American counterculture writer and creator of Gonzo journalism. He wrote Hell’s Angels and Fear and loathing in Las Vegas, among others.
A random paragraph from Fear and loating in Las Vegas is selected, then every letter is typed on screen with a typewriter animation and sound. When the paragraph comes to an end, a selected ASCII art Hunter S. Thompson picture is displayed. The loop starts all over again with a new paragraph and a new picture.
I will provide code snippets for every part of this project. If you are not familiar with Raspberry and Python, I do recommend this book
Parts- Raspberry Pi
- microSD card for Raspberry Pi OS
- microUSB Power Supply 5V 2A
- LCD screen or TV
- HDMI cable
- Optional: amplified speaker.
Setup Raspberry Pi OS https://www.raspberrypi.com/documentation/computers/getting-started.html
Connect HDMI cable from the Raspberry to the LCD screen. If your LCD screen does not have audio, connect the Raspberry Pi audio jack to an amplified speaker.
Book formatDigital book should be uploaded to the Raspberry Pi with.txt extension. You can use Calibre software to convert from epub or mobi to txt. Then upload to Raspberry Pi using FTP.
ASCII art imagesAview is used to convert images to Ascii art. The program is able to use jpg but not all jpgs are recognized, so it is better to have the images converted to pnm format.
To install aview
$ sudo apt-get install aview
To convert images from jpg to pnm use:
$ sudo apt-get install imagemagick
$ convert a.jpg a.pnm
How to read a text file and get a random paragraphlines = open('/home/pi/asciiart/book.txt').read().splitlines()
myline=""
# if the paragraph is too short - like chapter title - find another one
while myline=="" or len(myline)<100:
myline =random.choice(lines)
How to play an mp3 or wav sound from Python# play a typewriter sound
os.system('aplay /home/pi/asciiart/k1.wav -q &')
How to create and display Ascii art from an imageos.system('asciiview /home/pi/asciiart/myImage.pnm&')
Typewriter text animationI wrote this little routine with random pauses between letters and also errors, to provide a typewriter feeling.
def typingPrint(text):
for character in text:
sys.stdout.write(character)
if (randrange(10))==1:
myErrorLetter=randrange(5)
if myErrorLetter==1:
sys.stdout.write('\b' + 'X')
if myErrorLetter==2:
sys.stdout.write('\b' + '.')
if myErrorLetter==3:
sys.stdout.write('\b' + 'P')
if myErrorLetter==4:
sys.stdout.write('\b' + '&')
if myErrorLetter==5:
sys.stdout.write('\b' + '+')
sys.stdout.flush()
time.sleep(random.uniform(0, 0.4))
sys.stdout.write('\b' + character)
sys.stdout.flush()
time.sleep(random.uniform(0, 0.2))
sys.stdout.flush()
time.sleep(random.uniform(0, 0.2))
How to execute the script at startTo avoid having to manually execute the script, you can configure the Raspberry Pi to autoexecute the Python script.
$ sudo nano /home/pi/.bashrc
Add this line at the end:
sudo python /home/pi/asciiart/asciiart.py > /dev/tty1
Download images automaticallyIf you want to add images automatically, you can do so easily with this command executed once per day through cronjob
$ sudo python3 bbid.py -s "Hunter S. Thompson" --limit 5 +filterui:age-lt43200+filterui:imagesize-medium
And tweets about Hunter S. Thompson can be integrated with Tweepy.
Interested in other literature and technology projects?
Comments