gpt4 book ai didi

python - 使用 Python 效率和速度的图像保存 I/O

转载 作者:行者123 更新时间:2023-11-28 22:42:16 25 4
gpt4 key购买 nike

我是 Python 的新手(并且有一段时间没有使用过文件 IO),所以如果我在我说的任何事情上犯了初学者的错误,请多多包涵。

我有每张约 5 MB 的 .bmp 图片。我想取两个图像的平均值并将平均值保存在另一个文件目录中。该公司笔记本电脑是 8 GB RAM,64 位,处理器是 AMD A10-7300 Radeon R6,10 Compute Cores 4C+6G 1.9 GHz

我就是这样做的,但现在我的实习经理希望我加快保存过程(现在 500 张图像大约需要 2-3 分钟)。我正在使用函数 imageResult.save(currentSavePath,"bmp")。

图片保存代码如下:

# function for file selection 2
def FileSelect2(self, event):
dirDialog = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE);

# user canceled file opening
if dirDialog.ShowModal() == wx.ID_CANCEL:
return

# otherwise, proceed loading the file chosen by the user
self.rootDir2 = dirDialog.GetPath()
self.subdirArray2 = [];
for dirName, subdirList, fileList in os.walk(self.rootDir2):
for fname in fileList:
if os.path.splitext(fname)[1] == '.bmp':
self.subdirArray2.append(dirName+'\\'+fname)

self.fileDisplay2.Clear()
self.statusText.SetForegroundColour(wx.BLACK)
self.blocker = False
self.fileDisplay2.AppendText(self.rootDir2)
# function for making sure the directory matches
def CheckIfFilesMatch(self):
if(self.subdirArray1.__len__() != self.subdirArray2.__len__()):
self.statusText.SetValue("please enter same amount of files")
self.blocker = True
self.statusText.SetForegroundColour(wx.RED)
return False
for f in self.subdirArray1:
if f.replace(self.rootDir1,self.rootDir2) not in self.subdirArray2:
self.statusText.SetValue("This file: " + f + " does not correspond to any file in parallel.")
self.blocker = True
self.statusText.SetForegroundColour(wx.RED)
return False
for f in self.subdirArray2:
if f.replace(self.rootDir2,self.rootDir1) not in self.subdirArray1:
self.statusText.SetValue("This file: " + f + " does not correspond to any file in parallel.")
self.blocker = True
self.statusText.SetForegroundColour(wx.RED)
return False

平均图像函数

def Average(self, event):
self.CheckIfFilesMatch()
if self.blocker:
return
self.count = 0
# save file
saveDialog = wx.DirDialog(self, "Choose a directory(Your files will be saved in same file names under this):", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT);
# cancel
if saveDialog.ShowModal() == wx.ID_CANCEL:
# update status
self.statusText.SetValue("Did not save")
self.statusText.SetForegroundColour(wx.BLACK)
# ok
return

else:
savePath = saveDialog.GetPath()
# start reading file
for i in self.subdirArray1:
postfix = i.replace(self.rootDir1, "")
print postfix
print i
f = self.rootDir2+postfix
if not os.path.isdir(os.path.dirname(savePath+postfix)):
os.makedirs(os.path.dirname(savePath+postfix))
currentSavePath = savePath+postfix
try:
# update status
self.statusText.SetValue("Processing...")
self.statusText.SetForegroundColour(wx.BLACK)
# try reading the files
print "first path: "+i
print "second path: "+f
self.im1 = Image.open(i)
self.im2 = Image.open(f)
self.count += 1
# convert to matrix
self.mat1 = numpy.array(self.im1)
self.mat2 = numpy.array(self.im2)
# convert to uint16 for addition
self.mat1 = self.mat1.astype('uint16')
self.mat2 = self.mat2.astype('uint16')
# get offset
try:
self.offset = int(self.offsetCtrl.GetValue())
except ValueError:
#throw error
self.statusText.SetValue("Error: please enter integer offset")
self.statusText.SetForegroundColour(wx.RED)
return
# add and convert back to image (with offset)
self.result = (self.mat1 + self.mat2 + self.offset)/2
self.result[self.result > 255] = 255
# convert back to uint 8 for saving
self.result = self.result.astype('uint8')
self.imResult = Image.fromarray(self.result)
# self.imResult = Image.blend(self.im1, self.im2, 1)
self.imResult.save(currentSavePath,"bmp")
# update status
self.statusText.SetValue("Saved image to " + currentSavePath)
self.statusText.SetForegroundColour(wx.BLACK)
except IOError:
# throw error
self.statusText.SetValue("Error: cannot read file : " + i + " or " + f)
self.statusText.SetForegroundColour(wx.RED)
return

2-3分钟正常吗?它能走得更快吗?我应该降低最终图像的分辨率吗?

最佳答案

您可以计算它代表的总 IO 工作负载。

您有 500 张图片,每张 5 MB,您需要读取其中两张才能写入一张。因此,您读取 500*5*2 = 5 GB,并在磁盘上写入 2.5 GB。

假设它持续 3 分钟。这意味着 I/O 吞吐量在读取模式下为 27.7 MB/s,在写入模式下为 13.8 MB/s。对于经典的旋转圆盘,这个结果还算不错。

现在,如果您在这台笔记本电脑上安装了 SSD,则意味着您远未使 I/O 带宽饱和,并且您可能可以做得更好。例如,您可以尝试并行化该过程(通过引入线程池)。

关于python - 使用 Python 效率和速度的图像保存 I/O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31927146/

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