我正在嘗試將一些tiff文件從2000 * 2000重新採樣到500 * 500。 我創建了一個函數,我嘗試了一個文件,它很好地工作。現在我想將它應用於我擁有的所有可用文件。在Python中對圖像進行縮減採樣
我想編寫函數的輸出,並根據我的知識編寫了代碼,並在寫入out_file時收到錯誤。我已經複製了函數和主代碼供您考慮。主代碼根據其命名讀取tif文件並應用該函數。如果某人能指導我我的錯誤所在,我會很感激。
#*********function********************
def ResampleImage(infile):
fp = open(infile, "rb")
p = ImageFile.Parser()
while 1:
s = fp.read()
if not s:
break
p.feed(s)
img = p.close()
basewidth = 500
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
outfile=img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
return outfile
#********* main code********
import os,sys
import ImageResizeF
import PIL
from PIL import Image
from PIL import Image,ImageFile
tpath = 'e:/.../resampling_test/all_tiles/'
tifext = '.tif'
east_start = 32511616
north_start = 5400756
ilist = range (0,14)
jlist = range (0,11)
north = north_start
ee = ',4_'
en = ',2'
for i in ilist:
east = east_start
north = north_start + i * 400
snorth = str (north)
for j in jlist:
east = east_start + j * 400
seast = str (east)
infile = tpath + seast + ee + snorth + en + tifext
output = tpath + seast + ee + snorth + en + '_res'+tifext
out_file = ImageResizeF.ResampleImage(infile)
out_file.write (output)
out_file.close()
你還應該張貼ImageResizeF – elyase
代碼你是真正的懸念大師。你得到的神祕錯誤是什麼? – misha
@ elyase:ImageResizeF文件是第一個功能 – user2355306