我是靠谱客的博主 高大高山,这篇文章主要介绍对于IT男的表白方式,你可能存在什么误解,现在分享给大家,希望可以做个参考。

网络情人节

对于即将到来的网络情人节(520),程序员是怎么看待它的呢?
不知道大家是否会期待这天的到来,但对于我这个程序猿来说:

  • 从主观来讲,不放假的节日,都不算节日
  • 从客观来讲,由消费带动的节日,都不是纯粹的节日

找这么多理由,其实归根结底,主要是以为“穷”,穷人过什么节日啊!


IT男的思维

今天随手翻UC,看到一个关于程序猿520表白的段子。
虽然是17年的老梗,但当时帖子比较火名为**“她根本配不上我这么聪明的男人!” [段子链接]**
段子是一张长图,想想最近比较无聊,拿它做个Python的练习题呗。


图片的拆分与合并

Python的PIL模块在对图片处理上简直方便的不行…

先来说说图片的拆分吧

先来看看长图,内容是一共16张对白拼成的段子,其实我们只要把这16张图按照等高的方式进行裁剪就OK了,so easy!
代码主要用到了Image.crop(cropBox)的裁剪方式。
至于crop的拆分,点进去函数就能看到相关注释:

Returns a rectangular region from this image. The box is a
4-tuple defining the left, upper, right, and lower pixel
coordinate. See :ref:coordinate-system.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os from PIL import Image def split_image(file, split_times): path, filename = os.path.split(file) os.chdir(path) try: os.mkdir('pictures') except FileExistsError: pass img = Image.open(filename) width, height = img.size per_height = height / split_times for pictureNumber in range(split_times): _cropBox = (0, per_height * pictureNumber, width * 0.8, per_height * (pictureNumber + 1)) picture = img.crop(_cropBox) picture_name = os.path.join(path, 'pictures', "%d.png" % pictureNumber) picture.save(picture_name) split_image("C:\UsersAdministratorDownloads\520.jpg", 16)

代码片段如上,简单的处理下边缘与长度即可。至于width的0.8,主要是因为图片中万恶的马赛克和“腾讯视频”的字样,影响我看段子的心情…

结果如下图:
图片分隔效果.png

再来看看图片合并

将16张剪切好的图片,组合成一个gif的动画,看起来会比单纯的图片看着高端多了,不是吗?
之前说到了PIL模块的强大,我们只需要使用Image的duration关键字,就能达到我们的目的。
上代码看看吧:

复制代码
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*- # @Author : 王翔 # @JianShu : 清风Python # @Date : 2019/5/18 22:53 # Software : PyCharm # version: Python 3.6.8 # @File : ChromePassword.py import argparse from PIL import Image import os class SplitLongPicture: def __init__(self): self.dirName = os.path.split(os.path.abspath(__file__))[0] self.ImagePath = args.ImagePath self.SplitTimes = args.SplitTimes self.SwitchingTime = args.SwitchingTime self.Path, self.File = os.path.split(self.ImagePath) self.Image = self.check_image_file() self.pictureList = [] def check_image_file(self): _imageType = ['.jpg', '.png', '.bmp'] if not os.path.isfile(self.ImagePath): raise IOError("请检查图片路径", self.ImagePath) elif os.path.splitext(self.File)[1].lower() not in _imageType: raise TypeError("请选择系统适配的图片类型", _imageType) else: return Image.open(self.ImagePath) def split_image(self): os.chdir(self.Path) try: os.makedirs('pictures') except FileExistsError: pass width, height = self.Image.size _unitHeight = height / self.SplitTimes for pictureNumber in range(self.SplitTimes): _cropBox = (0, _unitHeight * pictureNumber, width * 0.8, _unitHeight * (pictureNumber + 1)) _unitPicture = self.Image.crop(_cropBox) _pictureName = os.path.join(self.Path, 'pictures', "%d.png" % pictureNumber) self.pictureList.append(_pictureName) _unitPicture.save(_pictureName) def composite_gif(self): images = [] im = Image.open(self.pictureList[0]) for file in self.pictureList[1:]: images.append(Image.open(file)) gifName = os.path.join(self.Path, "result.gif") im.save(gifName, save_all=True, loop=True, append_images=images, duration=self.SwitchingTime * 1000) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-p', '--ImagePath', help="所需分隔的图片途径") parser.add_argument('-t', '--SplitTimes', type=int, help="图片分隔次数") parser.add_argument('-s', '--SwitchingTime', type=float, help="GIF图片切换时常长(单位:秒),支持小数") args = parser.parse_args() if None in args.__dict__.values(): parser.print_help() else: Main = SplitLongPicture() Main.split_image() Main.composite_gif()

代码顺便复习了一下**argparse**的相关知识。那么该怎么运行呢?
python D:SplitLongPicture.py -p C:UsersAdministratorDownloads520.jpg -t 16 -s 1.25

result.gif

The End

段子链接下面还有一个修电脑的梗,如果大家想练习下,可以按照代码,自己做一个修电脑的动图。
希望大家喜欢我的小练习…

©本文由作者:清风Python 原创 如需转载请注明
欢迎大家关注我的公众号 清风Python

最后

以上就是高大高山最近收集整理的关于对于IT男的表白方式,你可能存在什么误解的全部内容,更多相关对于IT男内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部