生活随笔
收集整理的這篇文章主要介紹了
python处理图像文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
python處理圖像文件
pip
install pillow
from PIL
import ImageColor
1.pillow模塊的RGBA
在pillow模塊中,RGBA分別代表Red紅色、Green綠色、Blue藍(lán)色和Alpha透明度。這4個(gè)數(shù)值組成元組,范圍都在0~255之間,Alpha的值越小,透明度越高。
1.1 getrgb()
getrgb()函數(shù)可以將顏色符號或字符串轉(zhuǎn)成元組:
(r, g, b) = getrgb(color)
1.2 getcolor()
(r, g, b) = getcolor(color, “mode”) # 返回色彩元組
mode為”RGBA" --> 返回RGBA元組
mode為”RGB" --> 返回RGB元組
mode為其他 --> 返回整數(shù)值
from PIL
import ImageColor
"""
1.getrgb()
getrgb()函數(shù)可以將顏色符號或字符串轉(zhuǎn)成元組:
(r, g, b) = getrgb(color)
"""
print(ImageColor
.getrgb
('#0000ff'))
print(ImageColor
.getrgb
("rgb(0, 255, 255)"))
print(ImageColor
.getrgb
('blue'))
print(ImageColor
.getrgb
('rgb(0%, 50%, 100%)'))
"""
mode為”RGBA" --> 返回RGBA元組
mode為”RGB" --> 返回RGB元組
mode為其他 --> 返回整數(shù)值
"""
print(ImageColor
.getcolor
('#0000ff', 'RGB'))
print(ImageColor
.getcolor
("rgb(0, 255, 255)", 'RGBA'))
print(ImageColor
.getcolor
('blue', 'RGB'))
print(ImageColor
.getcolor
('rgb(0%, 50%, 100%)', 'RGBA'))
2. pillow模塊的盒子元組
最左上角的像素的(x,y)是(0,0),x軸像素往右遞增,y軸像素往下遞增。
盒子元組的參數(shù)是(left,top,right,bottom):
left:盒子左上角的x軸坐標(biāo);
top:盒子左上角的y軸坐標(biāo);
right:盒子右下角的x軸坐標(biāo);
bottom:盒子右下角的y軸坐標(biāo)
3. 圖像的基本操作
- open():打開圖片
- size屬性:獲取圖片寬高
- filename屬性:獲取圖片源文件名稱
- format屬性:獲取圖像文件格式(后綴)
- format_description屬性:獲取詳細(xì)的文件格式描述
- save():存儲文件,可將文件另存為其他圖片格式
- new(mode, size, color=0):創(chuàng)建新的圖像對象
from PIL
import Image
image
= Image
.open('gailun.jpeg')
print(type(image
))
width
, height
= image
.size
print(f'width: {width}, height: {height}')
print(f'filename: {image.filename}')
print(f'擴(kuò)展名: {image.format}')
print(f'文件格式描述:{image.format_description}')
image
.save
('gailun.png')
"""
使用new()方法創(chuàng)建新的圖像對象,語法格式:
new((mode, size, color=0)
mode:RGBA(建立png文件),RGB(創(chuàng)建jpg文件)
size的參數(shù)為元組
color預(yù)設(shè)為黑色
"""
picture
= Image
.new
(mode
="RGB", size
=(100, 200), color
='SteelBlue')
picture
.save
(fp
='newPicture.jpg')
newPicture.jpg:
4. 圖像的編輯
4.1 更改圖像大小–resize()
resize((width, height), Image.BILINEAR)
Image.NEAREST:最低質(zhì)量
Image.ANTIALIAS:最高質(zhì)量
Image.BISCUBIC:三次方取樣法
"""
resize((width, height), Image.BILINEAR)
Image.NEAREST:最低質(zhì)量
Image.ANTIALIAS:最高質(zhì)量
Image.BISCUBIC:三次方取樣法
"""
from PIL
import Image
picture
= Image
.open('newPicture.jpg')
width
, height
= picture
.size
newPict1
= picture
.resize
((width
* 2, height
))
newPict1
.save
('2width.jpg')
newPict2
= picture
.resize
((width
, height
* 2))
newPict2
.save
('2height.jpg')
4.2 圖像旋轉(zhuǎn)–rotate()
"""
rotate()方法可以逆時(shí)針旋轉(zhuǎn)圖像
如果旋轉(zhuǎn)90度或270度:寬高變化,圖像本身比率不變,多的部分以黑色圖像替代。
旋轉(zhuǎn)其他角度則圖像維持不變。
"""
from PIL
import Imagepicture
= Image
.open('gailun.jpeg')
picture
.rotate
(90).save
('r90.jpg')
picture
.rotate
(180).save
('r180.jpg')
picture
.rotate
(270).save
('r270.jpg')
picture
.rotate
(270, expand
=True).save
('r270_ex.jpg')
旋轉(zhuǎn)90度
旋轉(zhuǎn)180度
旋轉(zhuǎn)270度
旋轉(zhuǎn)270加放大
4.3 圖像翻轉(zhuǎn)–transpose()
使用**transpose()**方法可以讓圖像翻轉(zhuǎn):
- transpose(Image.Transpose.FLIP_LEFT_RIGHT):左右翻轉(zhuǎn)
- transpose(Image.Transpose.FLIP_TOP_BOTTOM):上下翻轉(zhuǎn)
"""
使用transpose()方法可以讓圖像翻轉(zhuǎn)
transpose(Image.Transpose.FLIP_LEFT_RIGHT):左右翻轉(zhuǎn)
transpose(Image.Transpose.FLIP_TOP_BOTTOM):上下翻轉(zhuǎn)
"""from PIL
import Imagepicture
= Image
.open('gailun.jpeg')
picture
.transpose
(Image
.Transpose
.FLIP_LEFT_RIGHT
).save
('FLIP_LEFT_RIGHT.jpg')
picture
.transpose
(Image
.Transpose
.FLIP_TOP_BOTTOM
).save
('FLIP_TOP_BOTTOM.jpg')
左右翻轉(zhuǎn):
上下翻轉(zhuǎn):
4.4 圖像像素編輯
- getpixel()方法可以取得圖像某一位置像素的色彩
"""
getpixel()方法可以取得圖像某一位置像素的色彩
getpixel((x,y))
"""from PIL
import ImagenewImage
= Image
.new
('RGBA', (200, 200), 'green')
print(newImage
.getpixel
((100, 100)))
newImage
.save
('newImage1.png')
- putpixel()方法可以在圖像的某個(gè)位置填入色彩
"""
putpixel()方法可以在圖像的某個(gè)位置填入色彩
putpixel((x,y),(r,g,b,a))
"""
from PIL
import Image
from PIL
import ImageColornewImage
= Image
.new
('RGBA', (200, 200), 'green')
for x
in range(50, 151): for y
in range(50, 100): newImage
.putpixel
((x
, y
), (0, 255, 255, 255))
newImage
.save
('putpixel_test1.png')
for x
in range(50, 150): for y
in range(90, 120): newImage
.putpixel
((x
, y
), ImageColor
.getcolor
('Yellow', 'RGBA'))
newImage
.save
('putpixel_test2.png')
4.5 裁切、復(fù)制與圖像合成
"""
crop()方法裁剪圖像
參數(shù)為元組,內(nèi)容為區(qū)間坐標(biāo)(左,上,右,下)
"""
from PIL
import Imagepicture
= Image
.open('yase.jpeg')
crop_pic
= picture
.crop
((30, 20, 450, 360))
crop_pic
.save
('crop_pic.jpg')
-
復(fù)制圖像copy()
"""
copy()方法
"""
from PIL import Imagepicture = Image.open('yase.jpeg')
copy_pic = picture.copy()
copy_pic.save('copy_pic.jpg')
-
圖像合成paste()
paste()方法實(shí)現(xiàn)圖像合成:
底圖圖像.paste(插入圖像,(x,y))
(x,y)為圖像插入的位置
"""paste()方法實(shí)現(xiàn)圖像合成底圖圖像.paste(插入圖像,(x,y))(x,y)為圖像插入的位置"""from PIL
import Imagepic1
= Image
.open('gailun.jpeg')pic2
= Image
.open('yase.jpeg')crop_pic
= pic2
.crop
((120, 20, 436, 370)) pic1
.paste
(crop_pic
, (1, 40))pic1
.save
('圖像合成.jpg')
-
將裁剪圖片填滿圖像區(qū)間
from PIL import Imagepic = Image.open('img.jpeg')
crop_pic = pic.crop((20, 20, 80, 80))
crop_width, crop_height = crop_pic.size width, height = 440, 440
newImage = Image.new('RGB', (width, height), 'green')
for x in range(10, width-20, crop_width):for y in range(10, height-20, crop_height):newImage.paste(crop_pic, (x, y)) newImage.save('圖像鋪滿.jpg')
4.6 圖像添加濾鏡
pillow模塊內(nèi)的ImageFilter模塊中的filter()可以給圖片加上濾鏡效果。此方法的參數(shù)意義:
- BLUR:模糊;
- CONTOUR:輪廓;
- DETAIL:細(xì)節(jié)增強(qiáng)
- EDGE_ENHANCE:邊緣增強(qiáng);
- EDGE_ENHANCE_MORE:深度邊緣增強(qiáng);
- EMBOSS:浮雕效果;
- FIND_EDGES:邊緣信息;
- SMOOTH:平滑效果;
- SMOOTH_MORE:深度平滑效果;
- SHARPEN:銳化效果。
"""
pillow模塊內(nèi)的ImageFilter模塊中的filter()可以給圖片加上濾鏡效果。此方法的參數(shù)意義:
BLUR:模糊;
CONTOUR:輪廓;
DETAIL:細(xì)節(jié)增強(qiáng)
EDGE_ENHANCE:邊緣增強(qiáng);
EDGE_ENHANCE_MORE:深度邊緣增強(qiáng);
EMBOSS:浮雕效果;
FIND_EDGES:邊緣信息;
SMOOTH:平滑效果;
SMOOTH_MORE:深度平滑效果;
SHARPEN:銳化效果。
"""
import PIL
from PIL
import Image
from PIL
import ImageFilterimage
= Image
.open('img.jpeg')
image
= image
.resize
((80, 80), resample
=PIL
.Image
.Resampling
.BILINEAR
)
filter_img
= image
.filter(ImageFilter
.BLUR
)
filter_img
.save
('img_BLUR.jpg')
filter_img
= image
.filter(ImageFilter
.CONTOUR
)
filter_img
.save
('img_CONTOUR.jpg')
filter_img
= image
.filter(ImageFilter
.EDGE_ENHANCE
)
filter_img
.save
('img_EDGE_ENHANCE.jpg')
filter_img
= image
.filter(ImageFilter
.EDGE_ENHANCE_MORE
)
filter_img
.save
('img_EDGE_ENHANCE_MORE.jpg')
filter_img
= image
.filter(ImageFilter
.EMBOSS
)
filter_img
.save
('img_EMBOSS.jpg')
filter_img
= image
.filter(ImageFilter
.FIND_EDGES
)
filter_img
.save
('img_FIND_EDGES.jpg')
filter_img
= image
.filter(ImageFilter
.SHARPEN
)
filter_img
.save
('img_SHARPEN.jpg')
效果:
4.7 圖像內(nèi)繪制圖案
pillow模塊內(nèi)有一個(gè)ImageDraw模塊,利用此模塊可以繪制:點(diǎn)(Points)、線(Lines)、矩形(Rectangles)、橢圓(Ellipses)、多邊形(Polygons)
- 1.繪制點(diǎn):point([(x1,y1),…(xn,yn)],fill),fill為設(shè)置的顏色
- 2.繪制線條:line([(x1,y1),…(xn,yn)], width, fill) ,width為寬度
- 3.ellipse()方法繪制圓或橢圓:
ellipse((left,top,right,bottom), fill, outline),outline為外框顏色 - 4.rectangle()方法繪制橢圓:rectangle((left,top,right,bottom), fill, outline)
- 5.polygon()方法繪制多邊形:polygon([(x1,y1),…(xn,yn)], fill, outline)
"""
pillow模塊內(nèi)有一個(gè)ImageDraw模塊,利用此模塊可以繪制:
點(diǎn)(Points)、線(Lines)、矩形(Rectangles)、橢圓(Ellipses)、多邊形(Polygons)
"""
from PIL
import Image
, ImageDrawnewImage
= Image
.new
('RGBA', (300, 300), 'green')
drawObj
= ImageDraw
.Draw
(newImage
)
for x
in range(100, 200, 3):for y
in range(100, 200, 3):drawObj
.point
([(x
, y
)], fill
='orange')
drawObj
.line
([(0, 0), (299, 0), (299, 299), (0, 299), (0, 0)], fill
='blue')
for x
in range(150, 300, 10):drawObj
.line
([(x
, 0), (300, x
- 150)], fill
='yellow')for y
in range(150, 300, 10):drawObj
.line
([(0, y
), (y
- 150, 300)], fill
='yellow')
drawObj
.rectangle
((100, 200, 210, 240), outline
='silver')
drawObj
.ellipse
((20, 10, 280, 290), outline
='black')
drawObj
.ellipse
((50, 50, 120, 120), outline
='brown')
drawObj
.ellipse
((180, 180, 200, 200), outline
='orange')
drawObj
.polygon
([(150, 120), (100, 100), (120, 100), (150, 120)], fill
='Aqua')newImage
.save
('圖案.png')
4.8 在圖像內(nèi)添加文字
text((x,y),text,fill,font)
使用其他字體,需調(diào)用ImageFont.truetype()方法選用字體,同時(shí)設(shè)定字號。
使用ImageFont.truetype()前需導(dǎo)入ImageFont模塊:
ImageFont.truetype(字體路徑,字號)
"""
text((x,y),text,fill,font)
使用其他字體,需調(diào)用ImageFont.truetype()方法選用字體,同時(shí)設(shè)定字號。
使用ImageFont.truetype()前需導(dǎo)入ImageFont模塊
text(字體路徑,字號)
"""
from PIL
import Image
, ImageDraw
, ImageFontnewImage
= Image
.new
('RGBA', (500, 300), 'green')
drawObj
= ImageDraw
.Draw
(newImage
)strText
= 'Hello world'
drawObj
.text
((50, 50), strText
, fill
='blue')
fontInfo
= ImageFont
.truetype
(r'C:\Windows\Fonts\IMPRISHA.TTF', 24)
drawObj
.text
((50, 100), strText
, fill
='red', font
=fontInfo
)strText_2
= '天朗氣清,惠風(fēng)和暢'
fontInfo
= ImageFont
.truetype
(r'C:\Windows\Fonts\Muyao.TTF', 28)
drawObj
.text
((50, 180), strText_2
, fill
='orange', font
=fontInfo
)strText_3
= '天朗氣清,惠風(fēng)和暢'
fontInfo
= ImageFont
.truetype
(r'C:\Windows\STXINGKA.TTF', 28)
drawObj
.text
((50, 240), strText_2
, fill
='Brown', font
=fontInfo
)
strText_4
= '玄池嫣韻\n坦蕩\n勤能補(bǔ)拙'
x
= 360
y
= 20
right
= 0
down
= 0
w
= 300
h
= 300
font
= ImageFont
.truetype
("C:\\Windows\\Fonts\\STXINGKA.TTF", 42)
for k
, s2
in enumerate(strText_4
):if k
== 0:box
= font
.getbbox
(s2
)w
, h
= box
[2] - box
[0], box
[3] - box
[1] if s2
== "," or s2
== "\n": right
= right
+ wdown
= 0continueelse:down
= down
+ h
print("序號-值", k
, s2
)print("寬-高", w
, h
)print("位移", right
, down
)print("坐標(biāo)", x
+ right
, y
+ down
)drawObj
.text
((x
+ right
, y
+ down
), s2
, (255, 255, 0), font
=font
)
newImage
.show
()
newImage
.save
('寫字.png')
參考:
- https://www.likecs.com/show-203574770.html
- https://blog.csdn.net/qq_36874480/article/details/123569375
總結(jié)
以上是生活随笔為你收集整理的python处理图像文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。