➜ /tmp rmdir show_gif ➜ /tmp mkdir show_gif ➜ /tmp cd show_gif ➜ show_gif python3 -m venv ./venv ➜ show_gif . ./venv/bin/activate (venv) ➜ show_gif pip install Pillow Collecting Pillow Using cached Pillow-8.1.0-cp39-cp39-macosx_10_10_x86_64.whl (2.2 MB) Installing collected packages: Pillow Successfully installed Pillow-8.1.0 WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available. You should consider upgrading via the '/private/tmp/show_gif/venv/bin/python3 -m pip install --upgrade pip' command.
接着便可以让它读入并解析一张GIF图片
1 2 3 4 5 6 7 8 9
import sys
from PIL import Image, ImageSequence
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): pass
然后将每一帧都转换为RGB模式再遍历其每一个像素
1 2 3 4 5 6 7 8 9 10 11 12 13
import sys
from PIL import Image, ImageSequence
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() for y in range(0, rgb_frame.height): for x in range(0, rgb_frame.width): pass
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() for y in range(0, rgb_frame.height): for x in range(0, rgb_frame.width): colors = pixels[x, y] print('\x1b[48;2;{};{};{}m \x1b[0m'.format(*colors), end='') print('')
在每次二重循环遍历了所有像素后,还必须清除输出的内容,并将光标重置到左上角才能再次打印,这可以用ASCII转义序列来实现。查阅VT100 User Guide可以知道,用ED命令可以擦除显示的字符,对应的转义序列为\x1b[2J;用CUP命令可以移动光标的位置到左上角,对应的转义序列为\x1b[0;0H。在每次开始打印一帧图像前输出这两个转义序列即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import sys
from PIL import Image, ImageSequence
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() print('\x1b[2J\x1b[0;0H', end='') for y in range(0, rgb_frame.height): for x in range(0, rgb_frame.width): colors = pixels[x, y] print('\x1b[48;2;{};{};{}m \x1b[0m'.format(*colors), end='') print('')
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() print('\x1b[2J\x1b[0;0H', end='') for y in range(0, rgb_frame.height): for x in range(0, rgb_frame.width): colors = pixels[x, y] print('\x1b[48;2;{};{};{}m \x1b[0m'.format(*colors), end='') print('') time.sleep(rgb_frame.info['duration'] / 1000)
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() print('\x1b[2J\x1b[0;0H', end='') for y in range(0, rgb_frame.height): last_colors = None line = '' for x in range(0, rgb_frame.width): colors = pixels[x, y] if colors != last_colors: line += '\x1b[0m\x1b[48;2;{};{};{}m '.format(*colors) else: line += ' ' last_colors = colors print('{}\x1b[0m'.format(line)) time.sleep(rgb_frame.info['duration'] / 1000)
defis_square(num: int) -> bool: return math.isqrt(num) ** 2 == num
deffind_x(D: int) -> int: """ 求出给定D时,满足题目所给的丢番图方程的最小的x。 """ assertnot is_square(D) y = 1 whileTrue: candidate = D * y * y + 1 if is_square(candidate): return math.isqrt(candidate) y += 1
defsolve_66(limit): """ 找出不大于limi的D中,使find_x的返回值最大的那一个数字。 """ max_D = None max_x = None D = 2 while D <= limit: if is_square(D): D += 1 continue x = find_x(D) if max_x isNoneor x > max_x: max_D = D max_x = x D += 1 return max_D, max_x
if __name__ == '__main__': D, x = solve_66(7) print('D is {} and x is {}'.format(D, x))
(formatt"~(hello world~)~%") ; 打印hello world (formatt"~:(hello world~)~%") ; 打印Hello World (formatt"~@(hello world~)~%") ; 打印Hello world (formatt"~:@(hello world~)~%") ; 打印HELLO WORLD
(multiple-value-bind (sec min hour date mon year) (decode-universal-time (get-universal-time)) (declare (ignorable sec)) (formatt"~4D-~2,'0D-~2,'0D ~2,'0D:~2,'0D~%" year mon date hour min))
VS Code扩展的核心逻辑定义在文件src/extension.ts中。在yo生成的示例代码中,用registerCommand注册了一个名为helloworld.helloWorld的命令,其逻辑是简单地在右下角弹出一句Hello VS Code from HelloWorld!。这个回调函数,便是业务逻辑的落脚点。
This is a tool for powerful automation of OS X. At its core, Hammerspoon is just a bridge between the operating system and a Lua scripting engine. What gives Hammerspoon its power is a set of extensions that expose specific pieces of system functionality, to the user.
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function() local windows = hs.window.allWindows() -- 在Lua中遍历表的方法:https://www.jianshu.com/p/de5a4b132918 for _, win inpairs(windows) do end end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function() local windows = hs.window.allWindows() -- 在Lua中遍历表的方法:https://www.jianshu.com/p/de5a4b132918 for _, win inpairs(windows) do local app = win:application() end end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function() local windows = hs.window.allWindows() -- 在Lua中遍历表的方法:https://www.jianshu.com/p/de5a4b132918 for _, win inpairs(windows) do local app = win:application() local bundleID = app:bundleID() end end)
现在,只要变量bundleID等于Emacs的bundle ID就可以聚焦到当前遍历的窗口上了
1 2 3 4 5 6 7 8 9 10 11
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function() local windows = hs.window.allWindows() -- 在Lua中遍历表的方法:https://www.jianshu.com/p/de5a4b132918 for _, win inpairs(windows) do local app = win:application() local bundleID = app:bundleID() if bundleID == "org.gnu.Emacs"then win:focus() end end end)