You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手求助:过曝图片检测代码缩进错误及文件移动问题

Hey there! Let's tackle your problem step by step.

First: Fixing the TabError

The TabError: inconsistent use of tabs and spaces in indentation error means your code mixes tabs and spaces for indentation—and Python is extremely strict about using only one type of indentation consistently.

Here's how to fix it:

  • Open your code in an editor that shows invisible characters (like VS Code, PyCharm, or Sublime Text). Enable the "show whitespace" option to spot where tabs (usually marked as →) and spaces (dots .) are mixed.
  • Replace all tabs with 4 spaces (the Python-recommended indentation size), or set your editor to automatically convert tabs to spaces. For example, in VS Code, you can toggle this via Settings > "Editor: Insert Spaces" and set "Editor: Tab Size" to 4.

Other Key Issues in Your Code (To Get It Working Properly)

Beyond the indentation error, there are a few missing or incorrect pieces that will stop your script from running as intended:

1. Missing glob import

You're using glob.glob() but haven't imported the glob module. Add this line at the top of your code:

import glob

2. Undefined variables

The variables bright_pixel, total_pixel, and dark_thres aren't defined in your snippet. You'll need to calculate these to detect overexposed images. For example:

# Define your threshold (adjust based on what you consider "overexposed")
dark_thres = 0.75  # If 75%+ of pixels are bright, mark as overexposed
total_pixel = gray.size  # Total number of pixels in the grayscale image
bright_pixel = np.sum(gray > 240)  # Count pixels brighter than 240 (0-255 scale)
image_value2 = (bright_pixel / total_pixel) > dark_thres

3. You're re-saving images instead of moving them

Your current code uses Image.fromarray(frame).save(...) to copy the image, but if you just want to move the original file (faster and no quality loss), use shutil.move() instead:

shutil.move(file, './reject/')

4. Ensure the reject folder exists

If the reject folder doesn't exist, your script will throw an error when trying to save/move files. Add this line before your loop to create it (and do nothing if it already exists):

os.makedirs('./reject', exist_ok=True)

Corrected Example Code

Putting it all together, here's a revised version of your script:

import cv2
import os
import shutil
import numpy as np
import glob  # Added missing import

os.chdir("./sample")
os.makedirs('./reject', exist_ok=True)  # Ensure reject folder exists

# Define overexposure threshold parameters
dark_thres = 0.75
bright_pixel_threshold = 240

for file in glob.glob("*.jpg"):
    frame = cv2.imread(file)
    if frame is None:  # Add check in case image fails to load
        print(f"Failed to load {file}")
        continue
    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    total_pixel = gray.size
    bright_pixel = np.sum(gray > bright_pixel_threshold)
    is_overexposed = (bright_pixel / total_pixel) > dark_thres

    if is_overexposed:
        print(f"{file} - Image is overexposed!")
        shutil.move(file, './reject/')  # Move instead of re-saving

内容的提问来源于stack exchange,提问作者farhanrbn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:48:56