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

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

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

具体的代码如下:

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)

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

相关推荐
weixin_440730504 分钟前
函数return、参数、参数组、递归函数、高阶函数等小结
开发语言·python
天赐范式18 分钟前
天赐范式第181天:让视图开始切换——PBFT视图切换与从safety到liveness的跨越
python·pbft·数字生命·天赐范式·动态运行时·拜占庭容错
sugarzhangnotes29 分钟前
【无标题】
开发语言·人工智能·python
weixin_447195291 小时前
【无标题】
pytorch·python
迅猛龙办公室1 小时前
Python实现简单的人名对话
python
阿坨2 小时前
firestart:一行命令启动你的日常应用和网页
python·pypi·cli·click
老歌老听老掉牙2 小时前
麻花钻切屑形态演变的力学机制与临界条件分析
python·算法·钻头
happylifetree2 小时前
Python18(补充):练习
python
weixin_416667963 小时前
银河麒麟V10看门狗试验-1
网络·chrome·python
古城小栈3 小时前
Pydantic 从入门到实践全讲解
python