You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For some reason, Azure often does not work and returns 400 Bad requests with message The file submitted couldn't be parsed. This can be due to one of the following reasons: the file format is not supported ( Supported formats include JPEG, PNG, BMP, PDF and TIFF), the file is corrupted or password protected.". This was reported in several libraries using the same API (e.g. here and here).
The way around it seems to upload the image and use ComputerVisionClient.read instead of ComputerVisionClient.read_in_stream. Here is description how to upload image to Google drive and get a shareable link to the image (we can delete it afterwards):
Now, you can use the following script to perform the desired operations:
importpickleimportos.pathfromgoogle_auth_oauthlib.flowimportInstalledAppFlowfromgoogleapiclient.discoveryimportbuildfromgoogleapiclient.errorsimportHttpErrorfromgoogle.oauth2.credentialsimportCredentials# Setup the Drive v3 APISCOPES= ['https://www.googleapis.com/auth/drive']
creds=None# Check if token existsifos.path.exists('token.pickle'):
withopen('token.pickle', 'rb') astoken:
creds=pickle.load(token)
# If there are no valid credentials available, prompt the user to log inifnotcredsornotcreds.valid:
ifcredsandcreds.expiredandcreds.refresh_token:
creds.refresh(Request())
else:
flow=InstalledAppFlow.from_client_secrets_file('path_to_your_downloaded_credentials.json', SCOPES)
creds=flow.run_local_server(port=0)
# Save the credentials for the next runwithopen('token.pickle', 'wb') astoken:
pickle.dump(creds, token)
service=build('drive', 'v3', credentials=creds)
# Upload filefile_metadata= {
'name': 'Your_PDF_Name.pdf'
}
media=MediaFileUpload('path_to_your_pdf_file.pdf', mimetype='application/pdf')
file=service.files().create(body=file_metadata, media_body=media, fields='id').execute()
file_id=file.get('id')
# Share the file and get shareable linkdefget_shareable_link(file_id):
permissions= {
'role': 'reader',
'type': 'anyone'
}
service.permissions().create(fileId=file_id, body=permissions).execute()
returnf"https://drive.google.com/uc?export=download&id={file_id}"print("Shared Link:", get_shareable_link(file_id))
# Delete the fileinput("Press Enter to delete the file...")
service.files().delete(fileId=file_id).execute()
print("File Deleted!")
Note: Replace 'path_to_your_downloaded_credentials.json' with the path to your downloaded JSON file from the Google Cloud Console, and 'path_to_your_pdf_file.pdf' with the path to your actual PDF file.
Also, be careful with sharing files using the 'type': 'anyone' setting as it makes the file publicly accessible.
Remember to place your credentials.json in the same directory as the script or adjust the path accordingly.
The text was updated successfully, but these errors were encountered:
fromioimportBytesIOimg_file=BytesIO()
# quality='keep' is a Pillow setting that maintains the quantization of the image.# Not having the same quantization can result in different sizes between the in-memory image and the file size on disk.image.save(img_file, 'png', quality='keep')
image_file_size=img_file.tell()
For some reason, Azure often does not work and returns
400 Bad requests
with messageThe file submitted couldn't be parsed. This can be due to one of the following reasons: the file format is not supported ( Supported formats include JPEG, PNG, BMP, PDF and TIFF), the file is corrupted or password protected."
. This was reported in several libraries using the same API (e.g. here and here).The way around it seems to upload the image and use
ComputerVisionClient.read
instead ofComputerVisionClient.read_in_stream
. Here is description how to upload image to Google drive and get a shareable link to the image (we can delete it afterwards):Google Cloud Project Setup:
APIs & Services
>Library
.Drive API
and enable it.Credentials
tab and click onCreate Credentials
. ChooseOAuth 2.0 Client ID
.Desktop App
and create the credentials.Install the necessary Python libraries:
Now, you can use the following script to perform the desired operations:
Note: Replace 'path_to_your_downloaded_credentials.json' with the path to your downloaded JSON file from the Google Cloud Console, and 'path_to_your_pdf_file.pdf' with the path to your actual PDF file.
Also, be careful with sharing files using the
'type': 'anyone'
setting as it makes the file publicly accessible.Remember to place your
credentials.json
in the same directory as the script or adjust the path accordingly.The text was updated successfully, but these errors were encountered: