pil 图像最大值_python PIL 图像处理

Image读出来的是PIL的类型,而skimage.io读出来的数据是numpy格式的#Image和skimage读图片import Image as imgimport osfrom matplotlib import pyplot as plotfrom skimage import io,transformimg_file1 = img.open('./CXR_png/MCUCXR_0042...
摘要由CSDN通过智能技术生成

Image读出来的是PIL的类型,而skimage.io读出来的数据是numpy格式的

#Image和skimage读图片

import Image as img

import os

from matplotlib import pyplot as plot

from skimage import io,transform

img_file1 = img.open('./CXR_png/MCUCXR_0042_0.png')

img_file2 = io.imread('./CXR_png/MCUCXR_0042_0.png')

输出可以看出Img读图片的大小是图片的(width, height);而skimage的是(height,width, channel), [这也是为什么caffe在单独测试时要要在代码中设置:transformer.set_transpose('data',(2,0,1)),因为caffe可以处理的图片的数据格式是(channel,height,width),所以要转换数据]

#读图片后数据的大小:

print "the picture's size: ", img_file1.size

print "the picture's shape: ", img_file2.shape

the picture's size: (4892, 4020)

the picture's shape: (4020, 4892)

#得到像素:

print(img_file1.getpixel((500,1000)), img_file2[500][1000])

print(img_file1.getpixel((500,1000)), img_file2[1000][500])

print(img_file1.getpixel((1000,500)), img_file2[500][1000])

(0, 139)

(0, 0)

(139, 139)

Img读出来的图片获得某点像素用getpixel((w,h))可以直接返回这个点三个通道的像素值

skimage读出来的图片可以直接img_file2[0][0]获得,但是一定记住它的格式,并不是你想的(channel,height,width)

在图片上面加文字

#新建绘图对象

draw = ImageDraw.Draw(image),

#获取图像的宽和高

width, height = image.size;

#** ImageFont模块**

#选择文字字体和大小

setFont = ImageFont.truetype('C:/windows/fonts/Dengl.ttf', 20),

#设置文字颜色

fillColor = "#ff0000"

#写入文字

draw.text((40, height - 100), u'广告', font=setFont, fill=fillColor)

作者:刑素素

链接:http://www.jianshu.com/p/c77315a5435f

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

图片信息

如果我们想知道一些skimage图片信息

from skimage import io, data

img = data.chelsea()

io.imshow(img)

print(type(img)) #显示类型

print(img.shape) #显示尺寸

print(img.shape[0]) #图片高度

print(img.shape[1]) #图片宽度

print(img.shape[2]) #图片通道数

print(img.size) #显示总像素个数

print(img.max()) #最大像素值

print(img.min()) #最小像素值

print(img.mean()) #像素平均值

print(img[0][0])#图像的像素值

PIL image 查看图片信息,可用如下的方法

print type(img)

print img.size #图片的尺寸

print img.mode #图片的模式

print img.format #图片的格式

print(img.getpixel((0,0)))#得到像素:

#img读出来的图片获得某点像素用getpixel((w,h))可以直接返回这个点三个通道的像素值

# 获取图像的灰度值范围

width = img.size[0]

height = img.size[1]

# 输出图片的像素值

count = 0

for i in range(0, width):

for j in range(0, height):

if img.getpixel((i, j))>=0 and img.getpixel((i, j))<=255:

count +=1

print count

print(height*width)

使用python进行数字图片处理,还得安装Pillow包。虽然python里面自带一个PIL(python images library), 但这个库现在已经停止更新了,所以使用Pillow, 它是由PIL发展而来的。

pil能处理的图片类型

pil可以处理光栅图片(像素数据组成的的块)。

通道

一个图片可以包含一到多个数据通道,如果这些通道具有相同的维数和深度,Pil允许将这些通道进行叠加

模式

1 1位像素,黑和白,存成8位的像素

L 8位像素,黑白

P 8位像素,使用调色板映射到任何其他模式

RGB 3×8位像素,真彩

RGBA 4×8位像素,真彩+透明通道

CMYK 4×8位像素,颜色隔离

YCbCr 3×8位像素,彩色视频格式

I 32位整型像素

F 32位浮点型像素

坐标

Pil采取左上角为(0,0)的坐标系统

图片的打开与显示

from PIL import Image

img=Image.open('d:/dog.png')

img.show()

虽然使用的是Pillow,但它是由PIL fork而来,因此还是要从PIL中进行import. 使用open()函数来打开图片,使用show()函数来显示图片。

这种图片显示方式是调用操作系统自带的图片浏览器来打开图片,有些时候这种方式不太方便,因此我们也可以使用另上一种方式,让程序来绘制图片。

from PIL import Image

import matplotlib.pyplot as plt

img=Image.open('d:/dog.png')

plt.figure("dog")

plt.figure(num=1, figsize=(8,5),)

plt.title('The image title')

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值