0

I have an image file decoded by base64. Now, I want to save the image file to the specified directory.

the directory is described as image_dir_path

image_dir_path = '/images/store/'
image_file = base64.b64decode(image_file)

How can I save the image_file to image_dir_path?

I tried shutil.copy(image_file, image_dir_path), but it doesn't work for my case.

I'm sorry I coundn't find the question like this.

2
  • Have you tried using Pillow or PIL? Commented Jul 22, 2015 at 8:51
  • I tried to use PIL, but unfortunately could not find any ways to save the image on specific directory... Commented Jul 22, 2015 at 9:05

1 Answer 1

1

You can write any content to a file with a file object and its write method. As an example, let's grab some base64 encoded data from the web:

import base64, urllib

decoded = base64.b64decode(urllib.urlopen("http://git.io/vYT4p").read())    
with open('/tmp/31558315.png', 'w') as handle:
    handle.write(decoded)

You should be able to open the file under /tmp/31558315.png as a regular image.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! It worked I got [Errno 21] Is a directory: 'images/store' though.
@ReiKudo, Error 21 means is a directory. You seemed to write something, but it fails, because the target is a directory, which is not expected at this point.
I got it wrong totally. Error occurred since I tried to open directory. Thank you @miku.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.