Search code examples
pythonimage-processingjpegpython-imaging-librarygoogle-cloud-vision

Python - Convert TIFF, PDF etc. to JPEG in-memory for input to Google Cloud Vision


I have a number of non-JPEG image files that I want to process using Google Cloud Vision, but the API only accepts certain formats (see question Cloud Vision API - PDF OCR and answer https://cloud.google.com/vision/docs/supported-files).

I can use PIL or some such to convert a TIFF to JPEG to be uploaded, but I'd like to avoid a temp file if possible.

So, in python, how do I convert a TIFF in-memory for upload to GCV? numpy array, base64, string..?


Solution

  • As suggested, you should use StringIO or BytesIO for Python 3.

    See this question: Python resize image and send to google vision function

    Example making a request string:

    output = BytesIO()
    image_object.save(output, 'JPEG', quality=90)
    contents = output.getvalue()
    output.close()
    ctxt = b64encode(contents).decode()
    
    request_string = [{
        'image': {'content': ctxt},
        'features': [{
            'type': 'TEXT_DETECTION',
            'maxResults': 1
            }]
    }]