资源描述:
《pil python图像处理模块》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、PythonImageLibraryPIL10/13/05PIL–Whatisit?PythoniclibraryforLoadingandSavingimages(diverseformats)Scaling,cropping,compositing,transforms,bands(e.g.alphachannel)DrawingtextandbasicshapesAkintotheprogramGIMP(*GIMPalsohasanicepythonAPIcalledGIMP-Python)PIL-ExamplesImageresi
2、zingorformatconversionsAddingsave/loadsupportintoyourapplication*FontsupportalsohandyChartgeneration(alsoseePIDDLEpiddle.sf.net)ThumbnailgenerationWatermarkingoraddinglegendPILAPI-ConceptsImage–fromfileornewlycreated>>>importImage>>>im=Image.open(“vacation.jpeg")>>>newIm=I
3、mage.new(“RGBA”,(640,480),(255,0,0))Format–fileformat>>>printim.mode,im.size,im.formatRGB(1024,768)JPEG>>>im.save(“vacation.png”)OR>>>im.save(“vacation.png”,“PNG”)PILAPI-ConceptsResamplingFiltersNEAREST,BILINEAR,BICUBIC,ANTIALIAS>>>smallIm=im.resize((128,128),Image.ANTIALI
4、AS)Sizes–tupleofwidthandheightBoundingboxes–tuplesofx1,y1,x2,y20,0isalwaysupperleftPILAPI-ConceptsMode–grayscale“L”,color“RGBA”“RGB”,etc.1(1-bitpixels,blackandwhite,storedwithonepixelperbyte)L(8-bitpixels,blackandwhite)P(8-bitpixels,mappedtoanyothermodeusingacolourpalette)
5、RGB(3x8-bitpixels,truecolour)RGBA(4x8-bitpixels,truecolourwithtransparencymask)CMYK(4x8-bitpixels,colourseparation)YCbCr(3x8-bitpixels,colourvideoformat)I(32-bitintegerpixels)F(32-bitfloatingpointpixels)Color–specifiedas32bitvalue,tuple,orstringmyColor=(255,0,0,128)#fullre
6、d,with50%alphamyColor=0x7f0000ff#fullred,with50%alphamyColor=“#00ff00”#webcolormyColor=“rgb(75%,0%,0%)”myColor=“hsl(0,100%,50%)”PILAPI-ConceptsBands–theseparatecolorchannels>>>bands=im.split()>>>rIm=bands[0]>>>aIm=bands[3]>>>remadeImage=Image.merge(“RGBA”,(rIm,gIm,bIm,aIm)
7、)>>>grayscaleIm=Image.open(“myAlpha.gif”)>>>remadeImage=myImage.putalpha(grayscaleIm)#replacethealphabandPIL–Slicing&DicingThumbnailsModifiesin-placePreservesaspectratio>>>myImage.thumbnail((128,128),Image.ANTIALIAS)Crop>>>bounds=(100,100,400,400)>>>cutoutIm=myImage.crop(b
8、ounds)PastePIL–Slicing&DicingBlend/compositeRotateTransposeROTATE_90/180/270,FLIP_TOP_BOT