How to load an image with python

@loading images with python

在Python中,常用的读图工具包括opencv、PIL(pillow)、tensorflow、mxnet等,下面我们将举例说明:
假定图片路径为

img_path = './img/0001.jpg'

opencv

import cv2
import numpy as np
# method 1
image1 = cv2.imread(img_path)
# method 2
image2 = cv2.imdecode(np.fromfile(img_path, dtype = np.uint8), -1)

可以用np.equal(image1, image2).all()判定两种方法得到的数据是完全相同的。

PIL.Image

from PIL import Image
import numpy as np
# method 3
image3 = Image.open(img_path)
image3 = np.array(image3)

tensorflow

import tensorflow as tf
# method 4
image4 = tf.gfile.FastGFile(img_path, 'rb').read()
image4 = tf.image.decode_jpeg(image4)

mxnet

import mxnet as mx
# method 5
image5 = mx.image.imdecode(open(img_path, 'rb').read())
image5 = image5[:, :, ::-1].asnumpy()

skimage.io

from skimage import io
# method 6
image6 = io.imread(img_path)
image6 = image6[:, :, ::-1]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值