本篇文章為大家展示了Pillow模塊怎么在python中使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
0、使用pip安裝
$ pip install pillow
1、使用easy_install
$ easy_install pillow
2、通過(guò)pycharm安裝
1、學(xué)習(xí)并使用pillow庫(kù)
#導(dǎo)入模塊 from PIL import Image #讀取文件 img = Image.open('test.jpg') #保存文件 #img.save(filename,format) img.save(filename,"JPEG") #獲取圖片大小 (width,height) = img.size #獲取圖片的源格式 img_format = img.format #獲取圖片模式,有三種模式:L(灰度圖像),RGB(真彩色)和CMYK(pre-press圖像) img_mode = img.mode #圖片模式的轉(zhuǎn)換 img = img.convert("L") #轉(zhuǎn)化成灰度圖像 #獲取每個(gè)坐標(biāo)的像素點(diǎn)的RGB值 r,g,b = img.getpixel((j,i)) #重設(shè)圖片大小 img = img.resize(width,height) #創(chuàng)建縮略圖 img.thumbnail(size)
2、實(shí)戰(zhàn)演練
其實(shí)應(yīng)該很容易想到,如果要達(dá)到這種效果,應(yīng)該能想得到就是獲取圖上每一點(diǎn)的RGB值,然后根據(jù)這三種值確定這一點(diǎn)采用什么字符,其實(shí)根據(jù)RGB來(lái)確定的交灰值,所以可以將圖片轉(zhuǎn)化成灰度圖片,來(lái)直接獲取每一點(diǎn)的灰度,或者通過(guò)灰度的轉(zhuǎn)換公式來(lái)使得RGB三值轉(zhuǎn)化成灰度
#coding:utf-8 from PIL import Image #要索引的字符列表 ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ") length = len(ascii_char) img = Image.open('03.jpg') #讀取圖像文件 (width,height) = img.size img = img.resize((int(width*0.9),int(height*0.5))) #對(duì)圖像進(jìn)行一定縮小 print(img.size) def convert(img): img = img.convert("L") # 轉(zhuǎn)為灰度圖像 txt = "" for i in range(img.size[1]): for j in range(img.size[0]): gray = img.getpixel((j, i)) # 獲取每個(gè)坐標(biāo)像素點(diǎn)的灰度 unit = 256.0 / length txt += ascii_char[int(gray / unit)] #獲取對(duì)應(yīng)坐標(biāo)的字符值 txt += '\n' return txt def convert1(img): txt = "" for i in range(img.size[1]): for j in range(img.size[0]): r,g,b = img.getpixel((j, i)) #獲取每個(gè)坐標(biāo)像素點(diǎn)的rgb值 gray = int(r * 0.299 + g * 0.587 + b * 0.114) #通過(guò)灰度轉(zhuǎn)換公式獲取灰度 unit = (256.0+1)/length txt += ascii_char[int(gray / unit)] # 獲取對(duì)應(yīng)坐標(biāo)的字符值 txt += '\n' return txt txt = convert(img) f = open("03_convert.txt","w") f.write(txt) #存儲(chǔ)到文件中 f.close()
給圖片加上文字(福利預(yù)警,前方有福利!?。。。?/strong>
#coding:utf-8 from PIL import Image,ImageDraw,ImageFont #http://font.chinaz.com/zhongwenziti.html 字體下載網(wǎng)站 img = Image.open('PDD01.jpg') draw = ImageDraw.Draw(img) myfont = ImageFont.truetype('HYLiuZiHeiJ.ttf',size=80) fillcolor = 'pink' (width, height) = img.size #第一個(gè)參數(shù)是加入字體的坐標(biāo) #第二個(gè)參數(shù)是文字內(nèi)容 #第三個(gè)參數(shù)是字體格式 #第四個(gè)參數(shù)是字體顏色 draw.text((40,100),u'萌萌噠',font=myfont,fill=fillcolor) img.save('modfiy_pdd01.jpg','jpeg')
給圖片加上數(shù)字
這個(gè)大家應(yīng)該見(jiàn)過(guò)的,就是有些頭像的左上角的那個(gè)小紅圈加上白色的數(shù)字,其實(shí)方法和上面那個(gè)加文字的差不多
講道理,我還不如用ps,移坐標(biāo)移到要死要死的
#coding:utf-8 from PIL import Image,ImageDraw,ImageFont img = Image.open("03.jpg") draw = ImageDraw.Draw(img) myfont = ImageFont.truetype(u"時(shí)光體.ttf",50) (width,height) = img.size draw.ellipse((width-40,0,width,40),fill="red",outline="red") #在圖上畫(huà)一個(gè)圓 draw.text((width-30,-8),'1',font=myfont,fill='white') img.save('03_modify.jpg')
生成4位隨機(jī)驗(yàn)證碼
#coding:utf-8 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random """ 創(chuàng)建四位數(shù)的驗(yàn)證碼 """ #產(chǎn)生隨機(jī)驗(yàn)證碼內(nèi)容 def rndTxt(): txt = [] txt.append(random.randint(97,123)) #大寫(xiě)字母 txt.append(random.randint(65,90)) #小寫(xiě)字母 txt.append(random.randint(48,57)) #數(shù)字 return chr(txt[random.randint(0,2)]) #隨機(jī)顏色(背景) def rndColor1(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) #隨機(jī)顏色(字體) def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) #240x60: width = 60*4 height = 60 img = Image.new('RGB',(width,height),(255,255,255)) font = ImageFont.truetype(u'時(shí)光體.ttf',36) draw = ImageDraw.Draw(img) #填充每個(gè)像素 for x in range(width): for y in range(height): draw.point((x,y),fill=rndColor1()) #輸出文字 for txt in range(4): draw.text((60*txt+10,10),rndTxt(),font=font,fill=rndColor2()) #模糊化處理 #img = img.filter(ImageFilter.BLUR) img.save("code.jpg")
上述內(nèi)容就是Pillow模塊怎么在python中使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:Pillow模塊怎么在python中使用-創(chuàng)新互聯(lián)
本文路徑:http://jinyejixie.com/article8/djcjip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、全網(wǎng)營(yíng)銷推廣、網(wǎng)頁(yè)設(shè)計(jì)公司、外貿(mào)建站、網(wǎng)站內(nèi)鏈、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容