# 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("完成了...")
python裁剪图片并粘贴到另一图片上(批量处理)
最新推荐文章于 2024-06-07 10:39:29 发布