猫史档案馆


【Python作品分享】图片组成图片【作品秀】

用户:雪球July雪球July查看:0 回复:1 评论:0 创建时间:2021-12-20T22:45:32


【作品展示】

center_image

 

【作品介绍】

额......说明看第一期。这只是最新(唯一能用)的程序。

 

【作品源代码】

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

# 请在下面的二维列表里输入图像
style = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
    [0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
    [0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
    [0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0],
    [0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0],
    [0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

wide_x = 300
high_y = 300


def my_paste(style, myDir, save_name, common_nam, wide_x, high_y):
    files = os.listdir(myDir)
    print(files)
    images_ext = [
        '.png', '.jpg', '.bmp', '.jpeg', '.gif'
    ]
    images = []
    for file in files:
        name, ext = os.path.splitext(file)
        if ext in images_ext and common_nam in name:
            img = Image.open(myDir + os.sep + file)
            _img = img.resize((wide_x, high_y))
            images.append(_img)
    print(images)
    col = len(style)
    row = len(style[0])
    #
    board = Image.new("RGBA", (high_y*col, wide_x*row))
    for i in range(row):
        for j in range(col):
            if style[i][j] == 1:
                index = j + col * i
                board.paste(images[index % len(images)], (j*high_y, i*wide_x))

    plt.imshow(board)
    plt.show()
    board.save(save_name + ".png")

"""
    调用函数,请依次输入:
    style, 
    图片所在文件夹(与程序同目录下), 
    要保存的图片名,
    图片名的共同点(名字里都有的字符), 
    wide_x, 
    high_y
"""
my_paste(
    style, 
    "图片", 
    "胡", 
    "雪球", 
    wide_x, 
    high_y
)

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
弑血残狼弑血残狼

imgsr喵demao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E6%90%93%E5%A4%B4.gif"alt="emotion_编程猫_厉害"

点赞0


评论