Last active
April 7, 2025 13:33
-
-
Save thingsiplay/c010f968d40b707d48e7e0b8a8b3aacc to your computer and use it in GitHub Desktop.
Convert Text files to Image format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# (This version is specific for my special use case of personal documents.) | |
# Version 1.1 | |
# 2025-04-07 | |
# | |
# Convert Text files to Image format. | |
# | |
# Usage: | |
# txt2png desc1.txt desc2.txt | |
# txt2png *.txt | |
# Use pango-list to list available fonts. | |
cleanup() { | |
local path | |
path="${1}" | |
# Remove last whitespace character of each line. | |
sed -i 's/ $//g' -- "${path}" | |
# Delete lines that are image filenames only, as these are just artifacts | |
# from manual copy/paste manouver. | |
sed -i -E '/^[a-zA-Z]+\.(png|gif|jpg)$/d' -- "${path}" | |
# I added this simple string at the beginning of some files. Its not | |
# needed for the created image. | |
sed -i '/^sonicretro$/d' -- "${path}" | |
# From Metroid, unneeded copy paste parts. | |
sed -E -i '/^Forum Thread:.+$/d' -- "${path}" | |
cat -- "${path}" | tr '\n' '\r' >>"${path}.tmp2" | |
mv -f -- "${path}.tmp2" "${path}" | |
sed -i -E \ | |
-e 's/(\(Note:)\r+/\1 /' \ | |
-e 's/(From.+:)\r+/\1 /' \ | |
-e 's/(Hack Name:)\r+/\1 /' \ | |
-e 's/(Hack of:)\r+/\1 /' \ | |
-e 's/(Platform:)\r+/\1 /' \ | |
-e 's/(Released by:)\r+/\1 /' \ | |
-e 's/(Language:)\r+/\1 /' \ | |
-e 's/(Status:)\r+/\1 /' \ | |
-e 's/(Patch Version:)\r+/\1 /' \ | |
-e 's/(Last updated:)\r+/\1 /' \ | |
-e 's/(Type of Hack:)\r+/\1 /' \ | |
-e 's/(Downloads:)\r+/\1 /' \ | |
-e 's/(Awards:)\r+/\1 /' \ | |
-- "${path}" | |
cat -- "${path}" | tr '\r' '\n' >>"${path}.tmp2" | |
mv -f -- "${path}.tmp2" "${path}" | |
# Compress multiple blank lines into one line. | |
sed -i '/^$/N;/^\n$/D' -- "${path}" | |
# Remove the beginning and end of file empty lines only. | |
sed -i -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;ba' -e '}' -- "${path}" | |
} | |
font_name='Liberation Mono' | |
font_size='16' | |
convert() { | |
local text_path | |
local image_path | |
text_path="${1}" | |
image_path="${2}" | |
pango-view \ | |
--font "${font_name}, ${font_size}" \ | |
--antialias subpixel \ | |
--hinting auto \ | |
--hint-metrics on \ | |
--line-spacing 0.95 \ | |
--width 480 \ | |
--height 640 \ | |
--no-display \ | |
--output "${image_path}" \ | |
-- "${text_path}" | |
} | |
for file in "${@}"; do | |
text_tmp="${file}.tmp" | |
png_out="${text_tmp%.txt.tmp}.png" | |
echo "${file}" | |
cp -- "${file}" "${text_tmp}" | |
cleanup "${text_tmp}" | |
convert "${text_tmp}" "${png_out}" | |
rm -- "${text_tmp}" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Convert Text files to Image format. | |
# | |
# Usage: | |
# txt2png desc1.txt desc2.txt | |
# txt2png *.txt | |
for file in "${@}"; do | |
pango-view \ | |
--font mono \ | |
--no-display \ | |
--width 320 \ | |
--height 480 \ | |
--output "${file%.txt}.png" \ | |
-- "${file}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, the new version 1.1 is for my personal use case. It includes some document cleanup from my personal copy/paste of Romhack descriptions from the internet. Also the settings are set to make the generated image texts better readable on my Steam Deck too.