https://blog.csdn.net/Richie_ll/article/details/69206210html
今天抽空學習了一下以前瞭解過的pillow庫,之前看到的記得這個庫能夠給圖片上加文字加數字,還能夠將圖片轉化成字符畫,不過一直沒有找時間去學習一下這個模塊,因爲放假不用訓練,因此就瞎搞了一下python
關於pillow庫的安裝有幾種方式
0、使用pip安裝ruby
$ pip install pillow
一、使用easy_installapp
$ easy_install pillow
二、經過pycharm安裝dom
#導入模塊 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 #圖片模式的轉換 img = img.convert("L") #轉化成灰度圖像 #獲取每一個座標的像素點的RGB值 r,g,b = img.getpixel((j,i)) #重設圖片大小 img = img.resize(width,height) #建立縮略圖 img.thumbnail(size)
其實應該很容易想到,若是要達到這種效果,應該能想獲得就是獲取圖上每一點的RGB值,而後根據這三種值肯定這一點採用什麼字符,其實根據RGB來肯定的交灰值,因此能夠將圖片轉化成灰度圖片,來直接獲取每一點的灰度,或者經過灰度的轉換公式來使得RGB三值轉化成灰度學習
#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))) #對圖像進行必定縮小 print(img.size) def convert(img): img = img.convert("L") # 轉爲灰度圖像 txt = "" for i in range(img.size[1]): for j in range(img.size[0]): gray = img.getpixel((j, i)) # 獲取每一個座標像素點的灰度 unit = 256.0 / length txt += ascii_char[int(gray / unit)] #獲取對應座標的字符值 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)) #獲取每一個座標像素點的rgb值 gray = int(r * 0.299 + g * 0.587 + b * 0.114) #經過灰度轉換公式獲取灰度 unit = (256.0+1)/length txt += ascii_char[int(gray / unit)] # 獲取對應座標的字符值 txt += '\n' return txt txt = convert(img) f = open("03_convert.txt","w") f.write(txt) #存儲到文件中 f.close()
#coding:utf-8 from PIL import Image,ImageDraw,ImageFont #http://font.chinaz.com/zhongwenziti.html 字體下載網站 img = Image.open('PDD01.jpg') draw = ImageDraw.Draw(img) myfont = ImageFont.truetype('HYLiuZiHeiJ.ttf',size=80) fillcolor = 'pink' (width, height) = img.size #第一個參數是加入字體的座標 #第二個參數是文字內容 #第三個參數是字體格式 #第四個參數是字體顏色 draw.text((40,100),u'萌萌噠',font=myfont,fill=fillcolor) img.save('modfiy_pdd01.jpg','jpeg')
這個你們應該見過的,就是有些頭像的左上角的那個小紅圈加上白色的數字,其實方法和上面那個加文字的差很少
講道理,我還不如用ps,移座標移到要死要死的字體
#coding:utf-8 from PIL import Image,ImageDraw,ImageFont img = Image.open("03.jpg") draw = ImageDraw.Draw(img) myfont = ImageFont.truetype(u"時光體.ttf",50) (width,height) = img.size draw.ellipse((width-40,0,width,40),fill="red",outline="red") #在圖上畫一個圓 draw.text((width-30,-8),'1',font=myfont,fill='white') img.save('03_modify.jpg')
#coding:utf-8 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random """ 建立四位數的驗證碼 """ #產生隨機驗證碼內容 def rndTxt(): txt = [] txt.append(random.randint(97,123)) #大寫字母 txt.append(random.randint(65,90)) #小寫字母 txt.append(random.randint(48,57)) #數字 return chr(txt[random.randint(0,2)]) #隨機顏色(背景) def rndColor1(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) #隨機顏色(字體) 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'時光體.ttf',36) draw = ImageDraw.Draw(img) #填充每一個像素 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")