Getting Started with Python image processing library Pillow - Add a thumbnail on the original

On the original:

Getting Started with Python image processing library Pillow - Add a thumbnail on the original

Here I would like to join in the upper left corner of a thumbnail picture, will make the layered look some reason that:

# _*_ coding:utf-8 _*_

from PIL import Image

__author__ = 'admin'

'''
    在原图上加入缩略图
'''

def thumbnail(im):
    #   缩略图大小
    size = (200, 200)
    try:
        #   创建后的缩略图大小不一定就是200x200的,是按照原图比例来的
        im.thumbnail(size)
        return im
    except IOError:
        print(u"无法创建缩略图!")


def mosaic(im):
    im0 = im.copy()
    width_im, height_im = im0.size
    #   调用thumbnail创建缩略图
    im_thumbnail = thumbnail(im0)
    #   获取缩略图大小
    width_thumbnail, height_thumbnail = im_thumbnail.size
    #   避免出现原图比缩略图尺寸还小的情况
    if width_im > width_thumbnail and height_im > height_thumbnail:
        im.paste(im_thumbnail, (0, 0, width_thumbnail, height_thumbnail))
    return im

im = Image.open(r'linuxidc.com.png')
mosaic(im).show()

Renderings:

Getting Started with Python image processing library Pillow - Add a thumbnail on the original

Let me during my worry is not directly in the original basis for creating thumbnails on the thumbnail and then back into the picture! Because I will create a thumbnail picture is this:

At this time im im not the original, and surely we already know, at this time im already a thumbnail of the
last I learned a method that is

im.copy()

Want to copy a few to copy a few, it will not affect the original image.

Guess you like

Origin www.linuxidc.com/Linux/2020-01/162138.htm