from PIL import Image import os def debug_image(image_path): if not os.path.exists(image_path): print(f"Error: {image_path} not found") return img = Image.open(image_path) print(f"Original mode: {img.mode}") # 模拟 convert_img.py 的处理流程 if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info): alpha = img.convert('RGBA').split()[-1] bg = Image.new("RGBA", img.size, (255, 255, 255, 255)) bg.paste(img, mask=alpha) img = bg img = img.convert('L') img = img.convert('1') print(f"Converted mode: {img.mode}") width, height = img.size print(f"Size: {width}x{height}") zeros = 0 non_zeros = 0 values = {} # 采样一些像素 for y in range(min(height, 10)): row_vals = [] for x in range(min(width, 10)): val = img.getpixel((x, y)) row_vals.append(str(val)) print(f"Row {y} first 10 pixels: {', '.join(row_vals)}") # 统计所有像素 for y in range(height): for x in range(width): val = img.getpixel((x, y)) if val == 0: zeros += 1 else: non_zeros += 1 if val not in values: values[val] = 0 values[val] += 1 print(f"Total pixels: {width*height}") print(f"Zeros (Black?): {zeros}") print(f"Non-zeros (White?): {non_zeros}") print(f"Non-zero values distribution: {values}") if __name__ == "__main__": # 尝试找一个存在的图片,或者创建一个 img_path = "test_image.png" if not os.path.exists(img_path): # 创建一个简单的测试图片 img = Image.new('RGB', (100, 100), color = 'white') # 画个黑框 from PIL import ImageDraw d = ImageDraw.Draw(img) d.rectangle([10, 10, 90, 90], outline='black', fill='black') img.save("debug_test.png") img_path = "debug_test.png" print("Created debug_test.png") debug_image(img_path)