对于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.

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:\\Users\Administrator\Downloads\\520.jpg", 16)

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

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

再来看看图片合并

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

# -*- 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:\Users\Administrator\Downloads\520.jpg -t 16 -s 1.25

result.gif

The End

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值