python切割图片发微信朋友圈——9图、4图、6图

微信朋友圈的折叠方案,基本只能支持这几种折叠方案了。

随便拿张图,扔到白背景,拉到正方形。

3*3和2*2都是标准的正方形,2*3不行,微信会自动缩图,正方形的要把图做图放里边会丢do宽,东西,拉宽,横纵比3比2,就可以了。

最后,还可以用切完的图和其他图去组合发,只要排版满足,都能用

3*3

#from Pillow import Image
from PIL import Image
import sys

def fill_image(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:
                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 cut_image(image):
        width,height = image.size
        item_width = int(width / 3)
        box_list = []
        for i in range(0,3):
                for j in range(0,3):
                        print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
                        box = ((j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width))
                        box_list.append(box)
        image_list = [image.crop(box) for box in box_list]
        return image_list

def save_images(image_list):
        index = 1
        for image in image_list:
#               image.show()
                image.save(str(index) + '.png','PNG')
                index += 1

if __name__ == '__main__':
        file_path = "dog.jpg"
        image = Image.open(file_path)
        image.show()
        image = fill_image(image)
        image.show()
        image_list = cut_image(image)
        save_images(image_list)

2*2

#from Pillow import Image
from PIL import Image
import sys

def fill_image(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:
                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 cut_image(image):
        width,height = image.size
        item_width = int(width / 2)
        box_list = []
        for i in range(0,2):
                for j in range(0,2):
                        print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
                        box = ((j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width))
                        box_list.append(box)
        image_list = [image.crop(box) for box in box_list]
        return image_list

def save_images(image_list):
        index = 1
        for image in image_list:
#               image.show()
                image.save(str(index) + '.png','PNG')
                index += 1

if __name__ == '__main__':
        file_path = "dog.jpg"
        image = Image.open(file_path)
        image.show()
        image = fill_image(image)
        image.show()
        image_list = cut_image(image)
        save_images(image_list)

2*3

#from Pillow import Image
from PIL import Image
import sys
#2*3

def fill_image(image):
        width,height = image.size
        new_image_length = width if width > height else height
        new_image = Image.new(image.mode, (int(new_image_length * 3 / 2),int(new_image_length)),color='white')
        print("new width:",new_image.width)
        print("new height:",new_image.height)
        if width > height:
                new_image.paste(image,(0,int((new_image_length - height)/2)))
        else:
                new_image.paste(image,(int((int(new_image_length * 3 / 2) - width)/2),0))
        return new_image
def cut_image(image):
        width,height = image.size
        #item_width = int(width / 3)
        item_width = int(width / 3)
        item_height = int(height / 2)
        box_list = []
        for i in range(0,2):
                for j in range(0,3):
                        print((j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height))
                        box = ((j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height))
                        box_list.append(box)
        image_list = [image.crop(box) for box in box_list]
        return image_list

def save_images(image_list):
        index = 1
        for image in image_list:
                #image.show()
                image.save(str(index) + '.png','PNG')
                index += 1

if __name__ == '__main__':
        file_path = "dog.jpg"
        image = Image.open(file_path)
        image = fill_image(image)
        image_list = cut_image(image)
        save_images(image_list)

猜你喜欢

转载自blog.csdn.net/huqinweI987/article/details/81990327