520这天,我突然意识到,她根本配不上我这么聪明的男人


640?wx_fmt=gif


作者|王翔

来源|恋习Python(ID:sldata2017)


网络情人节

对于即将到来的网络情人节(520),程序员是怎么看待它的呢?


不知道大家是否会期待这天的到来,但对于我这个程序猿来说:


  • 从主观来讲,不放假的节日,都不算节日

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


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


IT男的思维


今天随手翻UC,看到一个关于程序猿520表白的段子。


虽然是17年的老梗,但当时帖子比较火名为“她根本配不上我这么聪明的男人!”


640?wx_fmt=gif


 [段子链接]

http://www.sohu.com/a/168270871_99956288


段子是一张很长长长长图,让人看得难受


在这个重大节日---520情人节来临之际,我却是显得更加寂寞无聊。看着那张长图有点不爽(关键是朋友圈狗粮吃得有点多),于是.........就有了下面这张动态图(用Python将其做成一张动态图,这就是聪明的男人一贯的做法,哈哈哈哈)

640?wx_fmt=gif


下来让我们一起来看看聪明的男人,是如何将那张不爽的常常图做成一张动态图的?


图片的拆分与合并


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,主要是因为图片中万恶的马赛克和“腾讯视频”的字样,影响我看段子的心情...


结果如下图:


640?wx_fmt=other

图片分隔效果.png

再来看看图片合并


将16张剪切好的图片,组合成一个gif的动画,看起来会比单纯的图片看着高端多了,不是吗?


之前说到了PIL模块的强大,我们只需要使用Image的duration关键字,就能达到我们的目的。


上代码看看吧:


 
 
import argparsefrom PIL import Imageimport osclass 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()
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


640?wx_fmt=gif

result.gif


听完这位小哥哥说的

终于明白什么叫做注孤身了

真的是“凭自己本事”单的身!

没毛病啊!


送上视频

大家好好感受一下这位兄弟的“无奈”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值