Unexpected indent when using save function in Python

使用 Python 从该文件中提取路径和标签(如 0 和 1)。路径和标签以分号分隔。使用了一段示例代码来提取文件的路径,重命名并保存在另一个文件夹中。但是,出现了如下错误:

IndentationError: unexpected indent

当在 save 函数中替换路径(save(“_10_10_200_200.jpg”))时,它可以正常工作。但是,如果使用其他路径,例如 save(“.\modified\”+img.rstrip().split(‘\’)[5]+“_10_10_200_200.jpg”),就会出现错误。

  1. 解决方案: 为了解决这个问题,需要确保 save 函数中的缩进正确无误。在 Python 中,缩进非常重要,它用于表示代码块的层级关系。在 save 函数中,应该使用 4 个空格来表示函数体的缩进。确保所有 save 函数的调用都在函数体的正确位置,并且缩进正确无误。

修改后的代码如下:

def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
  # calculate offsets in original image
  offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
  offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
  # get the direction
  eye_direction = (eye_right[0] - eye_left[0], eye_right[1] - eye_left[1])
  # calc rotation angle in radians
  rotation = -math.atan2(float(eye_direction[1]),float(eye_direction[0]))
  # distance between them
  dist = Distance(eye_left, eye_right)
  # calculate the reference eye-width
  reference = dest_sz[0] - 2.0*offset_h
  # scale factor
  scale = float(dist)/float(reference)
  # rotate original around the left eye
  image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
  # crop the rotated image
  crop_xy = (eye_left[0] - scale*offset_h, eye_left[1] - scale*offset_v)
  crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)
  image = image.crop((int(crop_xy[0]), int(crop_xy[1]), int(crop_xy[0]+crop_size[0]), int(crop_xy[1]+crop_size[1])))
  # resize it
  image = image.resize(dest_sz, Image.ANTIALIAS)
  return image

def readFileNames():
    try:
        inFile = open('E:\\Project_Face\\Projects\\image_data.csv')
    except:
        raise IOError('There is no file named path_to_created_csv_file.csv in current directory.')
        return False

    picPath = []
    picIndex = []
    for line in inFile.readlines():
        if line != '':

            fields = line.rstrip().split(';')
            picPath.append(fields[0])
            picIndex.append(int(fields[1]))

    return (picPath, picIndex)


if __name__ == "__main__":
    [images, indexes]=readFileNames()
    if not os.path.exists("modified"):
        os.makedirs("modified")
        for img in images:
            image =  Image.open(img)
            CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.1,0.1), dest_sz=(200,200)).save(".\\modified\\"+img.rstrip().split('\\')[5]+"_10_10_200_200.jpg")
            CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.2,0.2), dest_sz=(200,200)).save(".\\modified\\"+img.rstrip().split('\\')[5]+"_20_20_200_200.jpg")
            CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.3,0.3), dest_sz=(200,200)).save(".\\modified\\"+img.rstrip().split('\\')[5]+"_30_30_200_200.jpg")
            CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.2,0.2)).save("_20_20_70_70.jpg").save(".\\modified\\"+img.rstrip().split('\\')[5]+"_20_20_70_70.jpg")

保存图像时,save 函数的缩进正确无误。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值