1. 程式人生 > >python圖像處理(2)圖像水印和PIL模式轉化

python圖像處理(2)圖像水印和PIL模式轉化

tex 我們 參考 height rom 設置 分享 show pen

模式轉化:

PIL模式轉化:將圖片轉化成其他模式

 1 # 我們將image圖像轉化為灰度圖像(python)
 2 from PIL import Image
 3 img = Image.open(c:\\1.JPG)
 4 img.show()    # 圖像的打開展示
 5 # L = I.convert(‘L‘)
 6 # PIL的九種不同模式:1,L,P,RGB,RGBA,CMYK,YCbCr,I,F 
 7 # 模式轉化語句 
 8 L = img.convert(I)
 9 w,h = img.size        # 圖尺寸
10 mode = img.mode         #
圖片的模式 11 geshi = img.format # 圖片的格式 12 print(w,h,mode,geshi) 13 L.show()

技術分享圖片

技術分享圖片

技術分享圖片

水印:

from PIL import Image, ImageDraw, ImageFont
# 添加文字水印
# RGBA的意思是(Red-Green-Blue-Alpha)它是在RGB上擴展包括了“alpha”通道,運行對顏色值設置透明度
im = Image.open("C:/1.jpg").convert(RGBA)  
txt=Image.new(RGBA, im.size, (0,0,0,0))    #
圖像的大小 fnt=ImageFont.truetype("c:/Windows/fonts/Candara.ttf", 50) # 選水印的字體 d=ImageDraw.Draw(txt) d.text((txt.size[0]-120,txt.size[1]-50), "Hello",font=fnt,fill=(255,0,0,255)) out=Image.alpha_composite(im,txt) out.show() out.save(r"c:/pp.png")

技術分享圖片

# 添加圖片水印

im = Image.open("C:/2.jpg")
mark=Image.open("C:/1.jpg
") print(im.size) # RGBA表示四個屬性A表示透明度 關於new(‘模式’,大小尺寸,顏色) layer=Image.new(RGBA, im.size, (0,0,0,0)) layer.paste(mark, (im.size[0]-400,im.size[1]-200)) # 改變水印圖片的位置 # composite:使用兩幅給出的圖片和一個與alpha參數相似用法的mask參數其值可為:"1","L","RGBA"。兩幅圖片的size必須相同。 out=Image.composite(layer,im,layer) out.show()

技術分享圖片

#參考 https://blog.csdn.net/icamera0/article/details/50747084

python圖像處理(2)圖像水印和PIL模式轉化