How do I encode multiple jpeg files in a folder as base 64. Currently my code can encode one file. However I would like this to encode all the jpeg files in a folder path.
with open("path\\A.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
extract_ocr_words(encoded_string)
In the above path there'll be multiple jpeg files such as
A.jpg
B.jpg
C.jpg
the final output should combine all the images in the folder and extract text and show as list in the final command
extract_ocr_words(encoded_string)
['ABC', 'LKM', 'GHI', 'TLI', 'CLI']
You have to iterate over the files inside the folder. You can use os
library ( link ):
import os
words = []
directory = 'path/to/your/pictures'
for filename in os.listdir(directory):
if filename.endswith(".jpg"):
with open(os.path.join(directory, filename), "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
words.extend(extract_ocr_Words(encoded_string))
else:
continue
print('Words from all files')
print(words)
Also you can use os.path.join()
( link to function ) function which concatenate two parts of path. It will works on Windows and Unix system. It is better than escaping of \\
. Then it is up to you what do you want to do with the encoded images.
You can still shorten your code by using glob
package. The functionality can be achieved as follows:
import glob
words = []
for f_name in glob.glob('*.jpg'):
with open(f_name,"rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
words = extract_ocr_Words(encoded_string)
words.extend(extract_ocr_Words(encoded_string))
else:
continue
print('Words from all files')
print(words)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.