python裁剪图片并粘贴到另一图片上(批量处理)

# python裁剪图片并粘贴到另一图片上(批量处理)
# author: zhou jinxing
import os
import PIL.Image as Image

def cropImage(srcPath,dstPath,counter,imgTemp):
    # 读取图片
    img_read = Image.open(srcPath)
    #print(img_read.size,imgTemp.size)
    
    # 情况1
    if img_read.size[0] < imgTemp.size[0] and img_read.size[1] > imgTemp.size[1]:
        #print('zhou')
        # 设置裁剪的位置
        tmp = 120;
        crop_box = (0,tmp,img_read.size[0],tmp + imgTemp.size[1])
        # 裁剪图片
        img_crop = img_read.crop(crop_box)
        imgTemp.paste(img_crop,(0,0))
        imgTemp.save(dstPath + str(counter) + '.jpg') 
    elif img_read.size[0] > imgTemp.size[0] and img_read.size[1] > imgTemp.size[1]:
        #print('zhou')
        # 设置裁剪的位置
        w_tmp = 300;
        h_tmp = 300;
        crop_box = (w_tmp,h_tmp, w_tmp + imgTemp.size[0],h_tmp + imgTemp.size[1])
        # 裁剪图片
        img_crop = img_read.crop(crop_box)
        imgTemp.paste(img_crop,(0,0))
        imgTemp.save(dstPath + str(counter) + 'a.jpg') 
        
        w_tmp = 300;
        h_tmp = 600;
        crop_box = (w_tmp,h_tmp, w_tmp + imgTemp.size[0],h_tmp + imgTemp.size[1])
        # 裁剪图片
    img_crop = img_read.crop(crop_box)
        imgTemp.paste(img_crop,(0,0))
        imgTemp.save(dstPath + str(counter) + 'b.jpg')    
        
        w_tmp = 300;
        h_tmp = 900;
        crop_box = (w_tmp,h_tmp, w_tmp + imgTemp.size[0],h_tmp + imgTemp.size[1])
        # 裁剪图片
        img_crop = img_read.crop(crop_box)
        imgTemp.paste(img_crop,(0,0))
        imgTemp.save(dstPath + str(counter) + 'c.jpg')         
        pass  
    

if __name__ == '__main__':
    t_w = 1242
    t_h = 375
    listPath  = '/workspace/tlt-experiments/data_fire_2/step_1_src'
    srcPath   = '/workspace/tlt-experiments/data_fire_2/step_1_src/'
    dstPath   = '/workspace/tlt-experiments/data_fire_2/step_2_sort/'   
    
    # 图片颜色
    color = [100,100,100]
    # 生成背景图片
    image_temp = Image.new('RGB',(1242,375),(color[0],color[1],color[2]))
    
    print("开始转换...")
    filename_list = os.listdir(listPath)
    myCounter = 0;
    for d in filename_list:
        if d.count('.jpg') > 0:
            #changeJpgToPng(t_w, t_h,srcPath + d,dstPath + d)
            cropImage(srcPath + d, dstPath, myCounter, image_temp)
            myCounter += 1;
        pass
    print("完成了...")
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值