str对象没有属性“转换”

时间:2019-10-07 04:57:50

标签: python

@charset "utf-8";
body {
  font: 90%/1.4 Verdana, Arial, Helvetica, sans-serif;
  background: #42413C;
  margin: 0;
  padding: 0;
  color: #000;
}


ul, ol, dl { 
  padding: 0;
  margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
  margin-top: 0;  
  padding-right: 15px;
  padding-left: 15px;    }
a img { 
  border: none;
}

a:link {
  color: #42413C;
  text-decoration: underline; 
}
a:visited {
  color: #000000;
  text-decoration: underline;
}
a:hover, a:active, a:focus { 
  text-decoration: none;
}
.container {
  width: 100%;
  background: #FFF;
  margin: 0 auto;
}

.header {
  background: #954a9a;
}
.sidebar1 {
  float: left;
  width: 200px;
  background: #e2a5e6;
  padding-bottom: 10px;
}
.content {

  padding: 10px 0;
  width: 780px;
  float: left;
}
.content ul, .content ol { 
  padding: 0 15px 15px 40px;
}
ul.nav {
  list-style: none; 
  border-top: 1px solid #666; 
  margin-bottom: 15px; 
}
ul.nav li {
  border-bottom: 1px solid #666;
}
ul.nav a, ul.nav a:visited {
  padding: 5px 5px 5px 15px;
  display: block;
  width: 160px;  
  text-decoration: none;
  background: #C6D580;
}
ul.nav a:hover, ul.nav a:active, ul.nav a:focus { 
  background: #ADB96E;
  color: #FFF;
}

.footer {
  padding: 10px 0;
  background: #954a9a;
  position: relative;
  clear: both; 
}

.fltrt { 
  float: right;
  margin-left: 8px;
}
.fltlft { 
  float: left;
  margin-right: 8px;
}
.clearfloat { 
  clear:both;
  height:0;
  font-size: 1px;
  line-height: 0px;
}
#info {
    font-size: 18px;
    background: #ffffff;
    width: 350px;
    border-radius: 6px;
    text-align: center;
    color: #666666;
    border: solid 1px #666666;
    margin: auto;
    padding: 3px;

}
#enviar {
    background: #fff;
    font-size: 22px;
    text-align: left;
    color: #000;
    margin: 10px 60px;
    padding:3px 10px 3px 10px;
    border-radius: 6px;
}
#respuesta {
    margin: auto 60px;
}
#googleMap {
    margin: 10px auto;
    width:90%;
    height:90%;
    border: 2px solid #666666; 
    border-radius: 6px;
}
#map {
    margin: 10px auto;
    /* width: 90%; */
    height: 80%;
    border: 2px solid #666666;
    border-radius: 6px;
}
.titular {
        background: #ffffff;
        font-size: 32px;
        color: #000;
        margin: 40px auto;
        text-align: center;
        width: 50%;
        padding:3px 10px 3px 10px;
        border-radius: 6px;
        border: solid 1px #999999;
    }

错误:

#Feature extraction on single image
img = data_path[0][0]#PIL image
img_gray = img.convert('L') #Converting to grayscale
img_arr = np.array(img_gray) #Converting to array
plt.imshow(img_gray)

如何解决此属性问题?

1 个答案:

答案 0 :(得分:1)

@LainShelvington注释的img = data_path[0][0]语句将给定路径的字符串值分配给img变量,因此它img.convert('L')给出 AtrributeError

以下代码可以帮助您解决问题:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as Image

img = Image.imread(data_path[0][0])     
img_gray=img.convert('L')    
img_arr = np.array(img_gray) 
plt.imshow(img_gray)