最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python圖像處理模塊Pillow的學(xué)習(xí)詳解

 更新時(shí)間:2019年10月09日 11:11:36   作者:15zhazhahe  
這篇文章主要介紹了python圖像處理模塊Pillow的學(xué)習(xí)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

今天抽空學(xué)習(xí)了一下之前了解過(guò)的pillow庫(kù),以前看到的記得這個(gè)庫(kù)可以給圖片上加文字加數(shù)字,還可以將圖片轉(zhuǎn)化成字符畫,不過(guò)一直沒(méi)有找時(shí)間去學(xué)習(xí)一下這個(gè)模塊,由于放假不用訓(xùn)練,所以就瞎搞了一下

0、工欲善其事,必先利其器

關(guān)于pillow庫(kù)的安裝有幾種方式

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") #在圖上畫一個(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))  #大寫字母
 txt.append(random.randint(65,90))  #小寫字母
 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")

學(xué)習(xí)于:廖雪峰的官方網(wǎng)站

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pandas 如何處理DataFrame中的inf值

    Pandas 如何處理DataFrame中的inf值

    這篇文章主要介紹了Pandas 如何處理DataFrame中的inf值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 最新評(píng)論

    竹山县| 南投县| 黎川县| 浦城县| 铜山县| 玉屏| 康平县| 黄龙县| 富蕴县| 财经| 甘肃省| 乳源| 鸡东县| 尚义县| 亚东县| 兴文县| 兴山县| 休宁县| 锡林浩特市| 博客| 曲周县| 家居| 依兰县| 永寿县| 蒙阴县| 枞阳县| 桂阳县| 旅游| 巴林右旗| 灵山县| 上饶市| 泗阳县| 榆林市| 桃园市| 库伦旗| 清水河县| 公主岭市| 江北区| 外汇| 西充县| 阿拉善盟|