gpt4 book ai didi

python-3.x - Pyinstaller .exe 找不到 _tiffile 模块 - 加载一些压缩图像会很慢

转载 作者:行者123 更新时间:2023-12-04 15:40:58 26 4
gpt4 key购买 nike

当我从 Pyinstaller 运行我的代码时,tiff 阅读器工作正常。使用 Pyinstaller 卡住后,我收到以下警告:

enter image description here

UserWarning: ImportError: No module named '_tifffile'. Loading of some compressed images will be very slow. Tifffile.c can be obtained at http://www.lfd.uci.edu/~gohlke

果然,以前需要几秒钟才能加载到 numpy 数组中的 tiff 文件现在可能需要几分钟。

这是我的代码的简化形式,用于解决问题。如果您加载示例 tiff,例如 this one它应该可以快速加载而没有问题。

如果您使用 C:\Python35\python.exe C:\Python35\Scripts\pyinstaller.exe --additional-hooks-dir=. --clean --win-private-assemblies tiffile_problems.py当您运行它时,您应该会得到一个带有上述错误消息的功能性 .exe。当您尝试加载相同的 tiff 时,现在需要更长的时间。

tiffile_problems.py
#!/usr/bin/env python3

import os
import sys
import traceback
import numpy as np
import matplotlib.pyplot as plt

from PyQt4.QtGui import *
from PyQt4.QtCore import *

sys.path.append('..')

from MBE_for_SO.util import fileloader, fileconverter

class NotConvertedError(Exception):
pass

class FileAlreadyInProjectError(Exception):
def __init__(self, filename):
self.filename = filename

class Widget(QWidget):
def __init__(self, project, parent=None):
super(Widget, self).__init__(parent)

if not project:
self.setup_ui()
return

def setup_ui(self):
vbox = QVBoxLayout()

## Related to importing Raws
self.setWindowTitle('Import Raw File')

#vbox.addWidget(QLabel('Set the size all data are to be rescaled to'))

grid = QGridLayout()

vbox.addLayout(grid)
vbox.addStretch()

self.setLayout(vbox)
self.resize(400, 220)

self.listview = QListView()
self.listview.setStyleSheet('QListView::item { height: 26px; }')
self.listview.setSelectionMode(QAbstractItemView.NoSelection)
vbox.addWidget(self.listview)

hbox = QVBoxLayout()
pb = QPushButton('New Video')
pb.clicked.connect(self.new_video)
hbox.addWidget(pb)

vbox.addLayout(hbox)
vbox.addStretch()
self.setLayout(vbox)


def convert_tif(self, filename):
path = os.path.splitext(os.path.basename(filename))[0] + '.npy'
#path = os.path.join(self.project.path, path)

progress = QProgressDialog('Converting tif to npy...', 'Abort', 0, 100, self)
progress.setAutoClose(True)
progress.setMinimumDuration(0)
progress.setValue(0)

def callback(value):
progress.setValue(int(value * 100))
QApplication.processEvents()

try:
fileconverter.tif2npy(filename, path, callback)
print('Tifffile saved to wherever this script is')
except:
# qtutil.critical('Converting tiff to npy failed.')
progress.close()
return path

def to_npy(self, filename):
if filename.endswith('.raw'):
print('No raws allowed')
#filename = self.convert_raw(filename)
elif filename.endswith('.tif'):
filename = self.convert_tif(filename)
else:
raise fileloader.UnknownFileFormatError()
return filename

def import_file(self, filename):
if not filename.endswith('.npy'):
new_filename = self.to_npy(filename)
if not new_filename:
raise NotConvertedError()
else:
filename = new_filename

return filename

def import_files(self, filenames):
for filename in filenames:
try:
filename = self.import_file(filename)
except NotConvertedError:
# qtutil.warning('Skipping file \'{}\' since not converted.'.format(filename))
print('Skipping file \'{}\' since not converted.'.format(filename))
except FileAlreadyInProjectError as e:
# qtutil.warning('Skipping file \'{}\' since already in project.'.format(e.filename))
print('Skipping file \'{}\' since already in project.'.format(e.filename))
except:
# qtutil.critical('Import of \'{}\' failed:\n'.format(filename) +\
# traceback.format_exc())
print('Import of \'{}\' failed:\n'.format(filename) + traceback.format_exc())
# else:
# self.listview.model().appendRow(QStandardItem(filename))

