How can use Image.open("image.png") on Google Colab? - python-3.x

I was trying to convert an image to text, I execute this code on Google Colab, I think, I have a problem with the path of a file, maybe, so, please help to get a solution to this code. How can I get a solution?
from PIL import Image
import pytesseract
# Open the temporary file using PIL
image = Image.open("image.png")
# Use the Tesseract OCR engine to extract the text
text = pytesseract.image_to_string(image, lang = 'eng', config='--psm 11')
# Print the extracted text
print(text)
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-63-86a0a5214b2a> in <module>
4
5 # Open the temporary file using PIL
----> 6 image = Image.open("image.png")
7
8 # Use the Tesseract OCR engine to extract the text
/usr/local/lib/python3.8/dist-packages/PIL/Image.py in open(fp, mode)
2841 # Abstract handlers.
2842
-> 2843
2844 class ImagePointHandler:
2845 """
FileNotFoundError: [Errno 2] No such file or directory: 'image.png'
I got this error when I execute. How can I get a solution to image = Image.open("image.png")?

Related

Unable to open an image through PIL in googlecolab

!wget https://github.com/lazyprogrammer/machine_learning_examples/blob/master/cnn_class/lena.png
from PIL import Image
im =Image.open('lena.png')
UnidentifiedImageError Traceback (most recent call last)
in ()
----> 1 im =Image.open('lena.png')
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2929 warnings.warn(message)
2930 raise UnidentifiedImageError(
-> 2931 "cannot identify image file %r" % (filename if filename else fp)
2932 )
2933
UnidentifiedImageError: cannot identify image file 'lena.png'
The link you are using to wget is all wrong. You are actually sending a GET request to the html file instead of the image.
Here's the actual image link for you :)
https://raw.githubusercontent.com/lazyprogrammer/machine_learning_examples/master/cnn_class/lena.png

Converting .wav audio files to .h5 (hdf) files using SciPy and PyTables

I need to convert audio .wav files to the .hf or the .npz format, as they are the supported format for training speech translation systems with FBK-Fairseq-ST (https://github.com/mattiadg/FBK-Fairseq-ST).
The following script is meant to run from terminal as python script.py /path/file.wav and write a new hdf file storing the information of the .wav file in the same folder.
from scipy.io import wavfile
import tables
import numpy
import sys
#read data from wav
#fs, data = wavfile.read('/home/vittoria/Documents/corpus-test/01.wav')
fs, data = wavfile.read(sys.argv[1])
#ouput
folder=sys.argv[1][:-6]
name= sys.argv[1][-6:-3]+"h5"
#save_to acoular h5 format
acoularh5 = tables.open_file(folder+name, mode = "w", title = name)
acoularh5.create_earray('/','time_data', atom=None, title='', filters=None, \
expectedrows=100000, chunkshape=[256,64], \
byteorder=None, createparents=False, obj=data)
acoularh5.set_node_attr('/time_data','sample_freq', fs)
acoularh5.close()
However, it raises a value error: ValueError: the shape ((0,)) and chunkshape ((256, 64)) ranks must be equal.
input from terminal:
python 2hf.py 01_83.wav" (python script.py relative-file-path)
Traceback error, please notice that in "environments/hdf/lib/python3.6/" "hdf" is the root folder of the virtual environment. "/tables/" is the folder for the package tables 3.6.1 (https://pypi.org/project/tables/) installed via the pip command in the virtual environment.
Traceback (most recent call last):
File "2hf.py", line 18, in <module>
byteorder=None, createparents=False, obj=data)
File "/home/giuseppe/environments/hdf/lib/python3.6/site-packages/tables/file.py", line 1384, in create_earray
track_times=track_times)
File "/home/giuseppe/environments/hdf/lib/python3.6/site-packages/tables/earray.py", line 160, in __init__
track_times)
File "/home/giuseppe/environments/hdf/lib/python3.6/site-packages/tables/carray.py", line 212, in __init__
(shape, chunkshape))
ValueError: the shape ((0,)) and chunkshape ((256, 64)) ranks must be equal.
Closing remaining open files:01_83.h5...done
I had the same error and solved it by changing the script this way
from scipy.io import wavfile
import tables
import numpy
import sys
#read data from wav
#fs, data = wavfile.read('/home/vittoria/Documents/corpus-test/01.wav')
fs, data = wavfile.read(sys.argv[1])
#ouput
folder=sys.argv[1][:-6]
name= sys.argv[1][-6:-3]+"h5"
#save_to acoular h5 format
acoularh5 = tables.open_file(folder+name, mode = "w", title = name)
acoularh5.create_earray('/','time_data', atom=None, title='', filters=None, \
expectedrows=100000, \
byteorder=None, createparents=False, obj=data)
acoularh5.set_node_attr('/time_data','sample_freq', fs)
acoularh5.close()
I basically just removed this part , chunkshape=[256,64] :-)
Hope this helped.

PIL.UnidentifiedImageError: cannot identify image file

I'm working on GCP cloud functions and intend to write a functions which combines two images. But I', getting the following error when I invoke the function:
Traceback (most recent call last): File
"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",
line 346, in run_http_function result =
_function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",
line 217, in invoke_user_function return
call_user_function(request_or_event) File
"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",
line 210, in call_user_function return
self._user_function(request_or_event) File "/user_code/main.py", line
74, in execute newIntro= generateIntroImage(nameMappings['stdName'],
nameMappings['stdPicture'], nameMappings['logo'],
nameMappings['stdYear'], nameMappings['font']) File
"/user_code/main.py", line 12, in generateIntroImage
images.append(Image.open(logo)) File
"/env/local/lib/python3.7/site-packages/PIL/Image.py", line 2862, in
open "cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file '/tmp/logo.jpg'
I have ran this function on my local machine and it works as expected but when I deploy it on GCP, it gives this error and crashes. Here's my function:
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def generateIntroImage(stdName, stdPicture, logo, year, typeFace):
images = [Image.open(x) for x in [stdPicture, logo]]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
font= ImageFont.truetype(typeFace, 70)
draw= ImageDraw.Draw(new_im)
draw.text((0, 0), stdName+"'s " +year+" Year Book", (0,0,0),font= font)
fileName= "/tmp/test.jpg"
new_im.save(fileName)
return fileName
These images are .jpg and .png files. Any idea what could be wrong?
Happened to me on Google Colab as well, apparently updating PIL version fixed the problem for me.
PIL throws error because it cannot identify the image format. Most probably the reason is that the image is corrupted and hence cannot be read (or "identified") by pillow's Image.open(). For example if you try opening the image in an IPython prompt, it would fail as well.
In [2]: from PIL import Image
In [3]: Image.open("176678612.jpg")
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-3-3f91b2f4e49a> in <module>
----> 1 Image.open("176678612.jpg")
/opt/conda/envs/swin/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode, formats)
3022 warnings.warn(message)
3023 raise UnidentifiedImageError(
-> 3024 "cannot identify image file %r" % (filename if filename else fp)
3025 )
3026
UnidentifiedImageError: cannot identify image file '176678612.jpg'
And the relevant piece of code handling this check is from PIL.Image.open()
"""
exception PIL.UnidentifiedImageError: If the image cannot be opened and
identified.
"""
raise UnidentifiedImageError(
"cannot identify image file %r" % (filename if filename else fp)
So, the fix is to delete the image, or replace it with an uncorrupted version.
you need to provide a downloadable link to the image. What worked for me was click on the download image and the copy that URL.

Specify where to save image taken with webcame Python

So I have a Python application that accesses the built-in webcam on a laptop and takes a picture. But I'm having difficulty specifying the storage location for the picture (in this case on the desktop). The code I have so far is:
import cv2
import time
import getpass
import os
getUser = getpass.getuser()
save = 'C:/Users/' + getUser + "/Desktop"
camera_port = 0
camera = cv2.VideoCapture(camera_port)
time.sleep(0.1)
return_value, image = camera.read()
os.path.join(cv2.imwrite(save, "user.png", image))
del camera
But when I run it I get the following error:
Traceback (most recent call last):
File "C:/Users/RedCode/PycharmProjects/MyApps/WebcamPic.py", line 13, in <module>
os.path.join(cv2.imwrite(save, "user.png", image))
TypeError: img is not a numpy array, neither a scalar
How can I specify where to store the image when it is taken?
This line here is where you have a problem.
os.path.join(cv2.imwrite(save, "user.png", image))
You want to do this
cv2.imwrite(os.path.join(save, "user.png"), image)
imwrite expects two arguments the file name and the image to be saved.
The call to os.path.join is building your saved file path.

Writing pdf with pypdf2 gives error

I'm trying to write a simple script to merge two PDFs but have run into an issue when trying to save the output to disk. My code is
from PyPDF2 import PdfFileWriter, PdfFileReader
import tkinter as tk
from tkinter import filedialog
### Prompt the user for the 2 files to use via GUI ###
root = tk.Tk()
root.update()
file_path1 = tk.filedialog.askopenfilename(
filetypes=[("PDF files", "*.pdf")],
)
file_path2 = tk.filedialog.askopenfilename(
filetypes=[("PDF files", "*.pdf")],
)
###Function to combine PDFs###
output = PdfFileWriter()
def append_pdf_2_output(file_handler):
for page in range(file_handler.numPages):
output.addPage(file_handler.getPage(page))
#Actually combine the 2 PDFs###
append_pdf_2_output(PdfFileReader(open(file_path1, "rb")))
append_pdf_2_output(PdfFileReader(open(file_path2, "rb")))
###Prompt the user for the file save###
output_name = tk.filedialog.asksaveasfile(
defaultextension='pdf')
###Write the output to disk###
output.write(output_name)
output.close
The problem is that I get an error of
UserWarning: File to write to is not in binary mode. It may not be written to correctly. [pdf.py:453] Traceback (most recent call last): File "Combine2Pdfs.py", line 44, in output.write(output_name) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho‌​n3.5/site-packages/P‌​yPDF2/pdf.py", line 487, in write stream.write(self.header + b("\n")) TypeError: write() argument must be str, not bytes
Where have I gone wrong?
I got it by adding mode = 'wb' to tk.filedialog.asksaveasfile. Now it's
output_name = tk.filedialog.asksaveasfile(
mode = 'wb',
defaultextension='pdf')
output.write(output_name)
Try to use tk.filedialog.asksaveasfilename instead of tk.filedialog.asksaveasfile. You just want the filename, not the file handler itself.
###Prompt the user for the file save###
output_name = tk.filedialog.asksaveasfilename(defaultextension='pdf')

Resources