您的位置:首页 > 其它

使用SciPy进行常用的图像操作

2017-08-19 16:47 405 查看

SciPy provides some basic functions to work with images. For example, it has functions to read images from disk into numpy arrays, to write numpy arrays to disk as images, and to resize images. Here is a simple example that showcases these functions:

# coding:utf-8
import numpy as np
from scipy.misc import imread, imsave, imresize

# Read an JPEG image into a numpy array
img = imread('cat.jpg')
print(img.dtype, img.shape)  # Prints "uint8 (400, 248, 3)"
print(type(np.array(img)))   # Prints "<class 'numpy.ndarray'>"

# We can tint the image by scaling each of the color channels
# by a different scalar constant. The image has shape (400, 248, 3);
# we multiply it by the array [1, 0.95, 0.9] of shape (3,);
# numpy broadcasting means that this leaves the red channel unchanged,
# and multiplies the green and blue channels by 0.95 and 0.9
# respectively.
img_tinted = img * [1, 0.95, 0.9]

# Resize the tinted image to be 300 by 300 pixels.
img_tinted = imresize(img_tinted, (300, 300))

# Write the tinted image back to disk
imsave('cat_tinted.jpg', img_tinted)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