def new_video(self):
filenames = QFileDialog.getOpenFileNames(
self, 'Load images', QSettings().value('last_load_data_path'),
'Video files (*.npy *.tif *.raw)')
if not filenames:
return
QSettings().setValue('last_load_data_path', os.path.dirname(filenames[0]))
self.import_files(filenames)

class MyPlugin:
def __init__(self, project):
self.name = 'Import video files'
self.widget = Widget(project)

def run(self):
pass

if __name__ == '__main__':
app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
w = QMainWindow()
w.setCentralWidget(Widget(None))
w.show()
app.exec_()
sys.exit()

文件转换器.py
#!/usr/bin/env python3

import os
import numpy as np

import tifffile as tiff

class ConvertError(Exception):
pass

def tif2npy(filename_from, filename_to, progress_callback):
with tiff.TiffFile(filename_from) as tif:
w, h = tif[0].shape
shape = len(tif), w, h
np.save(filename_to, np.empty(shape, tif[0].dtype))
fp = np.load(filename_to, mmap_mode='r+')
for i, page in enumerate(tif):
progress_callback(i / float(shape[0]-1))
fp[i] = page.asarray()

def raw2npy(filename_from, filename_to, dtype, width, height,
num_channels, channel, progress_callback):
fp = np.memmap(filename_from, dtype, 'r')
frame_size = width * height * num_channels
if len(fp) % frame_size:
raise ConvertError()
num_frames = len(fp) / frame_size
fp = np.memmap(filename_from, dtype, 'r',
shape=(num_frames, width, height, num_channels))
np.save(filename_to, np.empty((num_frames, width, height), dtype))
fp_to = np.load(filename_to, mmap_mode='r+')
for i, frame in enumerate(fp):
progress_callback(i / float(len(fp)-1))
fp_to[i] = frame[:,:,channel-1]

文件加载器.py
#!/usr/bin/env python3

import numpy as np

class UnknownFileFormatError(Exception):
pass

def load_npy(filename):
frames = np.load(filename)
# frames[np.isnan(frames)] = 0
return frames

def load_file(filename):
if filename.endswith('.npy'):
frames = load_npy(filename)
else:
raise UnknownFileFormatError()
return frames

def load_reference_frame_npy(filename, offset):
frames_mmap = np.load(filename, mmap_mode='c')
if frames_mmap is None:
return None
frame = np.array(frames_mmap[offset])
frame[np.isnan(frame)] = 0
frame = frame.swapaxes(0, 1)
if frame.ndim == 2:
frame = frame[:, ::-1]
elif frame.ndim == 3:
frame = frame[:, ::-1, :]
return frame

def load_reference_frame(filename, offset=0):
if filename.endswith('.npy'):
frame = load_reference_frame_npy(filename, offset)
else:
raise UnknownFileFormatError()
return frame

为什么?我该如何解决这个问题?我找到了 tifffile.py, tifffile.cpython-35.pyc, tifffile.c并将它们全部放在与.exe相同的目录中。没有效果。 _tifffile.cp35-win_amd64.pyd由 pyinstaller 创建并放置在与 .exe 相同的目录中。我不知道我还有什么其他选择。

tifffile_problems.spec
# -*- mode: python -*-

block_cipher = None


a = Analysis(['tiffile_problems.py'],
pathex=['C:\\Users\\Cornelis\\PycharmProjects\\tester\\MBE_for_SO'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=True,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='tiffile_problems',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='tiffile_problems')

使用 C:\Python35\python.exe C:\Python35\Scripts\pyinstaller.exe --additional-hooks-dir=. --clean --win-private-assemblies --onefile tiffile_problems.py 时的 tiffile.spec
# -*- mode: python -*-

block_cipher = None


a = Analysis(['tiffile_problems.py'],
pathex=['C:\\Users\\Cornelis\\PycharmProjects\\tester\\MBE_for_SO'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=['.'],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=True,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='tiffile_problems',
debug=False,
strip=False,
upx=True,
console=True )

最佳答案

我认为 muggy 对 __package__ 的古怪之处是正确的。导致这里的问题。我还没有找到修复的确切原因,但这似乎通过对 pyinstaller 的最新更新得到解决。 .检查您的版本:

→ pyinstaller --version
3.2.1

并升级

→ pip3 install --upgrade pyinstaller

此更新仅在 2017 年 1 月 15 日进行,因此当您最初询问时这不会有帮助,但现在确实有帮助。

关于python-3.x - Pyinstaller .exe 找不到 _tiffile 模块 - 加载一些压缩图像会很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40274354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com