python 解决手机拍的书籍图片发灰的问题

文章讲述了如何使用Python中的PIL和numpy库对手机拍摄的灰度图片进行处理,通过阈值分割提高打印质量。作者提供了详细的代码,包括处理单张图片和批量处理的函数,以及阈值调整的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

老师给发的作业经常是手机拍的,而不是扫描,背景发灰,如果二次打印就没有看了,象这样:

如果使用photoshop 处理,有些地方还是扣不干净,不如python 做的好,处理后如下:

具体的代码如下:

from PIL import Image
import numpy as np
import os

from PIL import Image, ImageOps

def process_image(input_path, output_path, threshold=110):
    # 打开图片并转换为灰度图像
    img = Image.open(input_path).convert('L')

    # 使用阈值分割,将灰色部分转为白色
    img = img.point(lambda x: 255 if x > threshold else x)

    # 获取原始文件名和扩展名
    base_name, ext = os.path.splitext(os.path.basename(input_path))

    # 构造新的文件名
    new_file_name = f"{base_name}-1{ext}"

    # 保存处理后的图片
    img.save(os.path.join(output_path, new_file_name))

def process_image_array(input_paths, output_path):
    # 创建输出文件夹(如果不存在)
    os.makedirs(output_path, exist_ok=True)

    # 处理每张图片
    for input_path in input_paths:
        process_image(input_path, output_path)

if __name__ == "__main__":
    input_path = "D:\\python_code\\temp\\2.jpg"  # 替换为你的输入图片路径
    output_path = "D:\\python_code\\temp\\2-1.jpg"  # 替换为你的输出图片路径

    input_paths = ["D:\\python_code\\temp\\1.jpg", 
                   "D:\\python_code\\temp\\2.jpg",
                   "D:\\python_code\\temp\\3.jpg",
                   "D:\\python_code\\temp\\4.jpg",
                   ]  
    # 替换为你的输入图片路径数组
    output_path = "D:\\python_code\\temp"  # 替换为你的输出文件夹路径

    process_image_array(input_paths, output_path)

    #process_image(input_path, output_path)

如果发现识别有问题,可以更改阈值,试试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值