Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_nolangs(self):
"""
Issue #51: Running OCR without any language installed causes a SIGSEGV.
"""
tessdata_prefix = os.getenv("TESSDATA_PREFIX", "")
os.environ['TESSDATA_PREFIX'] = '/opt/tulipe'
try:
with self.assertRaises(PyocrException):
self.tool.image_to_string(
PIL.Image.open(self._path_to_img('test-japanese.jpg')),
lang='fra'
)
finally:
if tessdata_prefix == "":
os.environ['TESSDATA_PREFIX'] = ""
os.unsetenv("TESSDATA_PREFIX")
else:
os.environ['TESSDATA_PREFIX'] = tessdata_prefix
def test_nolangs2(self):
with self.assertRaises(PyocrException):
self.tool.image_to_string(
PIL.Image.open(self._path_to_img('test-japanese.jpg')),
lang='doesnotexist'
)
def image2txt_pyocr(self, image, do_orientation):
txt = ""
orientation = ""
img_per_page = PI.open(io.BytesIO(image))
if do_orientation is True:
try:
if self.tool.can_detect_orientation():
orientation = self.tool.detect_orientation(img_per_page, lang=self.lang)
angle = orientation["angle"]
if angle != 0:
img_per_page.rotate(orientation["angle"])
except pyocr.PyocrException as exc:
print("Orientation detection failed: {}".format(exc))
print("Orientation: {}".format(orientation))
try:
txt = self.tool.image_to_string(
img_per_page, lang=self.lang,
builder=pyocr.builders.TextBuilder()
)
except pyocr.error.TesseractError as e:
print("{}".format(e))
return txt