How to read PDF from file storage object in pdf2image? - python

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.

Related

Expected bytes, HTTPResponse found

I want to use linebot to make an object detection application, and when I retrieve the url which is a http type, the system called an error.
[2022-11-23 19:13:12,335] ERROR in app: Exception on /callback [POST]
Traceback (most recent call last):
File "C:\officiallandprice\myproject\myenv\lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "C:\officiallandprice\myproject\myenv\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\officiallandprice\myproject\myenv\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "C:\officiallandprice\myproject\myenv\lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "part1deploy_3.py", line 55, in callback
handler.handle(body, signature)
File "C:\officiallandprice\myproject\myenv\lib\site-packages\linebot\webhook.py", line 259, in handle
self.__invoke_func(func, event, payload)
File "C:\officiallandprice\myproject\myenv\lib\site-packages\linebot\webhook.py", line 271, in __invoke_func
func(event)
File "part1deploy_3.py", line 201, in handle_content_message
text_detected1=text_detected(user.user_id)
File "part1deploy_3.py", line 81, in text_detected
image = vision.Image(content=input_file)
File "C:\officiallandprice\myproject\myenv\lib\site-packages\proto\message.py", line 604, in __init__
super().__setattr__("_pb", self._meta.pb(**params))
TypeError: expected bytes, HTTPResponse found
127.0.0.1 - - [23/Nov/2022 19:13:12] "POST /callback HTTP/1.1" 500 -
I do not have ideas which part of the code below requires a byte type file to make a detection(the PIL module?).
Is it necessary to change to the byte type, when it comes to an object detection?
def text_detected(user_id):
input_file=urllib.request.urlopen (
'https://storage.googleapis.com/
img_platecapture/{}.jpg'.format(user_id))
image = vision.Image(content=input_file)
response = vision_client.text_detection(image=image)
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
img = Image.open(input_file)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("simsun.ttc", 18)
for text in response.text_annotations[1::]:
ocr = text.description
draw.text((bound.vertices[0].x-25, bound.vertices[0].y-25),ocr,fill=(255,0,0),font=font)
draw.polygon(
[
bound.vertices[0].x,
bound.vertices[0].y,
bound.vertices[1].x,
bound.vertices[1].y,
bound.vertices[2].x,
bound.vertices[2].y,
bound.vertices[3].x,
bound.vertices[3].y,
],
None,
'yellow',
)
texts=response.text_annotations
a=str(texts[0].description.split())
b=re.sub(u"([^\u4e00-\u9fa5\u0030-u0039])","",a)
b1="".join(b)
print("偵測到的地址為:",b1)
return b1
#handler.add(MessageEvent, message=ImageMessage)
def handle_content_message(event):
message_content = line_bot_api.get_message_content(event.message.id)
user = line_bot_api.get_profile(event.source.user_id)
data=b''
for chunk in message_content.iter_content():
data+= chunk
global bucket_name
bucket_name = 'img_platecapture'
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(f'{user.user_id}.jpg')
blob.upload_from_string(data)
text_detected1=text_detected(user.user_id) ####Here's the problem
line_bot_api.reply_message(
event.reply_token,
messages=TextSendMessage(
text=text_detected1
))

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?

Autokey: sporadic error on system.exec_command

I have a script that grabs the window title and writes out a debug statement based on whether the file title includes a certain string.
active_title = window.get_active_title()
counter = system.exec_command("date '+%s'")
newcounter = counter[-5:]
counter = newcounter
time.sleep(.4)
if '.php' in active_title:
output = "die('<pre>[" + str(counter) + "] ' . date('Y-m-d H:i:s'));"
time.sleep(.2)
clipboard.fill_clipboard(output)
time.sleep(.2)
keyboard.send_keys('<ctrl>+v')
if '.js' in active_title:
output = "console.log('[" + str(counter) + "] ' + Date.now())"
time.sleep(.2)
clipboard.fill_clipboard(output)
time.sleep(.2)
keyboard.send_keys('<ctrl>+v')
time.sleep(.2)
keyboard.send_keys("<ctrl>+s")
99% of the time it works without issue, but that 100th time it throws this error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/autokey/service.py", line 485, in execute
exec(script.code, scope)
File "<string>", line 2, in <module>
File "/usr/lib/python3/dist-packages/autokey/scripting.py", line 1104, in get_active_title
return self.mediator.interface.get_window_title()
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1158, in get_window_title
return self.get_window_info(window, traverse).wm_title
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1074, in get_window_info
return self._get_window_info(window, traverse)
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1081, in _get_window_info
new_wm_class = self._try_get_window_class(window)
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1151, in _try_get_window_class
wm_class = window.get_wm_class()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 660, in get_wm_class
d = self.get_full_property(Xatom.WM_CLASS, Xatom.STRING)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 452, in get_full_property
prop = self.get_property(property, type, 0, sizehint)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 435, in get_property
r = request.GetProperty(display = self.display,
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/rq.py", line 1481, in __init__
self.reply()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/rq.py", line 1493, in reply
self._display.send_and_recv(request = self._serial)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 556, in send_and_recv
gotreq = self.parse_response(request)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 643, in parse_response
gotreq = self.parse_request_response(request) or gotreq
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 720, in parse_request_response
req = self.get_waiting_replyrequest()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 847, in get_waiting_replyrequest
raise RuntimeError("Request reply to unknown request. Can't happen!")
RuntimeError: Request reply to unknown request. Can't happen!
Closing and restarting Autokey helps, but I'd like to avoid the error if at all possible. I have several scripts that get the window title, and this is the only script that has this issue. Is there something wrong with using system.exec_command?
Since you are invoking the clipboard, it helps to add to the start and the end
os.system("sleep .1; xsel -cb")
to clear it.

Solving IOError while generating a pdf using reportlab and generated qr code image

I am trying to generate a qr code from text, and then insert into a reportlab pdf.
My code:
def qr_code_as_image(text):
from io import BytesIO
print("In show_qr")
img = generate_qr_code(text)
print(img, type(img))
i = Image(img)
print(i, type(i))
return i
def add_patient_header_with_qr(self):
line1 = ("Name", self.linkedcustomer.name,
"Age", self.linkedcustomer.age())
line2 = ("MRD No.", self.linkedcustomer.cstid,
"Date", self.prescription_time)
line3 = ("No.", "#", "Doctor", self.doc.name)
datatb = [line1, line2, line3]
patientdetailstable = Table(datatb)
patientdetailstable.setStyle(self.patientdetails_style)
col1 = patientdetailstable
checkin_url = reverse('clinicemr', args=[self.checkin.checkinno])
qr_image = qr_code_as_image(checkin_url)
qr_image.hAlign = 'LEFT'
col2 = Table([[qr_image]])
tblrow1 = Table([[col1, col2]], colWidths=None)
tblrow1.setStyle(self.table_left_top_align)
self.elements.append(tblrow1)
def final_generate(self, footer_content, action=None):
with NamedTemporaryFile(mode='w+b') as temp:
from django.http import FileResponse, Http404
from functools import partial
# use the temp file
cmd = "cat " + str(temp.name)
print(os.system(cmd))
print(footer_content, type(footer_content))
doc = SimpleDocTemplate(
temp.name,
pagesize=A4,
rightMargin=20,
leftMargin=20,
topMargin=20,
bottomMargin=80,
allowSplitting=1,
title="Prescription",
author="System.com")
frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height,
id='normal')
template = PageTemplate(
id='test',
frames=frame,
onPage=partial(footer, content=footer_content)
)
doc.addPageTemplates([template])
doc.build(self.elements,
onFirstPage=partial(footer, content=footer_content),
onLaterPages=partial(footer, content=footer_content)
)
print(f'Generated {temp.name}')
I get the following output:
2020-11-29 13: 06: 33, 915 django.request ERROR Internal Server Error: / clinic/presc/k-0NGpApcg
Traceback(most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 655, in open_for_read
return open_for_read_by_name(name, mode)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 599, in open_for_read_by_name
return open(name, mode)
ValueError: embedded null byte
During handling of the above exception, another exception occurred:
Traceback(most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 658, in open_for_read
return getBytesIO(datareader(name) if name[:5].lower() == 'data:' else urlopen(name).read())
File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.6/urllib/request.py", line 517, in open
req.timeout = timeout
AttributeError: 'bytes' object has no attribute 'timeout'
During handling of the above exception, another exception occurred:
Traceback(most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/joel/myappointments/clinic/views.py", line 6879, in GoGetPrescription
clinicobj = clinicobj,
File "/home/joel/myappointments/clinic/views.py", line 16222, in PDFPrescriptions
return prescription.generate_pdf(action=action, rating=True)
File "/home/joel/myappointments/clinic/views.py", line 15415, in generate_pdf
return self.final_generate(footer_content, action=action)
File "/home/joel/myappointments/clinic/views.py", line 15447, in final_generate
onLaterPages = partial(footer, content=footer_content)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/doctemplate.py", line 1291, in build
BaseDocTemplate.build(self, flowables, canvasmaker=canvasmaker)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/doctemplate.py", line 1056, in build
self.handle_flowable(flowables)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/doctemplate.py", line 912, in handle_flowable
if frame.add(f, canv, trySplit=self.allowSplitting):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/frames.py", line 174, in _add
w, h = flowable.wrap(aW, h)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/tables.py", line 1206, in wrap
self._calc(availWidth, availHeight)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/tables.py", line 641, in _calc
W = self._calcPreliminaryWidths(availWidth) # widths
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/tables.py", line 754, in _calcPreliminaryWidths
new = elementWidth(value, style) or 0
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/tables.py", line 518, in _elementWidth
w = v.minWidth() # should be all flowables
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/tables.py", line 873, in minWidth
style.leftPadding+style.rightPadding)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/tables.py", line 512, in _elementWidth
if hasattr(v, 'drawWidth') and isinstance(v.drawWidth, (int, float)): return v.drawWidth
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/flowables.py", line 494, in __getattr__
self._setup_inner()
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/flowables.py", line 455, in _setup_inner
img=self._img
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/platypus/flowables.py", line 488, in __getattr__
self._img=ImageReader(self._file)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 813, in __init__
annotateException('\nfileName=%r identity=%s' %
(fileName, self.identity()))
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 1394, in annotateException
rl_reraise(t, v, b)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 147, in rl_reraise
raise v
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 777, in __init__
self.fp=open_for_read(fileName, 'b')
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/reportlab/lib/utils.py", line 660, in open_for_read
raise IOError('Cannot open resource "%s"' % name)
OSError: Cannot open resource "b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xd2\x00\x00\x00\xd2\x01\x00\x00\x00\x00\x17\xe2\xa3\xef\x00\x00\x01$IDATx\x9c\xed\x98An\xc4 \x10\x04k\x16\xdf\xf1\x8f\xe0gyS~\x80\x9f\x92\x1f\xe0;\xab\xde\x03\xc6\xeb\x1c"\xe5d\xd0\xda\x1c\xd0 #\xcbRk4\x9e\xee\x01\xb6\xe5$\xa9 \xe5v\xc3\x83\xbf\xd7\xe7cA\x92\x94\xc1"N\x16k\x86\xa4\xd1x\x9e\x8bA\xc8#\xc8NJ\xbe e\'\xc0]</\x07\xccb\xdb\xfas\xe9\x8eM\xefP\xac\x13b\xed\xc6e0\xccKJ\x80\xd9\xecd\x11\x90T\xfap\x19\x06\xdb\x97\x13!;\xd5v\xd3\x87\xcbH\xd8\xa4=\x14\xeb\xd3\xc0\xb7\x9be$\x9e\x9d\xea\xa5V\x89/u\xab\xca\x94F\xe2yz^\x94\xbc\x04^\xda\x8ePe{,\x9e}\xeaE\xe9\xed\xe6\xe0\xae\x17\xa0\xa6#\xf9\xb2\x9b;\xe9\x1f\xdf}:\xc6A\x80v=\xbau\xba\xd5\xcb\xef_hk7#\xf1\xec\xee_\xf0\x92\x94\x9d.\xde_\xda<\xdd\xde\x19R\xb5\xbfW\xcf\xcb\x03V3\x8b\xb4\x911\xfc\x98\xd9\xd7\xe5\xfb\xcb\x11[f\'\x96\x19\x80\xa7\x8d\xcb\xf3\x0c\xec0O\x13\xbe\xa7b\xf8\x0c\x8b\xdd\xben\xef/\xc0\xf68\xb5E#\xf1\xec\xaaGU\xac\x9d\x08\xba\xba\xdf}\x01<\xf7\xbf\x8cN\xed-\x8a\x00\x00\x00\x00IEND\xaeB`\x82'"
fileName=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xd2\x00\x00\x00\xd2\x01\x00\x00\x00\x00\x17\xe2\xa3\xef\x00\x00\x01$IDATx\x9c\xed\x98An\xc4 \x10\x04k\x16\xdf\xf1\x8f\xe0gyS~\x80\x9f\x92\x1f\xe0;\xab\xde\x03\xc6\xeb\x1c"\xe5d\xd0\xda\x1c\xd0#\xcbRk4\x9e\xee\x01\xb6\xe5$\xa9 \xe5v\xc3\x83\xbf\xd7\xe7cA\x92\x94\xc1"N\x16k\x86\xa4\xd1x\x9e\x8bA\xc8#\xc8NJ\xbe e\'\xc0]</\x07\xccb\xdb\xfas\xe9\x8eM\xefP\xac\x13b\xed\xc6e0\xccKJ\x80\xd9\xecd\x11\x90T\xfap\x19\x06\xdb\x97\x13!;\xd5v\xd3\x87\xcbH\xd8\xa4=\x14\xeb\xd3\xc0\xb7\x9be$\x9e\x9d\xea\xa5V\x89/u\xab\xca\x94F\xe2yz^\x94\xbc\x04^\xda\x8ePe{,\x9e}\xeaE\xe9\xed\xe6\xe0\xae\x17\xa0\xa6#\xf9\xb2\x9b;\xe9\x1f\xdf}:\xc6A\x80v=\xbau\xba\xd5\xcb\xef_hk7#\xf1\xec\xee_\xf0\x92\x94\x9d.\xde_\xda<\xdd\xde\x19R\xb5\xbfW\xcf\xcb\x03V3\x8b\xb4\x911\xfc\x98\xd9\xd7\xe5\xfb\xcb\x11[f\'\x96\x19\x80\xa7\x8d\xcb\xf3\x0c\xec0O\x13\xbe\xa7b\xf8\x0c\x8b\xdd\xben\xef/\xc0\xf68\xb5E#\xf1\xec\xaaGU\xac\x9d\x08\xba\xba\xdf}\x01<\xf7\xbf\x8cN\xed-\x8a\x00\x00\x00\x00IEND\xaeB`\x82' identity=[ImageReader#0x7f1e0987ecf8 filename=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xd2\x00\x00\x00\xd2\x01\x00\x00\x00\x00\x17\xe2\xa3\xef\x00\x00\x01$IDATx\x9c\xed\x98An\xc4 \x10\x04k\x16\xdf\xf1\x8f\xe0gyS~\x80\x9f\x92\x1f\xe0;\xab\xde\x03\xc6\xeb\x1c"\xe5d\xd0\xda\x1c\xd0#\xcbRk4\x9e\xee\x01\xb6\xe5$\xa9 \xe5v\xc3\x83\xbf\xd7\xe7cA\x92\x94\xc1"N\x16k\x86\xa4\xd1x\x9e\x8bA\xc8#\xc8NJ\xbe e\'\xc0]</\x07\xccb\xdb\xfas\xe9\x8eM\xefP\xac\x13b\xed\xc6e0\xccKJ\x80\xd9\xecd\x11\x90T\xfap\x19\x06\xdb\x97\x13!;\xd5v\xd3\x87\xcbH\xd8\xa4=\x14\xeb\xd3\xc0\xb7\x9be$\x9e\x9d\xea\xa5V\x89/u\xab\xca\x94F\xe2yz^\x94\xbc\x04^\xda\x8ePe{,\x9e}\xeaE\xe9\xed\xe6\xe0\xae\x17\xa0\xa6#\xf9\xb2\x9b;\xe9\x1f\xdf}:\xc6A\x80v=\xbau\xba\xd5\xcb\xef_hk7#\xf1\xec\xee_\xf0\x92\x94\x9d.\xde_\xda<\xdd\xde\x19R\xb5\xbfW\xcf\xcb\x03V3\x8b\xb4\x911\xfc\x98\xd9\xd7\xe5\xfb\xcb\x11[f\'\x96\x19\x80\xa7\x8d\xcb\xf3\x0c\xec0O\x13\xbe\xa7b\xf8\x0c\x8b\xdd\xben\xef/\xc0\xf68\xb5E#\xf1\xec\xaaGU\xac\x9d\x08\xba\xba\xdf}\x01<\xf7\xbf\x8cN\xed-\x8a\x00\x00\x00\x00IEND\xaeB`\x82']
From the error, it appears that it is erroring out on getting the name of the image file. But there is no file. The image is being generated from BytesIO.
Your generate_qr_code function, which you did not show us, is NOT returning a BytesIO object. It's returning the raw bytes of the PNG image. When you print(img, type(img)), it told you it was of type "bytes", right? That's a string of bytes, not a BytesIO object. If you wrap those bytes into a BytesIO object, then the reportlab Image constructor will be able to handle it.

'S3File' object has no attribute 'forced'

Trying to append a parquet file in S3 using fastparquet lib, getting below error:
File "/Users/baluinfo/PycharmProjects/untitled/rough.py", line 55, in
write(parqKey, ws1 ,write_index=False,append=True,compression='GZIP', open_with=myopen)
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/fastparquet/writer.py", line 881, in write
compression, open_with, has_nulls, append)
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/fastparquet/writer.py", line 735, in write_simple
with open_with(fn, mode) as f:
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/fsspec/spec.py", line 775, in open
**kwargs
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/s3fs/core.py", line 378, in _open
autocommit=autocommit, requester_pays=requester_pays)
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/s3fs/core.py", line 1097, in init
cache_type=cache_type)
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/fsspec/spec.py", line 1062, in init
raise NotImplementedError("File mode not supported")
NotImplementedError: File mode not supported
Exception ignored in:
Traceback (most recent call last):
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/fsspec/spec.py", line 1343, in del
self.close()
File "/Users/baluinfo/PycharmProjects/Dash/venv/lib/python3.7/site-packages/fsspec/spec.py", line 1321, in close
if not self.forced:
AttributeError: 'S3File' object has no attribute 'forced'
aws_id = os.environ.get("AWS_ID")
aws_secret = os.environ.get("AWS_KEY")
bucket_name1 = 'eoddb/'
object_key = 'EOD.parquet'
s3 = s3fs.S3FileSystem(anon=False, key=aws_id, secret=aws_secret)
myopen = s3.open
parqKey = bucket_name1 + object_key
write(parqKey, ws1 ,write_index=False,append=True,compression='GZIP', open_with=myopen)

Categories

Resources