Canorus / commandline-mastodon

1 stars 0 forks source link

Show images in terminal #4

Open hackerb9 opened 5 years ago

hackerb9 commented 5 years ago

Todo checklist says to show images in terminal. This is trivial if we use 'system()' and have ImageMagick installed. It should be possible to do in Python directly, but I do not know the ImageMagik API.

Note that you'll need to have a terminal that supports Sixel graphics for this to work. For example: mlterm, xterm -tn 340, iTerm.

diff --git a/timeline.py b/timeline.py
index a9f00fe..0b9c4d1 100644
--- a/timeline.py
+++ b/timeline.py
@@ -80,6 +80,15 @@ def cal_spacer(w, text):
     spacer3 = int((spacer1 - spacer2)/2)
     return spacer2 + spacer3

+def show_image(url):
+    # Given the URL of an image, show it on the terminal.
+#    r = requests.get(url,stream=True)
+#    import PythonMagick
+#    im = PythonMagick.Image.read(r)
+    import os
+    os.system("curl -Ls " + url + " | convert -geometry 800x600 - sixel:-")
+
+
 # print timeline
 w = shutil.get_terminal_size().columns
 for res in timeline:
@@ -106,12 +115,16 @@ for res in timeline:
             print('!!변뚜주의!! '+res['reblog']['spoiler_text'])
         if res['reblog']['media_attachments']:
             for i in range(len(res['reblog']['media_attachments'])):
-                print('image link: '+res['reblog']['media_attachments'][i]['url'])
+                imglink=res['reblog']['media_attachments'][i]['url']
+                print('image link: '+imglink)
+                show_image(imglink)
     if res['spoiler_text']:
         print('!!변뚜주의!! '+res['spoiler_text'])
     if res['media_attachments']:
         for i in range(len(res['media_attachments'])):
-            print('image link: '+res['media_attachments'][i]['url'])
+            imglink=res['media_attachments'][i]['url']
+            print('image link: '+imglink)
+            show_image(imglink)
     strip(res['content'],w)

 print('timeline printed...')
hackerb9 commented 5 years ago

Screenshot from 2019-07-10 03-26-22