how to extract text from web gif file using python - python

I am trying to extract text from a gif image using the below code, it has worked for png format not working for gif.
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))
text = pytesseract.image_to_string(img)
print(text)
getting this error
C:\python\lib\site-packages\PIL\Image.py:1048: UserWarning: Couldn't allocate palette entry for transparency
warnings.warn("Couldn't allocate palette entry for transparency")
Traceback (most recent call last):
File "D:/elifesciences/prox.py", line 8, in <module>
text = pytesseract.image_to_string(img)
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 345, in image_to_string
}[output_type]()
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 344, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 242, in run_and_get_output
temp_name, input_filename = save_image(image)
File "C:\python\lib\site-packages\pytesseract\pytesseract.py", line 173, in save_image
image.save(input_file_name, format=extension, **image.info)
File "C:\python\lib\site-packages\PIL\Image.py", line 2088, in save
save_handler(self, fp, filename)
File "C:\python\lib\site-packages\PIL\GifImagePlugin.py", line 507, in _save
_write_single_frame(im, fp, palette)
File "C:\python\lib\site-packages\PIL\GifImagePlugin.py", line 414, in _write_single_frame
_write_local_header(fp, im, (0, 0), flags)
File "C:\python\lib\site-packages\PIL\GifImagePlugin.py", line 532, in _write_local_header
transparency = int(transparency)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
Process finished with exit code 1

The idea is to convert each of the frames to RGB image before performing the OCR on them, as shown below -
for frame in range(0,img.n_frames):
img.seek(frame)
imgrgb = img.convert('RGBA')
imgrgb.show()
text = pytesseract.image_to_string(imgrgb)
print(text)
Working sample - https://colab.research.google.com/drive/1ctjk3hH0HUaWv0st6UpTY-oo9C9YCQdw

Related

Open PIL image from zip (Kaggle competition)

