user615525
user615525

Reputation: 353

Need Help Appending Separate Lines of Text Retrieved From Image in Python

I am writing a Program that used Microsoft Computer Vision to read text from images. The line of text is then stored in a string to be used later.

I have accomplished that part of my project, which works fine if my image only has a single line of text.

However SOMETIMES the text on the image is in multiple, separate lines - This is where I run into trouble.

When there is one single line of text, my code works fine, BUT when the image has multiple lines of text, the string I am trying to store only gets the final line read from the image.

Here's my code:

if read_result.status == OperationStatusCodes.succeeded:
                  for text_result in read_result.analyze_result.read_results:
                      for line in text_result.lines:
                        line_str = line.text
                        print(f"Line string: ",line_str)  
                            
                      upload_title_str = line_str
                      print(f" Upload Title String: {(upload_title_str)}")

The Output is: Here are my printed output lines

I want all the lines to be on a SINGLE Line and then my upload_title_str to be - line_str1+line_str2+line_str3

I've tried different .joins and appends, += operator etc.

I think I am missing something basic, but after staring at this for a couple hours it's just not coming to me.

Any suggestions?

Upvotes: 1

Views: 46

Answers (1)

user615525
user615525

Reputation: 353

I was able to solve this by:

for line in text_result.lines:
                        line_str = line_str + line.text

And then moving the 'level' at which I printed out my upload_title_str to my outer loop indentation level.

Sometimes you just need to look away for a bit to see something obvious!

Upvotes: 0

Related Questions