Python-Written bulk image watermark Tool

Source: Internet
Author: User

Some time ago want to give SEO over there to do a tool station crawler, using the image recognition (opposing defense is tight ah, asynchronous return to the non-standard JSON structure +referer+ encryption + image four lines of defense.) Hey, the code will not be released.

Just the company to give the whole station picture watermark, just study the image recognition has a useful.

million in advance Google, found a watermark code, modified it to use. ^ ^

1 Importimage,imagefilter,imageenhance2 ImportOS3 4 defreduce_opacity (IM, opacity):5     """Returns an image with reduced opacity."""6     assertOpacity >= 0 andOpacity <= 17     ifIm.mode! ='RGBA':8im = Im.convert ('RGBA')9     Else:Tenim =im.copy () OneAlpha = Im.split () [3] AAlpha =imageenhance.brightness (Alpha). Enhance (opacity) - Im.putalpha (Alpha) -     returnim the  - defWatermark (IM, mark, Position, Opacity=1): -     """Adds a watermark to an image.""" -     ifOpacity < 1: +Mark =reduce_opacity (Mark, opacity) -     ifIm.mode! ='RGBA': +im = Im.convert ('RGBA') A     #Create a transparent layer the size of the image and draw the at     #watermark in that layer. -Layer = Image.new ('RGBA', Im.size, (0,0,0,0)) -     ifPosition = ='Tile': -          forYinchRange (0, im.size[1], mark.size[1]): -              forXinchRange (0, im.size[0], mark.size[0]): - Layer.paste (Mark, (x, y)) in     elifPosition = =' Scale': -         #Scale , but preserve the aspect ratio toRatio =min ( +Float (im.size[0])/mark.size[0], float (im.size[1])/mark.size[1]) -w = Int (mark.size[0] *ratio) theh = Int (mark.size[1] *ratio) *Mark =Mark.resize ((w, h)) $Layer.paste (Mark, ((im.size[0)-W)/2, (Im.size[1]-h)/2))Panax Notoginseng     Else: - Layer.paste (mark, position) the     #Composite the watermark with the layer +     returnimage.composite (layer, IM, layer) A  the defFetchdir (path): +     """Get the source list""" -     returnos.listdir (path) $  $ defFetchfile (Path, BasePath, flag):#flag:1 detail,2 Product -     """Get the source images path.""" -     ifFlag = = 1: theImageslist = Os.listdir (BasePath +path) -         delImageslist[imageslist.index ('index.htm')]Wuyi         returnimageslist the     elifFlag = = 2: -Imageslistraw = Os.listdir (BasePath +path) WuImageslist = [] -          forIinchImageslistraw: About             if  not '_' inchI: $ imageslist.append (i) -         returnimageslist -  - defTest (): ABasePath ='/var/img_test/' +Detail_img_root_path = BasePath +'upload/' theProduct_img_root_path = BasePath +'2014/' -Mark = Image.open ('/var/img_test/watermark.png') $  the     #""" the      forDinchFetchdir (detail_img_root_path): the          forPinchFetchfile (d, Detail_img_root_path, 1): theImpath = Detail_img_root_path + D +'/'+P -             #Print Impath in             #print ' openpic=========>>> ' + impath the             Try: theim =Image.open (Impath) About_, Im_ysize =im.size theSavepath = BasePath +'marked/upload/' the                 if  notOs.path.isdir (Savepath +d): theOs.makedirs (Savepath +d) +MARKEDIMG = Watermark (IM, mark, (506, im_ysize-67), 0.8). CONVERT ('RGB')#3:2:596:47 1:476*86 -Savepath = Savepath + D +'/'+ P#. replace (' jpg ', ' png ') the                 #markedimg.show ()Bayi Markedimg.save (Savepath) the             exceptIOError: the                 Print 'openfailed==============>'+Impath -      #""" -  the      forD1inchFetchdir (product_img_root_path): theD1_path = Product_img_root_path +D1 the          forD2inchFetchdir (d1_path): theD2_path = D1_path +'/'+D2 -              forPinchFetchfile (D2, D1_path +'/', 2): theImpath = D2_path +'/'+P the                 #print ' openpic=========>>> ' + D2_path + '/' + P the                 Try:94im =Image.open (Impath) the_, Im_ysize =im.size theSavepath = BasePath +'marked/2014/' the                     if  notOs.path.isdir (Savepath + d1 +'/'+D2):98Os.makedirs (Savepath + d1 +'/'+D2) AboutMARKEDIMG = Watermark (IM, mark, (506, im_ysize-67), 0.8). CONVERT ('RGB') -Markedimg.save (Savepath + d1 +'/'+ D2 +'/'+p)101                 exceptIOError:102                     Print 'openfailed==============>'+Impath103 104 if __name__=='__main__': theTest ()

PostScript: Also found between 0kb pictures and development code misinformation to the picture path, after the code changed into a picture security detection tool, hey. (In fact, the IO exception, PIL to the exception of the image can not be set directly inherit the IO exception)

Python-Written bulk image watermark Tool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.