前段时间想给seo那边做个某工具站的爬虫,用到了图像识别(对方防守可谓严密啊,异步返回非标准json结构+referer+加密+图像四道防线.嘿嘿,代码就不放了.)
正好公司要给全站图片加水印,刚研究的图像识别又有用武之地了.
万事先谷歌,找到个加水印的代码,修改了一番就用上了.^ ^
1 import Image,ImageFilter,ImageEnhance 2 import os 3 4 def reduce_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 return im 15 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 in range(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 return Image.composite(layer, im, layer) 41 42 def fetchdir(path): 43 """Get the source list""" 44 return os.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 return imageslist 52 elif flag == 2: 53 imageslistraw = os.listdir(basepath + path) 54 imageslist = [] 55 for i in imageslistraw: 56 if not ‘_‘ in i: 57 imageslist.append(i) 58 return imageslist 59 60 def test(): 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 in fetchdir(detail_img_root_path): 68 for p in fetchfile(d, detail_img_root_path, 1): 69 impath = detail_img_root_path + d + ‘/‘ + p 70 #print impath 71 #print ‘openpic=========>>>‘ + impath 72 try: 73 im = Image.open(impath) 74 _, im_ysize = im.size 75 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 except IOError: 83 print ‘openfailed==============>‘ + impath 84 #""" 85 86 for d1 in fetchdir(product_img_root_path): 87 d1_path = product_img_root_path + d1 88 for d2 in fetchdir(d1_path): 89 d2_path = d1_path + ‘/‘ + d2 90 for p in fetchfile(d2, d1_path + ‘/‘, 2): 91 impath = d2_path + ‘/‘ + p 92 #print ‘openpic=========>>>‘ + d2_path + ‘/‘ + p 93 try: 94 im = Image.open(impath) 95 _, im_ysize = im.size 96 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 except IOError: 102 print ‘openfailed==============>‘ + impath 103 104 if __name__ == ‘__main__‘: 105 test()
后记:之间还发现0kb图片和开发代码误传到图片路径,后代码小改变成了一个图片安全检测工具,嘿嘿.(其实就是IO异常,PIL对不能成立的图片的异常也直接继承了IO异常)
时间: 2024-10-04 11:13:46