I have seen so many different threads about this topic but none of their solutions seems to work for me. I've tried several ways of reading an image from my Drive into Colab using its URL, with no success. I want to read it using its URL rather than mounting my Drive and using directories because multiple people share this Colab, and their directory to the image might not be the same as mine.
The first attempt comes from a popular thread on this issue: How do I read image data from a URL in Python?
from PIL import Image
import requests
url = 'https://drive.google.com/file/d/1z33YPsoMe0lSNNa2XWa0tiK2571j2tFu/view?usp=sharing'
im = Image.open(requests.get(url).raw) # apparently no need for bytes wrapping in new Python versions
im = Image.open(requests.get(url, stream=True).raw) # also does not work
The error I got was UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f0189569770>
Then I tried:
from skimage import io
io.imshow(io.imread(url))
Which returned ValueError: Could not find a format to read the specified file in mode 'i'. Feeling very lost because all these approaches seem to work for everyone else. Would appreciate any feedback.
Using gdown to read an image from Google Drive into Colab.
If you have your image in your Google Drive and you are using colab.research.google.com, then you can follow these steps:
pip install gdown
Obtaining the share link from your image (remember to set the option "Share to anyone with the link"). For an example:
# https://drive.google.com/file/d/1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ/view?usp=sharing
Extract the id of the share link from the URL 1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ.
Download the image in the folder content of a particular user who has access to this Colab:
!gdown --id '1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ' --output bird1.jpp
Read the image file from the folder content
from PIL import Image
im = Image.open("../content/bird1.jpg")
im
Related
I have a folder of images I extracted for a computer-vision project, but don't know how to mount it onto Google Colab and iterate through the folder. I have a function I need to apply to each image inside the folder, but don't know how to get the images.
I've tried looking for resources but haven't found anything helpful to my situation, because most of them were for unzipping files that were not images. Can you please help me out? Thank you.
You can use the OpenCV library for this.
from google.colab import drive
import os
import cv2
First you need to change the current working directory to your image folder directory.
os.chdir("/content/drive/MyDrive/yourfolder")
You can iterate through every image, apply your function to them and save the final version like this:
for file in os.listdir():
img = cv2.imread(file)
result = myfunction(img)
cv2.imwrite(file, result)
I have one website which has search button and i need to give some numeric value and give enter button. It will go to another page and it display some content in which there are some URL, if i click that URL, it will ask to save diagram and the diagram is either tiff format or PDF.
To download Tiff format diagram, i am using swift plugin in internet explore and save to my machine
Here i am doing this work manually, just i want to do automate this whole process.
Steps:
Using python request module and pass the URL with numeric value to post method
save response content to variable
perform pattern matching and fetch url
click the url but i am stuck with this part to save the diagram local since it is tiff.
is there any module to download tiff based diagram and save to local machine?
Just I want to share How i resolved the issue for the above question and it might be useful for others.
Since tiff image needs to be downloaded from web, so I used python request module with pillow module as below,
from PIL import image
import requests
tiffURL='https://***.tif'
img=Image.open(requests.get(tiffURL,stream=True).raw)
img.save('imagename.jpg')
#img.save('imagename.jpg',quality=95)
Note:
tiff image can not be viewed by normal editor , so i converted to jpg
if you want high resoultion, you can pass quality=95 to save method
So I understand how I can download images from the web, in which they have an 'src' but now I am trying to access images that I have already downloaded on my computer, making changes to it, and then downloading it to my computer again.
from PIL import Image
redish = Image.open('gradient_red.jpg')
check_pics = open('pictures','wb')
check_pics.write(redish)
check_pics.close()
I tried doing the same thing as one would download images with an 'src', but it does not work because the object is not a 'byte-like object'
So I just found out that PIL library has a method called 'save'. I just had to do this.
from PIL import Image
redish = Image.open('gradient_red.jpg')
redish.save('test.jpg')
Thank you all for helping
I'm trying to convert pdf to images using pdf2image but getting problem of extra generated boxes.
This is my input pdf file screenshot
this in input file
from pdf2image import convert_from_path
images = convert_from_path('input_pdf.pdf',output_folder=r'C:\Users\Baith')
images[0].save('output.jpg')
after executing above code got this output
output_file
Since pdf2image is only a thin wrapper around pdftoppm, itself part of poppler, I would advise trying different parameters with the CLI tools to see it a specific combination works.
As for pdf2image itself, you might want to try use_cropbox=True and see if it still add lines.
Feel free to open an issue directly of the repository, if you can provide a sample PDF I would be happy to assist with the issue.
I have a store of images in Google Cloud Storage and I am looking to read them into OpenCV in Datalab. I can find information on how to read text files but can't find anthing on how I can read in an image. How would I go about doing this?
I am not really familiar with OpenCV, so let me cover the Datalab ⟷ GCS part and I hope that is enough for you to go on with the OpenCV part.
In Datalab, you can use two different approaches to access Google Cloud Storage resources. They are both documented (with working examples) in these Jupyter notebooks: access GCS using Storage commands ( %%gcs ) or access GCS using Storage APIs ( google.datalab.storage ).
I'll provide an example using Storage commands, but feel free to adapt it to the Datalab GCS Python library if you prefer.
# Imports
from google.datalab import Context
from IPython.display import Image
# Define the bucket and and an example image to read
bucket_path = "gs://BUCKET_NAME"
bucket_object = bucket_path + "/google.png"
# List all the objects in your bucket, and read the example image file
%gcs list --objects $bucket_path
%gcs read --object $bucket_object -v img
# Print the image content (see it is in PNG format) and show it
print(type(img))
img
Image(img)
Using the piece of code I shared, you are able to perform a simple object-listing for all the objects in your bucket and also read an example PNG image. Having its content stored in a Python variable, I hope you are able to consume it in OpenCV.