Python学习笔记(十六)——操作图像
程序员文章站
2023-12-24 10:18:33
...
计算机图像基础
颜色和RGBA值
>>> from PIL import ImageColor
>>> ImageColor.getcolor
<function getcolor at 0x0000022E2FDE5598>
>>> ImageColor.getcolor('red','RGBA')
(255, 0, 0, 255)
>>> ImageColor.getcolor('red','RGB')
(255, 0, 0)
坐标和Box元组
用Pillow操作图像
>>> from PIL import Image
>>> catIm = Image.open('zophie.png')
处理Image数据类型
>>> from PIL import Image
>>> catIm = Image.open('zophie.png')
>>> catIm
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=816x1088 at 0x22E2FDDDF28>
>>> catIm.size
(816, 1088)
>>> catIm.width
816
>>> catIm.height
1088
>>> catIm.filename
'zophie.png'
>>> catIm.format
'PNG'
>>> catIm.format_description
'Portable network graphics'
>>> catIm.save('zophie.jpg')
from PIL import Image
>>> im = Image.new('RGBA',(100,200),'purple')
>>> im.save('purpleImage.png')
>>> im2 = Image.new('RGB',(20,20))
>>> im2.save('transparentImage.png')
裁剪图片
>>> from PIL import Image
>>> catIm = Image
>>> catIm = Image.open('zophie.png')
>>> cropedIm = catIm
>>> cropedIm = catIm.crop((335,345,365,560))
>>> cropedIm.save('cropped.png')
复制和粘贴图像到其他图像
>>> catIm = Image.open('zophie.png')
>>> catCopyIm = catIm.copy()
>>> faceIm = catIm.crop((335,345,565,560))
>>> faceIm.size
(230, 215)
>>> catCopyIm.paste(faceIm,(0,0))
>>> catCopyIm.paste(faceIm,(400,500))
>>> catCopyIm.save('pasted.png')
>>> catImWidth,catImHeight = catIm.size
>>> faceImWidth,faceImHeight = faceIm.size
>>> catCopyTwo = catIm.copy()
>>> for left in range(0,catImWidth)
SyntaxError: invalid syntax
>>> for left in range(0,catImWidth,faceImWidth):
for top in range(0,catImHeight,faceImHeight):
print(left,top)
catCopyTwo.paste(faceIm,(left,top))
0 0
0 215
0 430
0 645
0 860
0 1075
230 0
230 215
230 430
230 645
230 860
230 1075
460 0
460 215
460 430
460 645
460 860
460 1075
690 0
690 215
690 430
690 645
690 860
690 1075
>>> catCopyTwo.save('tiled.png')
调整图像大小
>>> width,height = catIm.size
>>> quartersizedIm = catIm.resize((int(width/2),int(height/2)),Image.ANTIALIAS)
>>> quartersizedIm.save('quartersized.png')
>>> svelteIm = catIm.resize((width,height+300),Image.ANTIALIAS)
>>> svelteIm.save('svelte.png')
旋转和翻转图像
>>> catIm.rotate(90).save('rotated90.png')
>>> catIm.rotate(180).save('rotated180.png')
>>> catIm.rotate(270).save('rotated270.png')
>>> catIm.rotate(6).save('rotated6.png')
>>> catIm.rotate(6,expand=True).save('rotated6_expand.png')
>>>catIm.transpose(Image.FLIP_LEFT_RIGHT).save('horizontal_flip.png')
#左右镜像翻转
>>> catIm.transpose(Image.FLIP_TOP_BOTTOM).save('vertical_flip.png')
#上下镜像翻转
更改单个像素
>>> im = Image.new('RGBA',(100,100))
>>> im.getpixel((0,0))
(0, 0, 0, 0)
>>> for x in range(100):
for y in range(50):
im.putpixel((x,y),(210,210,210))
>>> from PIL import ImageColor
>>> for x in range(100):
for y in range(50,100):
im.putpixel((x,y),ImageColor.getcolor('darkgray','RGBA'))
>>> im.getpixel((0,0))
(210, 210, 210, 255)
>>> im.save('putPixel.png')
小实验——添加徽标
#! python3
#-*- encoding: utf-8 -*-# resizeAndAddLogo.py -
# Usage:
#
# Author : qmeng
# MailTo : aaa@qq.com
# QQ : 1163306125
# Blog : http://blog.csdn.net/Mq_Go/
# Create : 2018-02-19 15:40:07
# Version: 1.0
#
import os
from PIL import Image
SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo.png'
#打开徽标图案
logoIm = Image.open(LOGO_FILENAME)
logoWidth,logoHeight = logoIm.size
#遍历所有文件打开图像
for filename in os.listdir('.'):
if not (filename.endswith('.png') or filename.endswith('.jpg')) or filename == LOGO_FILENAME:
continue
im = Image.open(filename)
width,height = im.size
if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
if width > height:
height = int((SQUARE_FIT_SIZE/width)*height)
width = SQUARE_FIT_SIZE
else:
width = int((SQUARE_FIT_SIZE/height)*width)
height = SQUARE_FIT_SIZE
print('Resizing %s...'%(filename))
im.resize((width,height),Image.ANTIALIAS)
print('Adding logo to %s...'%(filename))
im.paste(logoIm,(width - logoWidth,height - logoHeight),logoIm)
im.save(os.path.join('withLogo',filename))
在图像上绘画
>>> from PIL import Image,ImageDraw
>>> im = Image.new('RGBA',(200,200),'white')
>>> draw = ImageDraw.Draw(im)
>>>
绘制形状
点
线
矩形
多边形
>>> draw.line([(0,0),(199,0),(199,199),(0,199),(0,0)],fill='black')
>>> draw.rectangle((20,30,60,60),fill='blue')
>>> draw.ellipse((120,30,160,60),fill='red')
>>> draw.polygon(((58,87),(79,62),(94,85),(120,90),(103,113)),fill='brown')
>>> for i in range(100,200,10):
draw.line([(i,0),(200,i-100)],fill='green')
>>> im.save('drawing.png')
绘制文本
>>> im = Image.new('RGBA',(200,200),'purple')
>>> draw = ImageDraw.Draw(im)
>>> draw.text((20,150),'hello',fill='red')
>>> fontsFolder = ImageFont.truetype('arial.ttf',32)
>>> draw.text((100,150),'Howdy',fill='gray',font=fontsFolder)
>>> im.save('text.png')