Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中打开jpg时出错_Python_Python 2.7_Pyqt4_Reportlab - Fatal编程技术网

在Python中打开jpg时出错

在Python中打开jpg时出错,python,python-2.7,pyqt4,reportlab,Python,Python 2.7,Pyqt4,Reportlab,我的程序提示用户选择文件(通过PyQT4主窗口),然后在ReportLab中生成pdf报告 我试图包括的两张图片中有一张出现IOError。这两个图像都位于我正在使用的每个其他文件所在的目录中。我还尝试将另一个图像作为两个图像加载,没有错误。我尝试使用目录外的另一个图像-效果很好。我怀疑问题图像的文件大小可能太大,所以我从450kB的图像改为45KB的图像。还是没有骰子 问题代码: #from the code for the Compiled Window - this is how I ac

我的程序提示用户选择文件(通过PyQT4主窗口),然后在ReportLab中生成pdf报告

我试图包括的两张图片中有一张出现IOError。这两个图像都位于我正在使用的每个其他文件所在的目录中。我还尝试将另一个图像作为两个图像加载,没有错误。我尝试使用目录外的另一个图像-效果很好。我怀疑问题图像的文件大小可能太大,所以我从450kB的图像改为45KB的图像。还是没有骰子

问题代码:

#from the code for the Compiled Window - this is how I acquire the images
...
def logo_pic(self):
    self.Logo_picture = unicode(QtGui.QFileDialog.getOpenFileName())
    self.LogoLabel.setText(self.Logo_picture) #this shows the user the path of the file they selected

def setup_pic(self):
    self.setup_picture = unicode(QtGui.QFileDialog.getOpenFileName())
    self.SetupLabel.setText(self.setup_picture) 
...

#take the file path and turn it into a workable file name
setup_pic_title_index = str(cw.setup_picture).rfind('/') + 1
setup_pic_title = cw.setup_picture[setup_pic_title_index:]
Logo_picture_title_index = str(cw.Logo_picture).rfind('/') + 1
Logo_picture_title = cw.Logo_picture[Logo_picture_title_index:]

#this is where the image itself is pulled and scaled appropriately for the ReportLab document, and added to the list of items to include
elements.append(get_image('./' + Logo_picture_title, width = 9*inch))
elements.append(get_image('./' + setup_pic_title, width = 8*inch))
以下是我得到的错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
File "C:/Users/moi/etc", line 367, in <module>
    elements.append(get_image('./' + setup_pic_title, width = 8*inch))
File "C:/Users/moi/etc", line 233, in get_image
    img = utils.ImageReader(path)
File "C:\Python27\lib\site-packages\reportlab\lib\utils.py", line 585, in __init__
annotateException('\nfileName=%r identity=%s'%(fileName,self.identity()))
File "C:\Python27\lib\site-packages\reportlab\lib\utils.py", line 549, in __init__
self.fp = open_for_read(fileName,'b')
File "C:\Python27\lib\site-packages\reportlab\lib\utils.py", line 452, in open_for_read
raise IOError('Cannot open resource "%s"' % name)

IOError: Cannot open resource "./DSCN2223.JPG"
fileName=u'./DSCN2223.JPG' identity=[ImageReader@0x70b1870 filename=u'./DSCN2223.JPG']
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第540行,在runfile中
execfile(文件名、命名空间)
文件“C:/Users/moi/etc”,第367行,在
元素。追加(获取图片('./'+设置图片标题,宽度=8*inch))
get_图像中第233行的文件“C:/Users/moi/etc”
img=utils.ImageReader(路径)
文件“C:\Python27\lib\site packages\reportlab\lib\utils.py”,第585行,在\uuu init中__
注释异常('\n文件名=%r标识=%s%%(文件名,self.identity()))
文件“C:\Python27\lib\site packages\reportlab\lib\utils.py”,第549行,在\uuu init中__
self.fp=打开以进行读取(文件名为'b')
文件“C:\Python27\lib\site packages\reportlab\lib\utils.py”,第452行,在open\u中进行读取
raise IOError('无法打开资源“%s”%name)
IOError:无法打开资源“/DSCN2223.JPG”
fileName=u'./DSCN2223.JPG'标识=[ImageReader@0x70b1870filename=u'./DSCN2223.JPG']

我包括的标志和设置图片的代码-信息基本上是相同的。此外,它还可以处理目录外的另一个.jpg文件

我发现我遇到的问题可以追溯到没有使用完整路径名。为了解决此问题,我保存了一个名为pathname的变量:

document_title_index = str(first_Fname).rfind('/') + 1 #find the last / and add 1
document_title = first_Fname[document_title_index: len(first_Fname)-4] #uses the index to grab the string that's affiliated with the first data file for naming the report
pathname = first_Fname[0:document_title_index] # grabs the pathname part to place new files correctly
然后,当我保存我的数字时,我使用路径名将它们放在正确的位置-使用报表的源数据,而不是使用.py文件本身:

plt.savefig(pathname + figname + '.jpg', dpi = 100)  #save plots to the same place as the first data file

其中
/DSCN2223.JPG
?与用户选择的其他8个文件位于同一目录中。和logo jpg在同一个目录中,你打开其他任何一个都没有问题吗?没有。我也可以在这两个文件中使用logo,这样可以工作。你能单独打开它吗?也就是说,启动python解释器并运行非常简化的代码?