今天刷抖音,连续刷到几次那个敲键盘,拼图片出来的视频。类似这种

觉得酷炫同时,就想研究一下,这个技术原理。带着好奇心去百度了一下,发现有现成的软件,不过要关注公众号,就放弃了。往下浏览时候,发现,Python竟然可以做到
研究了一下别人的代码,明白了原理以后,决定自己写一次。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| # -*- coding: utf-8 -*- from PIL import Image
codelist = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''
count = len(codelist)#获取长度
fp = open('3.jpg', 'rb')#手动 image_file = Image.open(fp)
def Transformation(image_file):
image_file = image_file.convert('L')#转为灰度图像 codepic = '' for h in range(0, image_file.size[1]): #size属性表示图片的分辨率,'0'为宽W大小,'1'为高H向 for w in range(0, image_file.size[0]): gray = image_file.getpixel((w, h))#转为元组 codepic = codepic + codelist[int(((count - 1)*gray)/256)]# 灰色映射 codepic = codepic + '\r\n'
print(u'W:', image_file.size[0], ' H:', image_file.size[1], ' INFO:', count) return codepic
image_file = image_file.resize((int(image_file.size[0] * 0.50), int(image_file.size[1] * 0.25)))#调整输出大小。[0]为宽,[1]为高
tmp = open('k.txt', 'w')#手动 tmp.write(Transformation(image_file)) tmp.close()
|
主要运用了Image 这个模块,,在Python3 中 是 Pillow 这个模块,用之前要先装个库 pip install Pillow
关于 Image 这个模块的用法,以下链接有详细讲到。
1
| https://blog.csdn.net/chenriwei2/article/details/42071517
|