Reading png does not give expected result - python

import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
img = misc.imread('W0002_0001.png')
plt.figure()
plt.imshow(img)
plt.show()
I want to read the first image that can be found here. The problem is that the image displayed is not the one I should see. I get:
When I should be getting:
When looking in detail, I noticed that the real image could be seen in the first row of the obtained image, repeated several times.
Also, I had several segfaults when trying to import the image several times in a row with IPython, although it seems like I cannot always reproduce the issue.
I tried reading the image with Pillow and it worked fine.
Do you have any idea what might cause that?

Related

Matplotlib plot lines are choppy

I have some plots generated by matplotlib, which I have been generating using the same code (for different data) for several years. The plots always come out looking normal. This time, I re-ran the same code using different data, and the plots showed up like
(Note that the missing border at the top is the result of my erasing the legend. The problem is the choppy, broken up blue and orange data lines.) Many of the plots generated are broken up like this, but not all are. All were normal the previous times that I ran this code.
I have no idea what's going on to make the lines all broken up like this. I tried re-starting my computer and it didn't help. I can't seem to find anything online about this issue. Does anyone have any idea what's going on here?
Here's the code that generated it (excerpted)
`
import matplotlib
from numpy import NaN
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from PyPDF2 import PdfFileMerger, PdfFileReader
for chan in channels:
dProd.plot(x='Date',y=[a_chan,b_chan],secondary_y=b_chan)
plt.title(chan).get_figure().savefig(folder+'\\'+prod+'\\plots\\'+chan+'.pdf')
plt.close('all')
`

How to copy an image using a URL and paste into Excel (through xlwings) without first downloading the image?

Apologies if this has been asked somewhere before, but I couldn't find a good answer. I'm trying to take an image URL obtained from scraping a website and use it to paste the image into an Excel worksheet without saving the image somewhere first. I guess this would be equivalent to right-clicking the image, copying, then pasting into Excel if someone were to try this manually.
Right now, I'm using skimage to get the image to pop up:
from skimage import io
io.imshow(io.imread('urlhere.com'))
io.show()
However, I don't think there is a way to work with the image like this and paste it into Excel using xlwings. I've seen people mention things like urllib.request.urlretrieve and PIL but these only seem to work if I want to save the image somewhere first and then bring it into Excel.
I feel like I'm missing some kind of obvious answer, but is there a way to skip the saving part and just copy the image from its URL to put it somewhere else?
Thanks!
After much trial and error, I was able to figure this out so I thought I'd post the answer:
xlwings only allows for pictures from a filepath or matplotlib figures to be added to Excel files. Therefore, I had to create a matplotlib figure of the image, turn its axes off, and insert it into the Excel file. There may be a better way to do it, but this is what worked for me:
from matplotlib import pyplot as plt
from skimage import io
import xlwings as xw
image_url = 'urlname.com'
fig = plt.figure()
plt.axis('off')
plt.imshow(io.imread(image_url))
wb = xw.Book(r'file\path\document.xlsm')
dashboard = wb.sheets['sheet1']
dashboard.pictures.add(fig)

Plotting spectrogram but returned weird results

I already have some audio converted to spectrogram image using matplotlib.pyplot.specgram function.
After some operations on the image, now I would like to save it as an image file.
My simple thought was that since the data are already numpy arrays, I could just plot them.
The code was:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import imageio
im = imageio.imread(path/0.png')
plt.plot(im)
plt.show()
However, it returns very weird image as shown.
The image looks weird and obviously is not recognized as spectrograms
The original image was greyscale with shape (256, 128), how come the returned image has different colors?
What should I do now? Is it the wrong parameters setting that caused the results?
similar to the demonstration in https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html, you should be using the plt.pcolormesh function instead of the plt.plot. Currently, you are plotting each of the spectrogram's lines separately.

Histogram image not getting saved

import numpy as np
import matplotlib.pyplot as plt
x=np.random.gamma( 2, 3, 100000)
plt.hist( x, bins=30)
plt.show()
plt.savefig("normalhistogram.png")
The above code is working perfectly for plotting histograms for gama distribution values, but the only problem is that I want to save the image of generated histogram but plt.savefig("normalhistogram.png") is creating a blank image everytime I execute the code instead of saving the histogram as image. I'm unable to figure out the issue here. Help?
Do not show() the image before saving it. Showing the image clears the canvas. (But saving does not, so you can show the image after saving it.)

Matplotlib imshow displays blank plot

I figured this should be simple but I guess not.
from scipy import misc
import matplotlib.pyplot as plt
img = misc.imread("Alyson.jpg")
plt.imshow(img)
plt.show()
It draws a blank canvas. Inspecting the array the values were obviously loaded correctly. I don't really know what the issue could be. I've also tried loading it with matplotlib.image.imread and with PIL.Image.open to the same effect.
I'm running Lubuntu 13.04. Here are some versions of various libraries:
Pillow==2.5.2
matplotlib==1.3.1
numpy==1.8.2
scipy==0.14.0
EDIT: SOLVED!
I switched the rendering backend using matplotlib.use("WX").
Not certain what the cause of the problem was, but I switched the backend to wxPython and images loaded with misc.imread or matplotlib.image.imread worked fine.
import matplotlib
matplotlib.use("WX")
import matplotlib.pyplot as plt
...etc

Categories