zoukankan      html  css  js  c++  java
  • Python_code_实现贴图功能

    
    
    注:在函数Image_tietu()中
    需要指定的前景图片共计右多少张这个参数 image_count
    才能合理的实现随机读取前景图片的目的
     

    代码:

    import os
    import glob
    import random
    from PIL import Image

    def Image_tietu(img_ditu,img_path,p1,p2,p3,p4,position_y):
    #读取底图(背景图)图片
    img = Image.open(img_ditu)

    for root,dirs,files in os.walk(img_path):
    #使用函数读取指定目录及其子目录下的以 png 为结尾的图片文件
    imgs_list = glob.glob(root + '/*.png') #元素下标从0开始
    print(imgs_list)
    #前景图片共计个数
    image_count = 3

    for i in range(1): #此处循环中 range() 中的参数为产生几张结果图像
    #产生随机数;读取指定索引下的前景图片
    temp1 = random.randint(1,image_count);
    temp2 = random.randint(1,image_count);
    temp3 = random.randint(1,image_count);
    temp4 = random.randint(1,image_count);
    img1 = Image.open(imgs_list[temp1])
    img2 = Image.open(imgs_list[temp2])
    img3 = Image.open(imgs_list[temp3])
    img4 = Image.open(imgs_list[temp4])
    #img是背景图片,img1等是前景图片,下面四句话的所用是将前景图片贴到背景图片上
    img.paste(img1,(p1,position_y,p1+img1.size[0],position_y+img1.size[1]))
    img.paste(img2,(p2,position_y,p2+img2.size[0],position_y+img2.size[1]))
    img.paste(img3,(p3,position_y,p3+img3.size[0],position_y+img3.size[1]))
    img.paste(img4,(p4,position_y,p4+img4.size[0],position_y+img4.size[1]))

    #下标从0开始,前景图片的第15~18为对应的为具体的数字,
    #数字位数这么多的原因是因为这是总的相对路径,并不只是文件名
    #print(imgs_list[temp1][15:19])
    print(imgs_list[temp1]) #输出样例: ..dataimages\_020a_0.png

    a = [] #定义数组
    #以下部分是存储标记区域对应的类标
    a.append(imgs_list[temp1][15])
    a.append(imgs_list[temp1][16])
    a.append(imgs_list[temp1][17])
    a.append(imgs_list[temp1][18])

    a.append(imgs_list[temp2][15])
    a.append(imgs_list[temp2][16])
    a.append(imgs_list[temp2][17])
    a.append(imgs_list[temp2][18])

    a.append(imgs_list[temp3][15])
    a.append(imgs_list[temp3][16])
    a.append(imgs_list[temp3][17])
    a.append(imgs_list[temp3][18])

    a.append(imgs_list[temp4][15])
    a.append(imgs_list[temp4][16])
    a.append(imgs_list[temp4][17])
    a.append(imgs_list[temp4][18])
    # 下标 0-15 共计16个数字 _ 表示该位置没有数字
    print(a)#输出样例:['_', '0', '2', '0', '_', '0', '2', '3', '_', '0', '2', '0', '_', '0', '2', '0']


    new_filename = img_ditu[:-4] + '__'+ str(i) +'.png'
    print(new_filename)
    img.save(new_filename);


    '''主函数部分'''
    #前景图片所在目录
    img_path = '..\data\images'

    #背景图片所在目录
    img_ditu = '..\data\background\008001.png';

    #相关位置参数
    p1=50;p2=185;p3=320;p4=455; position_y = 230

    #执行贴图函数
    Image_tietu(img_ditu, img_path, p1, p2, p3, p4,position_y);

  • 相关阅读:
    mysql查看进程
    mysql case, if
    centos升级python2.7
    centos多版本python安装pip
    Python library not found: libpython2.7mu.so.1.0
    pip cannot confirm SSL certificate: SSL module is not available
    python: no module named bz2
    summary
    python生成可执行文件保护源码
    mysql 存储过程
  • 原文地址:https://www.cnblogs.com/lyj0123/p/11245271.html
Copyright © 2011-2022 走看看