Skip to content

Instantly share code, notes, and snippets.

@ambv
Last active March 23, 2022 17:18
Show Gist options
  • Save ambv/fc5aa32738b1ccb231b5e3828a43c383 to your computer and use it in GitHub Desktop.
Save ambv/fc5aa32738b1ccb231b5e3828a43c383 to your computer and use it in GitHub Desktop.
Happy Atheist Day!
import base64
import bz2
from dataclasses import dataclass
from pathlib import Path
import pickle
from typing import Iterable, Iterator
try:
from rich.color import Color
from rich.console import Console, ConsoleOptions, RenderResult
from rich.segment import Segment
from rich.style import Style
except ModuleNotFoundError:
raise ImportError("Install Rich to run this.") from None
ColorLine = list[Color]
def image_to_color_lines(
path: Path,
*,
width: int = 80,
background_color: Color = Color.from_rgb(255, 255, 255),
) -> Iterator[ColorLine]:
try:
from PIL import Image
except ModuleNotFoundError:
raise ImportError("Install Pillow to generate color lines yourself.") from None
img = Image.open(path)
orig_width, orig_height = img.size
aspect_ratio = orig_height / orig_width
height = int(aspect_ratio * width)
img = img.resize((width, height))
line = []
count = 0
if img.mode == "RGBA":
bg = Image.new("RGBA", img.size, tuple(background_color.triplet))
img = Image.alpha_composite(bg, img)
img = img.convert("RGB")
for r, g, b in img.getdata():
line.append(Color.from_rgb(r, g, b))
count += 1
if count == width:
yield line
line = []
count = 0
@dataclass
class CommandLineImage:
color_lines: Iterable[ColorLine]
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
background = None
background_color = None
padding = None
for line in self.color_lines:
if background_color is None:
background_color = line[0]
if padding is None:
width = len(line)
padding = (options.max_width - width) // 2
if background is None:
background = line
else:
yield Segment(" " * padding, Style(bgcolor=background_color))
for fg, bg in zip(line, background):
yield Segment("▄", Style(color=fg, bgcolor=bg))
yield Segment.line()
background = None
color_lines_compressed = """\
6<ł%_0gSqh;d#@;DeT2@<<*!us53kVs8W-!rSRYTs8W-!s8W-!s8W-!s8W-!s8W,W&KVI5!!!"H-oUki#D(_S!!#
t#54ATg(`m)71.6Xd'Kh5[1hKHF;bYUK4[tK0qP-a4%1c>#4Z*=N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/(:R.m%Vq#=r9MuHS`f%7U
!L-50]9^/s,3ipfc(7o=#?KX1*ZJSD_Ul;(=`*?2RSA&TWf?jBka<jiLZLLOCN5h[dRYXP+'3[F82s=?2Tq]#>mA
*KX?Dh7O2d+C(hJ4,!;Y7Adr+kPTC1dt[6oQ*q-$^M1]8:m?NlBmTSZ@0WQ-=@uVsrechS4k'O'2VT[6@"]0fjGm
^'P%u<UUTSO?#@_5c(9U3[9T^;"mnmLW1elfO>=drł!a@npQ5<0c*=tA*^<VfirRf'#qama,1S/i,4Kio5,Gmf!g
!-]/WF<VXp1Uka6`L/OS^3]j#,lU?JfiHi^jroaQ3JM2duf/nnbfD5jI.Q7*QZiF3"4!R,i]Xs1T&^@+l+D`PpO!
]Lbp!$"/0&Tc"oc,;sg'?QX:-s&9n+;]8o8-+6nItFeZ`PB!8"k!c$+X%sC`(S$9DLl^1o+h&Y?S6'8i[L]iqKa`
K&YS&uJ8rGqh>e*dEmgPUJ6%[a7-Z%rAnRe-"A>g.;&m;@4A[:CJ0GSU0S10^MSW[h>#L36!gKG?#Dł!V84#VccK
f8!Dpi,=2i3.qO_W?YZpDXj0]3obPftR#WL-RO#Yba<7Ył;Z+pS6L;f[S%(n`<-OurjYnX2N3(QIHU)8?"n!u4Nt
6V5?n#12t$p-/B"R!K+Pq5@7KBg0sa1MRbBQ&IU',uc`1#TRFjPa!Ył+qSF81n7*fTb&CN$@ouMOVl(@5YVO2L`B
"gW@Fnł"@1I+0nb?$LeUf5eq_0*DhlT`L5*2]1n9n/Y`=EaX/iR3ł3e%dYVR+3$k!,g#Z*oF1^XkH6::m04PA?u+
YLQ],X(oFW8PJq7e#JRQV(4Ag_!E<@]Błupq)ł<_M!T>_g=o\
"""
def main(compressed: str = "") -> None:
console = Console()
if not compressed:
# regenerate image from .png
# original at https://www.atheistrepublic.com/atheist-day/symbol
color_lines = list(image_to_color_lines(Path("atheism.png"), width=42))
compressed = base64.a85encode(
bz2.compress(pickle.dumps(color_lines, protocol=4)), wrapcol=88, pad=True
).decode()
compressed = compressed.replace("\\", "ł")
print(len(compressed))
print(compressed)
compressed = compressed.replace("ł", "\\")
color_lines = pickle.loads(bz2.decompress(base64.a85decode(compressed)))
console.print(CommandLineImage(color_lines))
if __name__ == "__main__":
main(compressed=color_lines_compressed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment