How to save image to local PC in Python?

Shakers

Junior Member
Joined
Jul 24, 2019
Messages
109
Reaction score
68
Hi there,
Please show me a way to save processed image to local PC HDD.
I am using Python in Google Colab. And my code works fine, transforming images. It ends with the following:

output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std, content_img, style_img, input_img) plt.figure() imshow(output, title='Output Image') plt.ioff() plt.show()
 
BTW, I tried
output.save("output.png", format="png")
it doesn't work...
 
you are already close, here is a working example:
Code:
from google.colab import files

#save any file on the colab workspace
plt.savefig("abc.png")

#then download the saved file to your local system
files.download("abc.png")

check out this example (but it's with a text file):
Sadly that doesn't work
It says
NameError: name 'files' is not defined
 
you probably forgot the import (first line)

Code:
from google.colab import files
 
clone_img = output.clone() from torchvision.utils import save_image clone_img.shape img1 = clone_img[0] save_image(img1, '/root/out.png') files.download("/root/out.png")
 
Thank you all guys for the help!
 
Option 1: Download the image directly

Right-click on the image in Colab. Boom, magic download options appear. Choose "Download to..." and pick your local folder. Easy-peasy.

Get fancy with Python. Use io.BytesIO() to save the image as a byte stream, then use requests to send it to your local machine. Think of it as chucking the image across the internet like a digital frisbee.

Option 2: Mount your local drive

Open the Files sidebar in Colab (click the file icon next to the "Runtime" menu). Look for "Mount Drive" and follow the instructions. This basically connects your local HDD to Colab like a USB drive, allowing you to save files directly.

Save the image with Python. Use cv2.imwrite() or PIL.Image.save() to write the image to your mounted drive. Now it's chilling on your local HDD, ready to party with your other cool files.
 
Back
Top