使用save函数时出现意外的缩进

2024-07-07 05:36:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个读文件.csv文件的说明如下

E:\\Project_Face\\Projects\\arnold_schwarzenegger_01.jpg;1
E:\\Project_Face\\Projects\\arnold_schwarzenegger_02.jpg;1
E:\\Project_Face\\Projects\\emma_watson_01.jpg;0

我使用python提取路径和标签(例如0和1)。路径和标签用分号分隔。我使用了一个示例代码来提取文件的路径,将其重命名并保存在其他文件夹中。但是,我有如下错误。 enter image description here 当我替换save函数(save("_10_10_200_200.jpg"))中的路径时。效果很好。但是,如果我使用其他路径,例如。在

^{pr2}$

你能帮我修一下吗?事先谢谢。这是python代码

import sys, math, Image
import os
from __future__ import print_function

def Distance(p1,p2):
  dx = p2[0] - p1[0]
  dy = p2[1] - p1[1]
  return math.sqrt(dx*dx+dy*dy)

def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.BICUBIC):
  if (scale is None) and (center is None):
    return image.rotate(angle=angle, resample=resample)
  nx,ny = x,y = center
  sx=sy=1.0
  if new_center:
    (nx,ny) = new_center
  if scale:
    (sx,sy) = (scale, scale)
  cosine = math.cos(angle)
  sine = math.sin(angle)
  a = cosine/sx
  b = sine/sx
  c = x-nx*a-ny*b
  d = -sine/sy
  e = cosine/sy
  f = y-nx*d-ny*e
  return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)

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")

Tags: cropimagerightsavemathleftoffsetdest
1条回答
网友
1楼 · 发布于 2024-07-07 05:36:55

你的错误包括:

IndentationError: unexpected indent
IndentationError: unindent does not match any outer indentation level

这意味着行首的空格太少或太多。在

例如,这是错误的:

^{pr2}$

这也是错误的:

if c == 3:
    print('Hello world!')  # 4 spaces relative to "if" - ok
   print('Needs one more space')  # 3 spaces - error

根据PEP-8 coding standard,一般来说,Python代码应该在每一层上用4个空格缩进来编写。在

相关问题 更多 >