简体   繁体   中英

opening a JPG file in Google Colab

I have this code in to open a PNG file on google colab.

import numpy as np
import matplotlib
from PIL import Image
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('https://www.seekpng.com/png/full/161-1615982_doc-back-to-the- 
future-png-doc-back.png')

imgplot = plt.imshow(img)

The problem is that this code only works for PNG files. I have trouble opening a JPG file, and a GIF file in Google Colab. I would like to open a picture with its image address from the web, just like the one from the previous code. I know you can open JPG files with this code.

from PIL import Image                                                                                
img = Image.open("https://upload.wikimedia.org/wikipedia/commons/c/c2/Girl_in_front_of_a_green_background.jpg")
img.show() 

but it gives me the following error when compiling. FileNotFoundError: [Errno 2] No such file or directory:

check out this Q&A

a summary:

you need to retrieve the jpg or any kind of file save and then read it from it's path for the Image.open() function

using requests you can get

this might be useful too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM