The PIL module in Python is designed to do this thing.
A pattern
Original picture
Action one: thumbnails (usually not in this way, because the picture quality damage is too large)
Action two: Rotate a part of the picture
Action Three: Add a picture watermark to the picture, 2 layers merged
Operation four: To add a text watermark to the picture, this use more, I got a white light down here, can be made completely transparent
Operation V-ratio compression (more suitable for thumbnail images)
Operation six according to the proportion of the cut, and so on compression, sometimes need a proportional picture can do this
Two codes
#-*-encoding=utf-8-*-" "Author:orangleliupil processing pictures, validation, processing size, format filtering compression, conversion picture library best with pillow and a test picture test.jpg, a log picture, a font file" "#the basic parameter of the picture getsTry: fromPILImportImage, Imagedraw, Imagefont, ImageenhanceexceptImporterror:ImportImage, Imagedraw, Imagefont, ImageenhancedefCompress_image (IMG, w=128, h=128): " "thumbnail Images" "Img.thumbnail ((w,h)) Im.save ('Test1.png','PNG') PrintU'successfully saved to PNG format, compressed to 128*128 format Picture'defcut_image (IMG):" ", rotate, then paste" " #EFT, Upper, right, lower #x y z w x, y is the starting point, z,w is the offset valuewidth, height =img.size Box= (width-200, height-100, width, height) region=img.crop (Box)#Rotation AngleRegion =region.transpose (image.rotate_180) img.paste (Region, Box) Img.save ('test2.jpg','JPEG') PrintU're-puzzle success'defLogo_watermark (IMG, logo_path):" "Add a picture watermark, the principle is to merge layers, with PNG better" "Baseim=img Logoim=Image.open (logo_path) BW, BH=baseim.size LW, LH=logoim.size Baseim.paste (Logoim, (BW-LW, bh-LH)) Baseim.save ('test3.jpg','JPEG') PrintU'logo watermark Combination Success'defText_watermark (img, text, out_file="test4.jpg", angle=23, opacity=0.50): " "Add a text watermark, make a transparent watermark appearance, should be a PNG layer merge http://www.pythoncentral.io/watermark-images-python-2x/here will produce the famous Importerror ( "The _IMAGINGFT C module is not installed") error Pillow install to fix pip install Pillow" "Watermark= Image.new ('RGBA', Img.size, (255,255,255))#I have a layer of white film here, remove (255,255,255) This parameter is good.FONT="Msyh.ttf"size= 2N_font= Imagefont.truetype (FONT, size)#Get FontsN_width, n_height =n_font.getsize (text) Text_box= Min (watermark.size[0], watermark.size[1]) while(N_width+n_height <text_box): Size+ = 2N_font= Imagefont.truetype (FONT, size=size) n_width, N_height= N_font.getsize (text)#text zooms in, but less than the width and height of the pictureText_width= (Watermark.size[0]-n_width)/2Text_height= (Watermark.size[1]-n_height)/2#watermark = watermark.resize ((text_width,text_height), Image.antialias)Draw = Imagedraw.draw (watermark,'RGBA')#Add a brush to the watermark layerDraw.text ((text_width,text_height), text, font=n_font, fill="#21ACDA") Watermark=watermark.rotate (angle, image.bicubic) Alpha= Watermark.split () [3] Alpha=imageenhance.brightness (Alpha). Enhance (opacity) Watermark.putalpha (Alpha) image.composite (Watermark, IMG, wate Rmark). Save (Out_file,'JPEG') PrintU"Text watermark Success"#equal proportions Compress picturesdefResizeimg (IMG, dst_w=0, dst_h=0, qua=85): " "only give the width or height, or two to give, and then take the appropriate proportion if the picture is smaller than the size to be compressed, it does not compress" "Ori_w, Ori_h=im.size WidthRatio= HeightRatio =None ratio= 1if(Ori_w andOri_w > Dst_w)or(Ori_h andOri_h >dst_h):ifDst_w andOri_w >Dst_w:widthratio= Float (dst_w)/Ori_w#how to get decimals correctly ifDst_h andOri_h >Dst_h:heightratio= Float (dst_h)/Ori_hifWidthRatio andHeightRatio:ifWidthRatio <Heightratio:ratio=WidthRatioElse: Ratio=HeightRatioifWidthRatio and notHeightratio:ratio=WidthRatioifHeightRatio and notWidthratio:ratio=HeightRatio newwidth= Int (Ori_w *ratio) Newheight= Int (Ori_h *ratio)Else: Newwidth=Ori_w newheight=ori_h im.resize ((newwidth,newheight), Image.antialias). Save ("test5.jpg","JPEG", quality=qua)PrintU'equal to compression complete' " "Image.antialias also has the following values: Nearest:use NEAREST neighbour Bilinear:linear interpolation in a 2x2 environment Bicubic:cubic spline interpolation in a 4x4 environment Antialias:best down-sizing filter" "#Crop Compressed PicturesdefClipresizeimg (IM, Dst_w, Dst_h, qua=95): " "First cut the picture according to a scale, and then compress to a specified size an image 16:5, compressed to 2:1 and a width of 200, you must first cut the picture to 10:5, and then in the ratio of compression" "Ori_w,ori_h=im.size Dst_scale= Float (dst_w)/Dst_h#target aspect ratioOri_scale = float (ori_w)/Ori_h#Original aspect ratio ifOri_scale <=Dst_scale:#too highwidth =ori_w Height= Int (width/dst_scale) x=0 y= (ori_h-height)/2Else: #too wideHeight =ori_h Width= Int (height*dst_scale) x= (ori_w-width)/2y=0#croppingbox = (x,y,width+x,height+y)#the parameters here can be thought of: starting from the (x, y) coordinates of a graph, cutting to (width+x,height+y) coordinates #surrounded by images, the crop method is quite different from the Imagecopy method in PHPNewim =im.crop (box) IM=None#CompressionRatio = Float (dst_w)/width newwidth= Int (Width *ratio) Newheight= Int (Height *ratio) Newim.resize ((newwidth,newheight), Image.antialias). Save ("test6.jpg","JPEG", quality=95) Print "Old size%s%s"%(Ori_w, Ori_h)Print "New size%s%s"%(Newwidth, Newheight)PrintU"after trimming, the compression is completed ."if __name__=="__main__": " "mainly to implement the function, the code is not how to organize" "im= Image.open ('test.jpg')#Image Objectcompress_image (IM) im= Image.open ('test.jpg')#Image Objectcut_image (IM) im= Image.open ('test.jpg')#Image ObjectLogo_watermark (IM,'logo.png') im= Image.open ('test.jpg')#Image ObjectText_watermark (IM,'Orangleliu') im= Image.open ('test.jpg')#Image ObjectResizeimg (IM, dst_w=100, qua=85) im= Image.open ('test.jpg')#Image ObjectClipresizeimg (IM, 100, 200)
Python----simple image processing (PIL or Pillow)