I am trying to read an image from kaggle competition (It is an old competition, but I would like to practice):
https://www.kaggle.com/competitions/dogs-vs-cats-redux-kernels-edition
I am trying to read images from the training file zip using this code:
def get_files_names(zip_file_path):
with ZipFile(zip_file_path) as myzip:
return myzip.namelist()
def get_image(zip_path, image_name):
with ZipFile(zip_path) as myzip:
# print(myzip.namelist()[:10])
with myzip.open(image_name) as myfile:
# img = Image.open(myfile)
img = Image.open(myfile)
return img
names = get_files_names(train_file_path)
img = get_image(train_file_path, names[1])
img.show()
I am getting this error:
Traceback (most recent call last):
File "/cats_vs_dogs/unrelated_file.py", line 46, in <module>
img.show()
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 2205, in show
_show(self, title=title, command=command)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3167, in _show
_showxv(image, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3181, in _showxv
ImageShow.show(image, title, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 56, in show
if viewer.show(image, title=title, **options):
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 81, in show
return self.show_image(image, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 107, in show_image
return self.show_file(self.save_image(image), **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 103, in save_image
return image._dump(format=self.get_format(image), **self.options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 636, in _dump
self.load()
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageFile.py", line 247, in load
s = read(self.decodermaxblock)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 400, in load_read
s = self.fp.read(read_bytes)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 930, in read
data = self._read1(n)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 998, in _read1
data += self._read2(n - len(data))
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 1030, in _read2
data = self._fileobj.read(n)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 753, in read
self._file.seek(self._pos)
AttributeError: 'NoneType' object has no attribute 'seek'
If I extract the file into finder (using mac), then I see this image:
Also, if I try to convert the RGB image into into a numpy array np.array(img), I get this result:
What am I doing wrong?

How to read PDF from file storage object in pdf2image?

I am working with flask, where I am uploading a pdf file to convert it to an image and perform OCR using pytesseract.
However, pdf2image is not able to read the uploaded image.
I tried searching on the internet but I could not find anything.
I tried passing the file storage object directly, but am getting an error, my code looks like this:
log_file = request.files.get('pdf')
images = convert_from_path(log_file)
text = ""
for img in images:
im = img
ocr_dict = pytesseract.image_to_data(im, lang='eng', output_type=Output.DICT)
text += " ".join(ocr_dict['text'])
cleaned_text = clean_text(txt=text)
which gives this error,
**TypeError: expected str, bytes or os.PathLike object, not FileStorage**
I also tried doing,
log_file = request.files.get('pdf')
images = convert_from_path(log_file.read())
text = ""
for img in images:
im = img
ocr_dict = pytesseract.image_to_data(im, lang='eng', output_type=Output.DICT)
text += " ".join(ocr_dict['text'])
cleaned_text = clean_text(txt=text)
which gives error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/pdf2image/pdf2image.py", line 458, in pdfinfo_from_path
proc = Popen(command, env=env, stdout=PIPE, stderr=PIPE)
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child
self.pid = _posixsubprocess.fork_exec(
ValueError: embedded null byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py", line 467, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask/views.py", line 84, in view
return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py", line 582, in dispatch_request
resp = meth(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask_httpauth.py", line 172, in decorated
return self.ensure_sync(f)(*args, **kwargs)
File "/home/ubuntu/Credit_Scoring/API_Script/temp2.py", line 38, in post
json_text = coi_ocr.get_coi_ocr_text()
File "/home/ubuntu/Credit_Scoring/API_Script/ocr_script/certificate_of_incorporation/coi_ocr_script_pdf.py", line 51, in get_coi_ocr_text
text1 = self.extract_text_from_COI()
File "/home/ubuntu/Credit_Scoring/API_Script/ocr_script/certificate_of_incorporation/coi_ocr_script_pdf.py", line 16, in extract_text_from_COI
images = convert_from_path(self.fl)
File "/usr/local/lib/python3.8/dist-packages/pdf2image/pdf2image.py", line 98, in convert_from_path
page_count = pdfinfo_from_path(pdf_path, userpw, poppler_path=poppler_path)["Pages"]
File "/usr/local/lib/python3.8/dist-packages/pdf2image/pdf2image.py", line 489, in pdfinfo_from_path
"Unable to get page count.\n%s" % err.decode("utf8", "ignore")
UnboundLocalError: local variable 'err' referenced before assignment
Okay, it turns out I need to pass convert_from_bytes instead of convert_from_path.

OsError(err) in PIL Image library using Image.paste

I am doing a small script to paste an image into a black background to have same size images for a dataset. Here is the script:
for file in glob.glob(imagetteDir):
r = str(file)
image = Image.open(file)
img = np.zeros([100,100,3],dtype=np.uint8)
imageio.imwrite('black.tif', img)
black = Image.open('black.tif')
position = ((black.width - image.width), (black.height - image.height))
print(position)
Image.Image.paste(black,image,position)
dirr , name = r.split("imagette")
path1 = name.raplace("Hexagone_resized","Hexagone")
print(path1)
black.save(imagetteresizeDir+path1)
So Image.Image.paste() make the following error that i didnt find how to resolve:
Traceback (most recent call last):
File "C:\Users\user\Documents\Project\segmentation\Imagette creation\resizing.py", line 29, in <module>
Image.Image.paste(black,image)
File "C:\Users\user\Anaconda3\envs\tf_gpu\lib\site-packages\PIL\Image.py", line 1455, in paste
im.load()
File "C:\Users\user\Anaconda3\envs\tf_gpu\lib\site-packages\PIL\TiffImagePlugin.py", line 1070, in load
return self._load_libtiff()
File "C:\Users\user\Anaconda3\envs\tf_gpu\lib\site-packages\PIL\TiffImagePlugin.py", line 1182, in _load_libtiff
raise OSError(err)
OSError: -2
Have you any idea from where it might come?
Thanks in advance for your help

OSError: [Errno 9] Bad file descriptor while saving image with PIL

i amusing PIL to write some text inside and an image and then saving it at the same location. Following is my function which does this
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def add_text_to_image(image_path):
img = Image.open(image_path)
img = img.convert('RGB')
widht, height = img.size
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("helvetica.ttf", 20)
draw.text((0, 0), "Some text", (0, 0, 0), font=font)
img.save(image_path)
But i am getting following error.
Traceback (most recent call last): File
"/usr/local/lib/python3.6/site-packages/background_task/tasks.py",
line 43, in bg_runner func(*args, **kwargs) File
"/home/paksign/app/app/document/tasks.py", line 74, in
document_status_changed_to_completed
add_branding_texts_to_document_images(meta) File
"/home/paksign/app/app/document/utils.py", line 277, in
add_branding_texts_to_document_images for snapshot in snapshots: File
"/home/paksign/app/app/document/utils.py", line 270, in
add_text_to_image img.save(image_path) File
"/usr/local/lib/python3.6/site-packages/PIL/Image.py", line 1994, in
save save_handler(self, fp, filename) File
"/usr/local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line
761, in _save ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0,
rawmode)], bufsize) File
"/usr/local/lib/python3.6/site-packages/PIL/ImageFile.py", line 519,
in _save s = e.encode_to_file(fh, bufsize) OSError: [Errno 9] Bad file
descriptor Marking task
document.tasks.document_status_changed_to_completed as failed
i have tried some solution from internet but nothing is working and i don't know what i am doing wrong here. any help is appreciated

IOError: broken data stream when reading image file

This is the code(a simple python code which could add a number to a picture):
from PIL import Image, ImageDraw, ImageFont
def addnum(number, filepath):
img = Image.open(filepath)
width, height = img.size
draw = ImageDraw.Draw(img)
front_size = height/4
ttf_front = ImageFont.truetype('Arial.ttf', front_size)
draw.text((width - front_size, 0), number, (255, 0, 0), font = ttf_front)
del draw
img.save('wechat_addnum.jpg')
img.show()
if __name__ == '__main__':
addnum('4','wechat.jpg')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/Users/JourneyWoo/Python/python100/python001/python000.py", line 27, in <module>
addnum('4','wechat.jpg')
File "/Users/JourneyWoo/Python/python100/python001/python000.py", line 16, in addnum
draw = ImageDraw.Draw(img)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageDraw.py", line 299, in Draw
return ImageDraw(im, mode)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageDraw.py", line 60, in __init__
im.load()
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageFile.py", line 244, in load
raise_ioerror(err_code)
File "/Users/JourneyWoo/anaconda/lib/python2.7/site-packages/PIL/ImageFile.py", line 59, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
I don't know the real problem, and from the google and other questions from stackoverflow I cannot find the method which could solve this problem.
Thank you!!
This is a know issue. Try to update Pillow using pip install Pillow --upgrade

Categories

Resources