matplotlib.pyplot 仿射变换 倾斜扭曲

matplotlib.pyplot对图片进行倾斜和扭曲处理

import math
import matplotlib.pyplot as plt
from PIL import Image


if __name__ == '__main__':
    img = Image.open('./example.png').resize((128, 128))
    params = [ # 参数
        [0.5, 0.0, 0.0, 0.0, 1.0, 0.0],
        [1.5, 0.0, 0.0, 0.0, 1.0,  0.0],

        [1.0, 0.5, 0.0, 0.0, 1.0, 0.0],
        [1.0, 1.5, 0.0, 0.0, 1.0, 0.0],

        [1.0, 0.0, 0.0, 0.5, 1.0, 0.0],
        [1.0, 0.0, 0.0, 1.5, 1.0, 0.0],

        [1.0, 0.0, 0.0, 0.0, 0.5, 0.0],
        [1.0, 0.0, 0.0, 0.0, 1.5, 0.0],

        [1.0, 0.0, 0.0, 0.0, 1.0,  64],
        [1.0, 0.0, 0.0, 0.0, 1.0, -64],

        [1.0, 0.0,  64, 0.0, 1.0, 0.0],
        [1.0, 0.0, -64, 0.0, 1.0, 0.0],
    ]
    plt.rcParams['figure.figsize'] = (8.0, 8.0)
    plt.subplots(
        3,
        math.ceil(len(params)/3),
        constrained_layout=True
    )
    for i in range(len(params)):
        plt.subplot(
            3,
            math.ceil(len(params)/3),
            i+1
        )
        plt.imshow(img.transform((128, 128), Image.AFFINE, params[i]))
        plt.title(
            '['+(', '.join(['%.1f' % e for e in params[i]]))+']',
            fontsize=7
        )
    plt.show()

不同参数值产生的效果

欸呦图丢了

猜你喜欢

转载自blog.csdn.net/dscn15848078969/article/details/120407213
今日推荐