1

I am trying to create an image thumbnail creation function using python, running in a google cloud platform's function. The image is sent as a base64 string to the cloud function, manipulated and made smaller with Python's Pillow package. It is then uploaded as an image, going from a Pillow Image object, to a BytesIO object, then saved to google cloud storage. This is all done successfully.

The problem here is very strange: Google Cloud Storage does not recognize the image until an access token is created manually. Otherwise, the image is left in an infinite loop, never loading, and never being able to be used.

I have reviewed this SO post, which has a very similar problem to mine (the image here shows exactly my problem: an uploaded file cannot be loaded properly), but it differs in two imporant categories: 1) They are manipulating the image array directly, while my code never touches it and 2) they are working in Node.js, where the Firebase SDK is different than in Python.

The code to generate the image is as follows:

def thumbnailCreator(request):
    # Setting up the resourcse we are going to use
    storage_client = storage.Client()
    stor_bucket = storage_client.bucket(BUCKET_LINK)

    # Retriving the Data
    sent_data = request.get_json()['data']
    name = sent_data['name']
    userID = sent_data['userID']

    # Process to go between base64 string to bytes, to a file object 
    imageString = stor_bucket.blob(PATH_TO_FULL_SIZE_IMAGE).download_as_string()
    
    imageFile = BytesIO(imageString)
    image = Image.open(imageFile)

    # Resizing the image is the goal 
    image = image.resize(THUMBNAIL_SIZE)

    # Go between pillow Image object to a file
    imageFile = BytesIO()
    image.save(imageFile, format='PNG')
    imageBytes = imageFile.getvalue()
    image64 = base64.b64encode(imageBytes)
    imageFile.seek(0)


    # Uploading the Data 
    other_blob = stor_bucket.blob(PATH_FOR_THUMBNAIL_IMAGE)
    other_blob.upload_from_file(imageFile, content_type = 'image/png')

    return {'data': {'response': 'ok', 'status': 200}}

Again, this works. I have a feeling there is something wrong with the MIME type. I am a novice when it comes to this type of programming/networking/image manipulation, so I'm always looking for a better way to do this. Anyway, thanks for any and all help.

et-hicks
  • 21
  • 4
  • You seem to not use the `image64` variable, you may want to delete it as it probably takes resources from your CloudFunction – Juancki Feb 10 '21 at 09:15
  • Have you downloaded the file to your machine and inspected what is there? – Juancki Feb 10 '21 at 09:18
  • You are also dowloading the image as a string, it seems proper to use `download_as_bytes` – Juancki Feb 10 '21 at 09:22
  • 1
    I have deleted the image64 variable. The image on the machine works as intended. I have tried download_as_bytes(), but doing so makes the downloaded image not work on the machine. However, I appear to be wrong that there is an error here. The file is not loaded until a access token is made, but it doesn't seem to cause an error on the client side. That is, an access token does not have to be made manually for this to work, it can be made via programming and still generate a valid png. Thank you for looking at this, you did help by catching the image64 waste. – et-hicks Feb 11 '21 at 22:53

1 Answers1

1

It appears that the premise of this question - that a access token must be made manually for the image to work - is not accurate. After further testing, the error came from other parts of the code base I was working in. The above python script does work for image manipulation. An access token to the image can be generated via code, and be provided client-side.

Leaving this up in case someone stumbles upon it in the future when they need to work with Pillow/PIL in the Google Cloud Platform.

et-hicks
  • 21
  • 4
  • Thank you for posting the solution, consider marking it as correct! https://stackoverflow.com/help/self-answer – Juancki Feb 12 '21 at 08:40