python项目实战——❤️为女友写点东西❤️

Posted 肥学大师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python项目实战——❤️为女友写点东西❤️相关的知识,希望对你有一定的参考价值。

效果图


大概讲解

简单的使用了PIL库里面的Image对目标图片进行剪切和保存将图片填充为正方形,组成:原图+百底+正方形。创建了ImageObj(object)类类里面的函数功能标注的都有有不明白的地方可以留言。这次选择的照片像素不是很高,效果不是太理想。之所以没放女友照片一是怕你们爱上她我吃醋,二是现在官方查的严不让放美女图。😁😁

部分讲解

🛎️基本上总体分为四部主功能:

 	 # 1、打开图片
	 image = Image.open(file_path)

    # 2、将图片填充为正方形,组成:原图+百底+正方形
    image = self.__fill_image(image)

    # 保存图片
    image.save('temp.jpg')

    # 3、裁剪合成后图片,为9张图片
    nine_images = self.__cut_image(image)

    # 4、保存9张图片到本地
    self.save_images(nine_images)

🛎️剪切图片功能函数:

def __cut_image(self, image):
        """
        把一张图片裁剪成9张图片的坐标值,然后进行裁剪成小图片
        :return:
        """
        width, height = image.size

        
        print('原图的宽/高分别为:', width, height)

       
        # 每张图片的宽度
        item_width = int(width / 3)

        print('裁剪后的宽度为:', item_width)

        box_list = []

        for i in range(0, 3): 
            for j in range(0, 3):
                # 坐标值分别是:左、上、右、底
                box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
                print(box)
                box_list.append(box)

        # 裁剪图片
        image_list = [image.crop(box) for box in box_list]

        return image_list

🛎️填充切割图片部分:

  def __fill_image(self, image):
       
        width, height = image.size

        # 长和宽较大值,作为新图片的宽高
        new_image_length = width if width > height else height

        # 生成新图片[纯白底]
        new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')

        # 将之前的图粘贴在新图上,居中
        # 如果原图宽大于高(横图),则填充图片的竖直维度
        if width > height:
            # (x,y)二元组表示粘贴上图相对下图的起始位置
            new_image.paste(image, (0, int((new_image_length - height) / 2)))
        else:
            # 如果原图宽小于高(竖图),则填充图片的水平纬度
            new_image.paste(image, (int((new_image_length - width) / 2), 0))
        return new_image

源码


from PIL import Image


class ImageObj(object):
    def __init__(self):
        pass

    def start(self, file_path):
        # 1、打开图片
        image = Image.open(file_path)

        # 2、将图片填充为正方形,组成:原图+百底+正方形
        image = self.__fill_image(image)

        # 保存图片
        image.save('temp.jpg')

        # 3、裁剪合成后图片,为9张图片
        nine_images = self.__cut_image(image)

        # 4、保存9张图片到本地
        self.save_images(nine_images)

    def __cut_image(self, image):
        """
        把一张图片裁剪成9张图片的坐标值,然后进行裁剪成小图片
        :return:
        """
        width, height = image.size

        
        print('原图的宽/高分别为:', width, height)

       
        # 每张图片的宽度
        item_width = int(width / 3)

        print('裁剪后的宽度为:', item_width)

        box_list = []

        for i in range(0, 3): 
            for j in range(0, 3):
                # 坐标值分别是:左、上、右、底
                box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
                print(box)
                box_list.append(box)

        # 裁剪图片
        image_list = [image.crop(box) for box in box_list]

        return image_list

    def __fill_image(self, image):
        """
        将图片填充为正方形
        :param image:
        :return:
        """
        width, height = image.size

        # 长和宽较大值,作为新图片的宽高
        new_image_length = width if width > height else height

        # 生成新图片[纯白底]
        new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')

        # 将之前的图粘贴在新图上,居中
        # 如果原图宽大于高(横图),则填充图片的竖直维度
        if width > height:
            # (x,y)二元组表示粘贴上图相对下图的起始位置
            new_image.paste(image, (0, int((new_image_length - height) / 2)))
        else:
            # 如果原图宽小于高(竖图),则填充图片的水平纬度
            new_image.paste(image, (int((new_image_length - width) / 2), 0))
        return new_image

    def save_images(self, nine_images):
        """
        保存图片
        :param nine_images:
        :return:
        """
        index = 1
        for image in nine_images:
            image.save(str(index) + '.jpg')
            index += 1



有什么问题可以私信或者评论,如果您觉得还可以,可以动动手指帮小弟点个赞吗,不胜感激。

🎃特别介绍:

📣小白练手专栏,适合刚入手的新人欢迎订阅编程小白进阶
📣有什么不明白的欢迎私信或留言,得到细致讲解。另外想要进阶的朋友可以关注练手项目专栏

📣另外想学JavaWeb进厂的同学可以看看这个专栏:传送们
📣是个面试和考研的算法练习我们一起加油上岸之路

以上是关于python项目实战——❤️为女友写点东西❤️的主要内容,如果未能解决你的问题,请参考以下文章

❤️❤️❤️情人节必备,和女友一起玩新版飞机大战!万字只为你❤️❤️❤️收藏起来吧

OpenCV-Python实战——图像与视频文件的处理(两万字详解,️建议收藏️)

❤️肝了一个SpringBoot+Vue的招聘系统开源了还有教程强烈建议收藏❤️

❤️肝了一个SpringBoot+Vue的招聘系统开源了还有教程强烈建议收藏❤️

✨ 实战系列 ✨ 1️⃣ 微信小程序自动化测试实践(附 Python 源码)❤️

Python爬虫eval混淆,爬虫进阶实战系列