如何使用python

时间:2017-03-26 08:37:04

标签: python

from PIL import Image
import PIL.ImageOps 
import cv2
import numpy as np 
from matplotlib import pyplot as plt

# read image

img = cv2.imread('bnw11.png')
height, width = img.shape
print "height and width : ",height, width

size = img.size
print "size of the image in number of pixels", size 

# plot the binary image

cv2.imshow('binary',img)

当我运行此代码时,我收到以下错误: -

Traceback (most recent call last):
File "C:/Python27/BnW.py", line 9, in <module>
height, width = img.shape
ValueError: too many values to unpack

我的图像已经是二进制图像了。我想在几个二进制图像中不计算黑白像素......我是新手......可以提供给你的任何帮助..

1 个答案:

答案 0 :(得分:1)

错误是因为img.shape正在返回height, width = img.shape中假设的大小大于或小于2的元组。在图像为numpy数组的情况下,.shape()在RGB图像的情况下返回3个值,因此您可以将其更改为

height, width, channels = img.shape

但是对于GrayScale imgaes height, width = img.shape可以正常工作。