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

如何用Python提取网页GIF文件文本?代码报错求助

Fixing Tesseract OCR Error with GIF Images

Hey there, let's figure out why your OCR code works flawlessly for PNGs but throws a fit with GIFs. The core issue here is how PIL handles transparency and color palettes in GIF files—this conflicts with how Tesseract tries to save the image for processing.

The Problem Breakdown

Looking at your error trace, the critical line is:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

GIFs use a palette-based color mode (P mode), and when transparency is involved, PIL stores the transparency info as a tuple instead of a single integer. When Tesseract tries to save the image temporarily for OCR, it can't convert that tuple to an integer, causing the crash.

Quick Fix: Convert GIF to RGB Mode

The simplest fix is to convert the GIF to RGB mode right after opening it. This strips out the palette and transparency issues entirely, making the image compatible with Tesseract. Here's your modified code:

import pytesseract
import io
import requests
from PIL import Image

url = requests.get('http://article.sapub.org/email/10.5923.j.aac.20190902.01.gif')
img = Image.open(io.BytesIO(url.content))

# Convert to RGB mode to eliminate palette/transparency conflicts
img = img.convert('RGB')

text = pytesseract.image_to_string(img)
print(text)

Alternative: Preserve Background (Fill Transparency)

If you want to keep the original visual context instead of forcing RGB, you can fill transparent areas with a solid color (like white) before processing:

import pytesseract
import io
import requests
from PIL import Image

url = requests.get('http://article.sapub.org/email/10.5923.j.aac.20190902.01.gif')
img = Image.open(io.BytesIO(url.content))

# Handle transparency by filling with a white background
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
    # Create a white background image matching the GIF size
    background = Image.new('RGB', img.size, (255, 255, 255))
    # Paste the original image over the background, using its alpha channel as a mask
    background.paste(img, mask=img.split()[-1])
    img = background

text = pytesseract.image_to_string(img)
print(text)

Why This Works

PNGs usually use direct color modes (like RGBA) that PIL handles cleanly for Tesseract. GIFs rely on palettes, which introduce edge cases with transparency metadata. Converting to RGB or filling transparency removes these metadata conflicts, letting Tesseract process the GIF just like it does with PNGs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:18:35