Python中怎么读写图像

Python中,主要使用的图像处理库是PIL(Python Imaging Library)opencvscikit-image
1. 用PIL读写图片1

from PIL import Image
path = 'data/timg.jpg'
img = Image.open(path)#读取图片
print(type(img))#<class 'PIL.PngImagePlugin.PngImageFile'>
print(img.mode,img.size,img.format)#RGB (2048, 1365) JPEG
img1 = np.array(img) ## 转成numpy形式
print(img1.shape)#(803, 599, 3)
## 在图片中放置文字
## 显示中文字符
## simsun.ttc 是宋体,可以显示中文.
## 字体在ubuntu下没有时,可以将字体文件复制到Ubuntu系统中去
font = ImageFont.truetype("simsun.ttc", 36)  # 指定字体和字号
 # 定义需要绘制的文本
text = "你好,世界!"
draw.text((40,40), text, font=font, fill=(0, 0, 255))
img.save('a.jpg')#保存图片
img.show()#显示图像

在这里插入图片描述

  • PIL读出的图片是PIL.PngImagePlugin.PngImageFile格式。img.size得到的尺寸是(W,H)形式的,转换成numpy数据后就是(H,W,C).颜色模式img.modeRGB
  • 如果在ubuntu系统运行,可能没有“simsun.ttc”字体,需要从windows系统复制过去,或者在网上下载。

1.1 PIL图片上画矩形

  • 使用draw.rectangle().
  • 使用说明参考官网 在这里插入图片描述
  • 画圆角矩形draw.rounded_rectangle
    在这里插入图片描述
from PIL import ImageFont,ImageDraw,Image
img_path = 'woman_dog.jpg'
raw_image = Image.open(img_path).convert('RGB')

#下面就是设置字体的样式以及字体的大小,都是自行设置的
myFont = ImageFont.truetype('DENGL.TTF', 66)
#给图形中添加文本信息
draw = ImageDraw.Draw(raw_image)

text = 'Be happy!'
# 获取字符串的宽、高
text_width, text_height = draw.textsize(text, font=myFont)
# 画实心框,使用fill参数。第一个参数是框的(xmin,ymin,xmax,ymax)
draw.rectangle((40,120,40+text_width,120+text_height),fill=(0,255,255))
draw.text((40,106),text,font = myFont,fill=(255, 0, 255),direction=None)
x = 40
y = 150+text_height
## 画外框,使用outline参数
draw.rectangle((x,y,x+text_width,y+text_height),outline=(0,0,255),width = 3)
# #显示图片
raw_image.show()

在这里插入图片描述
画完实心框后,写字上去,给字添加背景色

2. opencv读写图片2

import cv2 as cv

img = cv.imread('data/timg.jpg')  # 灰度模式读取图片
print(type(img))#<class 'numpy.ndarray'>
print(img.shape)#(803, 599, 3)
cv.imshow('image', img)  # 显示图片,窗口名称为'image'
k = cv.waitKey(0)  # 无限等待一个键击,将此键击存在k变量中
if k == 27:  # 27代表esc,可以查看ascii码表
    cv.destroyAllWindows()  # 退出窗口
elif k == ord('s'):  # 等待s键,ord函数可以将字符串转换为ascii码
    cv.imwrite('a.png', img)  # 写入图片
    cv.destroyAllWindows()  # 关闭窗口

opencv读出的图片是numpy.ndarray格式。尺寸是(H,W,C)形式.颜色模式为BGR`。

3. scikit-image读写图片

from skimage.io import imshow,imsave
from matplotlib import pyplot as plt

img = io.imread('data/timg.jpg')#读取图片
print(type(img))#<class 'numpy.ndarray'>
print(img.shape)#(803, 599, 3)
imshow(img)#显示图像要借助matplotlib库
imsave('a3.jpg',img)#保存图像
plt.show()#显示图像

scikit-image读出的图片是numpy.ndarray格式。尺寸是(H,W,C)形式.颜色模式为RGB`。

python中PIL.Image和OpenCV图像格式相互转换

PIL转opencv

import cv2
from PIL import Image
import numpy
 
image = Image.open("plane.jpg")
image.show()
img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)
cv2.imshow("OpenCV",img)
cv2.waitKey()

###opencv转PIL

import cv2
from PIL import Image
import numpy
 
img = cv2.imread("plane.jpg")
cv2.imshow("OpenCV",img)
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
image.show()
cv2.waitKey()

skimage与opencv图片格式的相互转换


  1. https://www.cnblogs.com/meitian/p/3699223.html ↩︎

  2. https://blog.csdn.net/claroja/article/details/83031748 ↩︎

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值