Background
Recently seen in the Python workbook, a small program every day this project, very interesting, but also more practical.
In the evening read the No. 000 question, about the Python image processing:
Add a red number to the top right corner of your QQ head (or Weibo avatar), similar to the number of unread messages that prompt the effect. Similar to the effect in the diagram
Before the use of PiL Library, in the generation of verification code and identification verification code also need to understand this knowledge, hands-on try to practice.
PIL
The Python Imaging Library adds image processing capabilities to your Python interpreter. This library provides a wide range of file format support, efficient internal representations, and fairly powerful image processing capabilities.
Documentation in this: http://omz-software.com/pythonista/docs/ios/PIL.html
Ideas
The actual meaning of the title is to add a watermark to the picture, which can be divided into the following 2 steps:
- "Turn" the text into a picture
- "Overlay" The resulting watermark picture with the original
The principle is almost like this, the specific processing also has to use PIL.
Finally put the code:
#!/usr/bin/env python#-*-Coding:utf-8-*-# @Date: 2014-11-29 19:09:59# @Author: Linsir ([email protected])# @Link: http://Linsir.sinaapp.comImport Image, Imageenhance, Imagedraw, ImagefontDefText2img(Text,Font_color="Blue",Font_size=25): "" To generate a watermark "" "with text contentFont= Imagefont.TrueType(' SIMSUN.TTC ',Font_size) #多行文字处理Text=Text.Split(' \ n ')Mark_width= 0 ForIInchRange(Len(Text)): (Width,Height) =Font.GetSize(Text[I]) IfMark_width<Width:Mark_width=Width mark_height=Height*Len(Text) #生成水印图片Mark= Image.New(' RGBA ', (Mark_width,Mark_height))Draw= Imagedraw.Imagedraw(Mark, "RGBA")Draw.SetFont(Font) ForIInchRange(Len(Text)): (Width,Height) =Font.GetSize(Text[I])Draw.Text((0,I*Height),Text[I],Fill=Font_color) ReturnMarkDefSet_opacity(Im,Opacity): "" Set Transparency "" " AssertOpacity>=0 andOpacity< 1 IfIm.Mode!= "RGBA":Im=Im.Convert(' RGBA ') Else:Im=Im.Copy()Alpha=Im.Split()[3]Alpha= Imageenhance.Brightness(Alpha).Enhance(Opacity)Im.Putalpha(Alpha) ReturnImDefWatermark(Im,Mark,Position,Opacity=1): "" Add Watermark "" " Try: IfOpacity< 1:Mark=Set_opacity(Mark,Opacity) IfIm.Mode!= ' RGBA ':Im=Im.Convert(' RGBA ') IfIm.Size[0] <Mark.Size[0] OrIm.Size[1] <Mark.Size[1]: Print "The mark image size is larger size than original image file." Return False #设置水印位置 IfPosition== ' Left_top ':X= 0Y= 0 ElifPosition== ' Left_bottom ':X= 0Y=Im.Size[1] -Mark.Size[1] ElifPosition== ' Right_top ':X=Im.Size[0] -Mark.Size[0]Y= 0 ElifPosition== ' Right_bottom ':X=Im.Size[0] -Mark.Size[0]Y=Im.Size[1] -Mark.Size[1] Else:X= (Im.Size[0] -Mark.Size[0]) / 2Y= (Im.Size[1] -Mark.Size[1]) / 2Layer=Image.New(' RGBA ',im.size,)Layer.Paste(Mark,(x,y))returnImage.Composite(Layer,im,Layer)exceptException ase:Print">>>>>>>>>>> WaterMark EXCEPTION:"+Str(e)returnFalsedefMain():text=u' Linsir. Watermark. \[email protected] '# Text = open (' readme.md '). Read (). Decode (' Utf-8 ')# Print Textim=Image.Open(' Origal.png ')Mark=text2img(text)Image=Watermark(im,Mark,' Center ',0.9)ifImage:Image.Save(' Watermark.png ')Image.Show()Else:Print"Sorry, Failed."if__name__==' __main__ ':Main()
In fact, this code, you can do something like: Python generated image verification code, 10 lines of code to determine the color situation picture, Python generated pictures, the growth of micro-blog and so on.
Enjoy it.
Python workbook--pil processing images plus watermarks