Read an image using url imageio python - python-3.5

Can anyone tell how to read an image using an URL given, with the help of imageio python3?
I have already looked at this stackoverflow question but is it possible to use imageio instead of PIL.

Related

PyQt5 - Load JPEG-compressed image to QImage

I have an application that displays images, using QtGui.QImage. To save space, I changed the GeoTiff compression from LZW to JPEG, but now I get the following error:
foo: JPEG compression support is not configured.
foo: Sorry, requested compression method is not configured.
I have not found anything how I can configure PyQt to understand that type of compression. Do I need a specific build or can I set it somewhere?
Using Python 3.10 with PyQt5.15
Thanks to the comment of #musicamante, the issue could be solved simply by using:
from PIL.ImageQt import ImageQt
my_q_image = ImageQt(image_path)
Then, my_q_image acts exactly like a QImage.
Important reminder though, which I found while investigating this: PyQt5 support from PIL ends in July 2023!

What are Python3 libraries which replace "from scikits.audiolab import Format, Sndfile"

Hope you'll are doing good. I am new to python. I am trying to use audio.scikits library in python3 verion. I have a working code version in 2.7(with audio.scikits) . While I am running with python3 version I am getting the Import Error: No Module Named 'Version' error. I get to know that python3 is not anymore supporting audio.scikits(If I am not wrong). Can anyone suggest me replacing library for audio.scikits where I can use all the functionalities like audio.scikits do OR any other solution which might helps me. Thanks in advance.
2.7 Version Code :
from scikits.audiolab import Format, Sndfile
from scipy.signal import firwin, lfilter
array = np.array(all)
fmt = Format('flac', 'pcm16')
nchannels = 1
cd, FileNameTmp = mkstemp('TmpSpeechFile.wav')
# making the file .flac
afile = Sndfile(FileNameTmp, 'w', fmt, nchannels, RawRate)
#writing in the file
afile.write_frames(array)
SendSpeech(FileNameTmp)
To check entire code please visit :Google Asterisk Reference Code(modifying based on this code)
I want to modify this code with python3 supported libraries. Here I am doing this for Asterisk-Microsoft-Speech To Text SDK.
Firstly the link code you paste is Asterisk-Google-Speech-Recognition, it's not the Microsoft-Speech-To-Text, if you want get a sample about Microsoft-Speech-To-Text you could refer to the official doc:Recognize speech from an audio file.
And about your problem you said, yes it's not completely compatible, in the github issue there is a solution for it, you could refer to this comment.

Image type Python: loaded a jpg, showing a png

I have been playing around with images in Python, just trying to understand how things work basically. I have noticed something odd and was wondering if anyone else could explain it.
I have an image 'duck.jpg' -
If I look at the properties I can see that it is a jpg image.
However, after importing into python using the follwoing convoluted way:
from PIL import Image
import io
with open('duck.jpg', 'rb') as f:
im = Image.open(io.BytesIO(f.read()))
f.close()
I get the following output after calling
im.format
'PNG'
Is there some sort of automatic conversion going on?

Is there any way I can save the plot as .jpg [duplicate]

I am using matplotlib (within pylab) to display figures. And I want to save them in .jpg format. When I simply use the savefig command with jpg extension this returns :
ValueError: Format "jpg" is not supported.
Supported formats: emf, eps, pdf, png, ps, raw, rgba, svg, svgz.
Is there a way to perform this ?
You can save an image as 'png' and use the python imaging library (PIL) to convert this file to 'jpg':
import Image
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.savefig('testplot.png')
Image.open('testplot.png').save('testplot.jpg','JPEG')
The original:
The JPEG image:
To clarify and update #neo useful answer and the original question. A clean solution consists of installing Pillow, which is an updated version of the Python Imaging Library (PIL). This is done using
pip install pillow
Once Pillow is installed, the standard Matplotlib commands
import matplotlib.pyplot as plt
plt.plot([1, 2])
plt.savefig('image.jpg')
will save the figure into a JPEG file and will not generate a ValueError any more.
Contrary to #amillerrhodes answer, as of Matplotlib 3.1, JPEG files are still not supported. If I remove the Pillow package I still receive a ValueError about an unsupported file type.
Just install pillow with pip install pillow and it will work.
I just updated matplotlib to 1.1.0 on my system and it now allows me to save to jpg with savefig.
To upgrade to matplotlib 1.1.0 with pip, use this command:
pip install -U 'http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.1.0/matplotlib-1.1.0.tar.gz/download'
EDIT (to respond to comment):
pylab is simply an aggregation of the matplotlib.pyplot and numpy namespaces (as well as a few others) jinto a single namespace.
On my system, pylab is just this:
from matplotlib.pylab import *
import matplotlib.pylab
__doc__ = matplotlib.pylab.__doc__
You can see that pylab is just another namespace in your matplotlib installation. Therefore, it doesn't matter whether or not you import it with pylab or with matplotlib.pyplot.
If you are still running into problem, then I'm guessing the macosx backend doesn't support saving plots to jpg. You could try using a different backend. See here for more information.
Matplotlib can handle directly and transparently jpg if you have installed PIL. You don't need to call it, it will do it by itself. If Python cannot find PIL, it will raise an error.
I'm not sure about all versions of Matplotlib, but in the official documentation for v3.5.0 savfig allows you to pass settings through to the underlying Pillow library which anyway does the image saving. So if you want a jpg with specific compression settings for example:
import matplotlib.pyplot as plt
plt.plot(...) # Plot stuff
plt.savefig('filename.jpg', pil_kwargs={
'quality': 20,
'subsampling': 10
})
This should give you a highly compressed jpg as the output.
Just for completeness, if you also want to control the quality (i.e. compression level) of the saved result, it seems to get a bit more complicated, as directly passing plt.savefig(..., quality=5) does not seem to have an effect on the output size and quality. So, on the one hand, one could either go the way of saving the result as a png first, then reloading it with PIL, then saving it again as a jpeg, using PIL's quality parameter – similar to what is suggested in Yann's answer.
On the other hand, one can avoid this deviation of loading and saving, by using BytesIO (following the answer to this question):
from io import BytesIO
import matplotlib.pyplot as plt
from PIL import Image
buf = BytesIO()
plt.plot(...) # Plot something here
plt.savefig(buf)
Image.open(buf).convert("RGB").save("testplot.jpg", quality=5)

Getting error while running a classification code in keras

When I run the code from the following link:
https://gist.github.com/fchollet/f35fbc80e066a49d65f1688a7e99f069#file-classifier_from_little_data_script_2-py
I get the following error:
Using TensorFlow backend. Found 2000 images belonging to 2 classes.
/home/nd/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:692:
UserWarning: Possibly corrupt EXIF data. Expecting to read 80000 bytes
but only got 0. Skipping tag 64640 "Skipping tag %s" % (size,
len(data), tag))
I am Using Ubuntu.
Tried Solution : change 'w' to 'wb' in line 70 and 81.
Thnx in advance
This is because some of the images have corrupted exif info. You can just remove the exif info of all your images to remove this warning.
The python package piexif can help you. you can use the following code to remove the exif info of an image:
import piexif
# suppose im_path is a valid image path
piexif.remove(im_path)
You can find more discussion here.
The error seems to imply that you try to use TIFF images (rather than JPEGs) and that the PIL library can´t import these without an error (Possibly corrupt EXIF data).
I suggest you try some test JPEGs to make sure your images can be imported correctly.

Resources