Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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中作为函数对图像进行下采样_Python_Image Processing_Python Imaging Library_Tiff_Resampling - Fatal编程技术网

在Python中作为函数对图像进行下采样

在Python中作为函数对图像进行下采样,python,image-processing,python-imaging-library,tiff,resampling,Python,Image Processing,Python Imaging Library,Tiff,Resampling,我正在尝试将一些tiff文件从2000*2000重新采样到500*500。 我已经创建了一个函数,并尝试了一个文件,它工作得很好。现在我想将其应用于所有可用的文件 我想写函数的输出,我已经根据我的知识写了代码,我收到了关于写出_文件的错误。我已经复制了函数和主代码供您考虑。主代码只是根据tif文件的名称读取它们,并应用该函数。如果有人能指引我的错误所在,我将不胜感激 #*********function******************** def ResampleImage(infile)

我正在尝试将一些tiff文件从2000*2000重新采样到500*500。 我已经创建了一个函数,并尝试了一个文件,它工作得很好。现在我想将其应用于所有可用的文件

我想写函数的输出,我已经根据我的知识写了代码,我收到了关于写出_文件的错误。我已经复制了函数和主代码供您考虑。主代码只是根据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.ResampleImage
返回的内容有关,它是文件句柄吗?否则,您就错了,因为您无法关闭()不是文件句柄的对象。您应该在函数内执行整个文件处理或返回图像对象,例如:

def process_image(image):
    "Processes the image"
    image.resize((x, y), Image.ANTIALIAS) # or whatever you are doing to the image
    return image

image = Image.open('infile.tiff')
proc_image = process_image(image)
proc_image.save('outfile.tiff')

你还应该发布ImageResizefy的代码你真的是悬念大师。您遇到的神秘错误是什么?@elyase:ImageResizeF文件是第一个函数错误是:它无法写入文件非常感谢您的提示。是的,问题是说“保存”。