所以,我有一堆扫描的图像,周围有恼人的白色,我想删除。我正在准备一个使用numpy数组的算法来找到最接近的完整白色像素行和列,将它们裁剪到那些位置。这是我的代码:
from scipy import misc
import os
import datetime
a0 = datetime.datetime.now()
print("Imported at " + str(a0))
def run():
inputdir = '/Users/nicholasezyk/Documents/funeralhomescans'
os.chdir(inputdir)
#batch image renaming
counter = 1
for file in os.listdir(inputdir):
if file.endswith('.jpg'):
img = misc.imread(file)
print(file + ' being analyzed.')
cropx = -1
cropy = -1
lx = img.shape[0]
ly = img.shape[1]
countx = 0
county = 0
while countx < lx:
pix = img[countx, 0]
if pix == (255, 255, 255):
tempcountx = 1
scorex = 1
while tempcountx < ly:
if img[countx, tempcountx] == (255, 255, 255):
scorex = scorex + 1
tempcountx = tempcountx + 1
if 1000 * (scorex / ly) > 1000:
cropx = tempcountx
break
while county < ly:
pix = img[0, county]
if pix == (255, 255, 255):
tempcounty = 1
scorey = 1
while tempcounty < lx:
if img[tempcounty, county] == 255:
scorey = scorey + 1
tempcounty = tempcounty + 1
if 1000 * (scorey / lx) == 1000:
cropy = tempcounty
break
send = img[0:cropx, 0:cropy]
misc.imsave('crop' + str(counter) + '.png', send)
print(file + ' cropped and exported as ' + 'crop' + str(counter) + '.png')
run()
现在,这是我的控制台声明:
Imported at 2014-10-04 15:12:32.237369
test.jpg being analyzed.
Traceback (most recent call last):
File "/Users/nicholasezyk/Documents/whiteout.py", line 52, in <module>
run()
File "/Users/nicholasezyk/Documents/whiteout.py", line 26, in run
if pix == (255, 255, 255):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
由于我不熟悉语法,如果有人能帮我弄清楚如何使用numpy数组,那就太棒了。