实用脚本能提取图片吗?

wen 实用脚本 6

本文目录导读:

实用脚本能提取图片吗?

  1. 1️⃣ 提取图片中的文字(OCR-光学字符识别)
  2. 2️⃣ 提取图片中的主要颜色(调色板)
  3. 3️⃣ 提取图片中人脸/物体
  4. 4️⃣ 提取图片中的二维码/条码
  5. 5️⃣ 提取图片中的表格数据
  6. 📦 一键提取脚本(综合版)
  7. ⚡ 快速使用建议

可以的!根据你的具体需求,有多种方法可以“从图片中提取”不同类型的内容,以下是几个实用脚本方案:

1️⃣ 提取图片中的文字(OCR-光学字符识别)

工具: pytesseract + Pillow

# 安装:pip install pytesseract pillow
from PIL import Image
import pytesseract
# 指定tesseract路径(Windows需要)
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def extract_text(image_path):
    """从图片中提取文字"""
    img = Image.open(image_path)
    text = pytesseract.image_to_string(img, lang='chi_sim+eng')  # 中文+英文
    return text
# 使用
text = extract_text('screenshot.png')
print(text)

支持: 简体中文、英文、数字、特殊字符

2️⃣ 提取图片中的主要颜色(调色板)

# 安装:pip install colorthief
from colorthief import ColorThief
import matplotlib.pyplot as plt
def extract_colors(image_path, color_count=5):
    """提取图片中的主要颜色"""
    color_thief = ColorThief(image_path)
    palette = color_thief.get_palette(color_count=color_count)
    # 显示色块
    plt.imshow([[palette[i] for i in range(color_count)]])
    plt.axis('off')
    plt.show()
    return palette
colors = extract_colors('photo.jpg', 5)

3️⃣ 提取图片中人脸/物体

# 安装:pip install opencv-python
import cv2
def extract_faces(image_path, output_dir='faces/'):
    """提取图片中所有人脸并保存"""
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face_cascade = cv2.CascadeClassifier(
        cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
    )
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    for i, (x, y, w, h) in enumerate(faces):
        face = img[y:y+h, x:x+w]
        cv2.imwrite(f'{output_dir}face_{i}.jpg', face)
    return len(faces)
count = extract_faces('group_photo.jpg')
print(f'提取了 {count} 张人脸')

4️⃣ 提取图片中的二维码/条码

# 安装:pip install pyzbar pillow
from pyzbar.pyzbar import decode
from PIL import Image
def extract_barcode(image_path):
    """从图片中提取二维码/条码信息"""
    img = Image.open(image_path)
    barcodes = decode(img)
    results = []
    for barcode in barcodes:
        data = barcode.data.decode('utf-8')
        barcode_type = barcode.type
        results.append({'data': data, 'type': barcode_type})
        print(f'类型: {barcode_type}, 内容: {data}')
    return results
data = extract_barcode('qrcode.jpg')

5️⃣ 提取图片中的表格数据

# 安装:pip install camelot-py
import camelot
def extract_tables(pdf_or_image_path):
    """从PDF或图片中提取表格"""
    tables = camelot.read_pdf(pdf_or_image_path, pages='1', flavor='lattice')
    # 保存为CSV
    tables[0].to_csv('extracted_table.csv')
    # 转为DataFrame
    df = tables[0].df
    return df
df = extract_tables('report.pdf')

📦 一键提取脚本(综合版)

import os
from PIL import Image
import pytesseract
class ImageExtractor:
    def __init__(self, image_path):
        self.image_path = image_path
        self.img = Image.open(image_path)
    def extract_text(self, lang='chi_sim+eng'):
        """提取文字"""
        return pytesseract.image_to_string(self.img, lang=lang)
    def extract_metadata(self):
        """提取图片元数据(Exif信息)"""
        info = {}
        exif = self.img._getexif()
        if exif:
            for tag_id, value in exif.items():
                tag = Image.ExifTags.TAGS.get(tag_id, tag_id)
                info[tag] = value
        return info
    def extract_palette(self, colors=5):
        """提取主色调"""
        reduced = self.img.quantize(colors)
        palette = reduced.getpalette()[:colors*3]
        return [(palette[i], palette[i+1], palette[i+2]) 
                for i in range(0, len(palette), 3)]
# 使用
extractor = ImageExtractor('example.jpg')
print('文字内容:', extractor.extract_text())
print('拍摄时间:', extractor.extract_metadata().get('DateTime', '未知'))

⚡ 快速使用建议

你需要 推荐方案 安装命令
提取文字 OCR pip install pytesseract pillow
提取人脸 OpenCV pip install opencv-python
提取颜色 ColorThief pip install colorthief
提取二维码 Pyzbar pip install pyzbar
提取表格 Camelot pip install camelot-py

最实用的组合: 如果你想快速从图片“扒”出文字,用第1个OCR脚本即可,准确率很高(特别是清晰图片)。

需要针对某个具体场景(比如批量处理、特定格式图片)优化脚本吗?我可以帮你调整!

抱歉,评论功能暂时关闭!