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)
I am using python on a linux shell and trying to save plot instead of displaying (displaying plot window leads to an error). I looked at question Save plot to image file instead of displaying it using Matplotlib, but didn't help. Here is my code:
import matplotlib.pyplot as plt
#
# list3 is list of data
plt.hist(list3, bins=10)
plt.xlabel('X')
plt.ylabel('Y')
fig.savefig('plot.png')
The problem is figure window is appearing even though I don't call plt.figure(). Is there any way to suppress graphical figure window and instead save plot to the file?
plt.savefig('plot.png') saves the png file for me. May be you need to give full path for the file
How can I save images generated with plotly in different formats? Only "Download as PNG" is possible from the generated HTML figure. I would need to interact with the figure (change rotation, choose which data to plot) and save an .eps figure for each online modified plot. Thanks a lot!
Plotly supports exporting to EPS (the docs mention that you need the poppler library) and the Figure object has a write_image method that saves a figure to a file.
You can specify the format through the filename and the resolution with the width and height keyword arguments, representing logical pixels.
You can read more on static image exporting in Plotly here. This is a code example:
fig.write_image("name.eps", width=1920, height=1080)
In order to select what is plotted you will have to set the figure's camera controls.
I am using google colaboratory and trying to save some plots in pdf format for further use. But some text of the labels from the plots have been totally or partially cut while rendering the images in pdf or png format. Using Jupyter notebook gives the same result. Is there any solution to this?
from google.colab import files
plt.savefig("Fid.pdf")
files.download("Fid.pdf")
There is no error message shown.Half of the x label text of this plot has been cut
You can try using this code.
fig = plt.figure(figsize=(15, 15))
plt.savefig("Fid.pdf", bbox_inches='tight')
files.download("Fid.pdf")
This should work. If it works do not forget to click the upvote button and mark this answer as accepted please.
i'm a beginner in python, i really don't know how create an animation. I have different plots and i want to create a short video that concatenate these plots.
import pandas as pd
import matplotlib.pyplot as plt
data=[[1,1,3],[1,2,1],[2,1,9],[2,2,0]]
a=pd.DataFrame(data)
a.columns=['x','y','value']
data2=[[1,1,5],[1,2,2],[2,1,1],[2,2,3]]
b=pd.DataFrame(data2)
b.columns=['x','y','value']
data3=[[1,1,15],[1,2,7],[2,1,4],[2,2,8]]
c=pd.DataFrame(data3)
c.columns=['x','y','value']
final=[a,b,c]
for i in range(0,len(final)):
fig, ax = plt.subplots()
plt.scatter(final[i]['x'],final[i]['y'],c=final[i]['value'],vmin=0, vmax=15,)
plt.colorbar()
Python isn't really the way to go for creating a video.
If you want to present your graph(s) in video format, screenshot them using Windows Button + Print Screen (PrtSc) on your keyboard. This will save a screenshot to your pictures folder. Then, use a video editor, such as Vegas or free options like WeVideo to put the screenshots into a video.
You can also use presentation software, like Prezi, for a more engaging experience.