python批量去除图片水印_python写的批量图片水印工具

1 importImage,ImageFilter,ImageEnhance2 importos3

4 defreduce_opacity(im, opacity):5 """Returns an image with reduced opacity."""

6 assert opacity >= 0 and opacity <= 1

7 if im.mode != 'RGBA':8 im = im.convert('RGBA')9 else:10 im =im.copy()11 alpha = im.split()[3]12 alpha =ImageEnhance.Brightness(alpha).enhance(opacity)13 im.putalpha(alpha)14 returnim15

16 def watermark(im, mark, position, opacity=1):17 """Adds a watermark to an image."""

18 if opacity < 1:19 mark =reduce_opacity(mark, opacity)20 if im.mode != 'RGBA':21 im = im.convert('RGBA')22 #create a transparent layer the size of the image and draw the

23 #watermark in that layer.

24 layer = Image.new('RGBA', im.size, (0,0,0,0))25 if position == 'tile':26 for y in range(0, im.size[1], mark.size[1]):27 for x inrange(0, im.size[0], mark.size[0]):28 layer.paste(mark, (x, y))29 elif position == 'scale':30 #scale, but preserve the aspect ratio

31 ratio =min(32 float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])33 w = int(mark.size[0] *ratio)34 h = int(mark.size[1] *ratio)35 mark =mark.resize((w, h))36 layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2))37 else:38 layer.paste(mark, position)39 #composite the watermark with the layer

40 returnImage.composite(layer, im, layer)41

42 deffetchdir(path):43 """Get the source list"""

44 returnos.listdir(path)45

46 def fetchfile(path, basepath, flag): #flag: 1 detail,2 product

47 """Get the source images path."""

48 if flag == 1:49 imageslist = os.listdir(basepath +path)50 del imageslist[imageslist.index('index.htm')]51 returnimageslist52 elif flag == 2:53 imageslistraw = os.listdir(basepath +path)54 imageslist =[]55 for i inimageslistraw:56 if not '_' ini:57 imageslist.append(i)58 returnimageslist59

60 deftest():61 basepath = '/var/img_test/'

62 detail_img_root_path = basepath + 'upload/'

63 product_img_root_path = basepath + '2014/'

64 mark = Image.open('/var/img_test/watermark.png')65

66 #"""

67 for d infetchdir(detail_img_root_path):68 for p in fetchfile(d, detail_img_root_path, 1):69 impath = detail_img_root_path + d + '/' +p70 #print impath

71 #print 'openpic=========>>>' + impath

72 try:73 im =Image.open(impath)74 _, im_ysize =im.size75 savepath = basepath + 'marked/upload/'

76 if not os.path.isdir(savepath +d):77 os.makedirs(savepath +d)78 markedimg = watermark(im, mark, (506, im_ysize - 67), 0.8).convert('RGB') #3: 2:596:47 1:476*86

79 savepath = savepath + d + '/'+ p #.replace('jpg', 'png')

80 #markedimg.show()

81 markedimg.save(savepath)82 exceptIOError:83 print 'openfailed==============>' +impath84 #"""

85

86 for d1 infetchdir(product_img_root_path):87 d1_path = product_img_root_path +d188 for d2 infetchdir(d1_path):89 d2_path = d1_path + '/' +d290 for p in fetchfile(d2, d1_path + '/', 2):91 impath = d2_path + '/' +p92 #print 'openpic=========>>>' + d2_path + '/' + p

93 try:94 im =Image.open(impath)95 _, im_ysize =im.size96 savepath = basepath + 'marked/2014/'

97 if not os.path.isdir(savepath + d1 + '/' +d2):98 os.makedirs(savepath + d1 + '/' +d2)99 markedimg = watermark(im, mark, (506, im_ysize - 67), 0.8).convert('RGB')100 markedimg.save(savepath + d1 + '/' + d2 + '/' +p)101 exceptIOError:102 print 'openfailed==============>' +impath103

104 if __name__ == '__main__':105 test()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值