Exporting and downloading images from Azure Blob Storage - python

I am exporting some matplotlib plots from Databricks to Blob Storage. No problem here, just using:
plt.savefig('/dbfs/my_plot.png')
dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)
The problem is that when I download and open the image locally, it is blank. I am not sure if I have to transform it or at some point specify the metadata, cause it appears as application/octet-stream in the Blob.
any ideas?

• When you are trying to download and open the image locally, you are still seeing the image as blank because you are using the command as posted by you for saving them in readable image format in the Azure blob storage, which is correct but when you are showing the plot or downloading it, you should use the command ‘plt.show('/dbfs/my_plot.png')’ before the below commands as shown: -
plt.show('/dbfs/my_plot.png')
plt.savefig('/dbfs/my_plot.png')
dbutils.fs.cp('dbfs:my_plot.jpg', blob_container)
• If you are not using the above command as said, you will only get the image as blank only. Also, if you don’t want undesirable white space around your image, you can remove that using the command as below as this white space may also be the reason behind your image showing as blank as the space might have consumed greater area than the actual plot.
plt.savefig('/dbfs/my_plot.png', bbox_inches='tight')
For more information and details regarding the solution for this problem, kindly refer to the below community thread: -
Save plot to image file instead of displaying it using Matplotlib

Related

Discord.Py blend pictures and send them without dowload them

I have the following problem:
1.- I want to send a file in discord without dowloading. I don't know if this is posible but I want to send it for example with BytesIo.
2.- I have one picture saved on my Bot files and the other one comes from ctx.author.avatar
3.- I want to blend both images and send the result. With blend I mean like for example if I would be using cv2 I would use addWeighted().
The Code I have right know what does is dowload the picture of the member, using cv2 to read both pictures, resize them and use addWeighted. After that I save the blend picture and I send it as a message. When all It's done I delete the pictures (both, the avatar and the blend one). From my point of view this is really inefficient, thats why I want know if there is a way using PIL and BytesIo or something to use the dataArray to blend them and send it without dowload it.
So in short, I want to know if there is a way to blends both images without download the second one (member avatar picture) and send it without dowloading the blend image.
I can the code I already have if needed but as my code is download the picture I guess that won't help.
You can get the image's URL from the message (Get a picture from the message):
message.attachments[0].url
Then load the image into memory with the requests library (How to open an image from an url with opencv using requests from python)
The way I found at the end to solve it, was using just Pillow doing the following:
image1 = Image.open("img.jpg", mode='r')
image2 = Image.open(requests.get(url, stream=True).raw)
With I have both images on memory, so I just needed to resize both of them and blend. Finally I just used io.Bytes() to make into bytes and send it.
How to blend -> https://pythontic.com/image-processing/pillow/blend

How to save images from Python Interactive tab in Microsoft Visual Studio Code

I am using the Python Interactive tab (similar to Jupyter notebooks) in Microsoft Visual Studio Code.
When I plot an image, I am unable to save it directly from the editor. There's no option to save it directly with the mouse, or to save it directly from the interface.
Is there a way to save it from the interface or should I only use matplotlib's savefig method?
In my version (1.47.3) I can double click the image, which opens it in a separate tab for inspection.
One of the buttons in this tab is a save icon, which allows you to save the image in a format that depends on the file ending you choose. I've tried .png, but couldn't find an option to change resolution. .pdf or .svg saves it losslessly as vector graphic.
In the image below you can see what it looks like for me. (on Ubuntu)
Not fully sure if this counts as a full answer here on Stack Overflow. But the answer here is that we don't have a way to do this currently. You can highlight it in the interactive window and Ctrl-C to copy it out, but even that support is rather flakey at this point. If you would like to log this issue this would be the best spot to get it on our radar:
https://github.com/Microsoft/vscode-python/issues
We keep our issues open to the public so you can track when we work on it after it's filed there.

How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?

I would like to include image in a jupyter notebook.
If I did the following, it works :
from IPython.display import Image
Image("img/picture.png")
But I would like to include the images in a markdown cell and the following code gives a 404 error :
![title]("img/picture.png")
I also tried
![texte]("http://localhost:8888/img/picture.png")
But I still get the same error :
404 GET /notebooks/%22/home/user/folder/img/picture.png%22 (127.0.0.1) 2.74ms referer=http://localhost:8888/notebooks/notebook.ipynb
You mustn't use quotation marks around the name of the image files in markdown!
If you carefully read your error message, you will see the two %22 parts in the link. That is the html encoded quotation mark.
You have to change the line
![title]("img/picture.png")
to
![title](img/picture.png)
UPDATE
It is assumed, that you have the following file structure and that you run the jupyter notebook command in the directory where the file example.ipynb (<-- contains the markdown for the image) is stored:
/
+-- example.ipynb
+-- img
+-- picture.png
There are several ways to post an image in Jupyter notebooks:
via HTML:
from IPython.display import Image
from IPython.core.display import HTML
Image(url= "http://my_site.com/my_picture.jpg")
You retain the ability to use HTML tags to resize, etc...
Image(url= "http://my_site.com/my_picture.jpg", width=100, height=100)
You can also display images stored locally, either via relative or absolute path.
PATH = "/Users/reblochonMasque/Documents/Drawings/"
Image(filename = PATH + "My_picture.jpg", width=100, height=100)
if the image it wider than the display settings: thanks
use unconfined=True to disable max-width confinement of the image
from IPython.core.display import Image, display
display(Image(url='https://i.ytimg.com/vi/j22DmsZEv30/maxresdefault.jpg', width=1900, unconfined=True))
or via markdown:
make sure the cell is a markdown cell, and not a code cell, thanks #游凯超 in the comments)
Please note that on some systems, the markdown does not allow white space in the filenames. Thanks to #CoffeeTableEspresso and #zebralamy in the comments)
(On macos, as long as you are on a markdown cell you would do like this: ![title](../image 1.png), and not worry about the white space).
for a web image:
![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)
as shown by #cristianmtr
Paying attention not to use either these quotes "" or those '' around the url.
or a local one:
![title](img/picture.png)
demonstrated by #Sebastian
Alternatively, you can use a plain HTML <img src>, which allows you to change height and width and is still read by the markdown interpreter:
<img src="subdirectory/MyImage.png" width=60 height=60 />
Insert the image directly in the Jupyter notebook.
Note: You should have a local copy of the image on your computer
You can insert the image in the Jupyter notebook itself. This way you don't need to keep the image separately in the folder.
Steps:
Convert the cell to markdown by:
pressing M on the selected cell
OR
From menu bar, Cell > Cell Type > Markdown.
(Note: It's important to convert the cell to Markdown, otherwise the "Insert Image" option in Step 2 will not be active)
Now go to menu bar and select Edit -> Insert Image.
Select image from your disk and upload.
Press Ctrl+Enter or Shift+Enter.
This will make the image as part of the notebook and you don't need to upload in the directory or Github. I feel this looks more clean and not prone to broken URL issue.
Set cell mode to Markdown
Drag and drop your image into the cell. The following command will be created:
![image.png](attachment:image.png)
Execute/Run the cell and the image shows up.
The image is actually embedded in the ipynb Notebook and you don't need to mess around with separate files. This is unfortunately not working with Jupyter-Lab (v 1.1.4) yet.
Edit: Works in JupyterLab Version 1.2.6
I know this is not fully relevant, but since this answer is ranked first many a times when you search 'how to display images in Jupyter', please consider this answer as well.
You could use matplotlib to show an image as follows.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread("your_image.png")
plt.imshow(image)
plt.show()
I'm surprised no one here has mentioned the html cell magic option.
from the docs (IPython, but same for Jupyter)
%%html
Render the cell as a block of HTML
In addition to the other answers using HTML, either embedded into Markdown or using the %%HTML magic:
If you need to specify the image height, this will not work:
<img src="image.png" height=50> <-- will not work
That is because the CSS styling in Jupyter uses height: auto per default for the img tags, which overrides the HTML height attribute. You need to overwrite the CSS height attribute instead:
<img src="image.png" style="height:50px"> <-- works
Here's how you can do it with Markdown:
![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)
If you want to use the Jupyter Notebook API (and not the IPython one anymore), I find the ipywidgets Jupyter's sub-project. You have an Image widget. Docstring specifies that you have a value parameter which is a bytes. So you can do:
import requests
from ipywidgets import Image
Image(value=requests.get('https://octodex.github.com/images/yaktocat.png').content)
I agree, it's simpler to use the Markdown style. But it shows you the Image display Notebook API. You can also resize the image with the width and height parameters.
While a lot of the above answers give ways to embed an image using a file or with Python code, there is a way to embed an image in the jupyter notebook itself using only markdown and base64!
To view an image in the browser, you can visit the link data:image/png;base64,**image data here** for a base64-encoded PNG image, or data:image/jpg;base64,**image data here** for a base64-encoded JPG image. An example link can be found at the end of this answer.
To embed this into a markdown page, simply use a similar construct as the file answers, but with a base64 link instead: ![**description**](data:image/**type**;base64,**base64 data**). Now your image is 100% embedded into your Jupyter Notebook file!
Example link: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==
Example markdown:
![smile](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==)
Here is a Solution for Jupyter and Python3:
I droped my images in a folder named ImageTest.
My directory is:
C:\Users\MyPcName\ImageTest\image.png
To show the image I used this expression:
![title](/notebooks/ImageTest/image.png "ShowMyImage")
Also watch out for / and \
This works for me in a markdown cell. Somehow I do not need to mention specifically if its an image or a simple file.
![](files/picture.png)
I made some research on this topic and found 4 ways for inserting images in Jupyter Notebook that I've described in my article.
1. Drag-and-drop image to the cell (simplest!)
Please switch the cell to markdown type and just drag and drop the image there. The image will be internally encoded as Base64 and its text representation will be included in the ipynb file. I recommend this only for small images because your notebook ipynb file can become very large and laggy (if too many large images are dropped in).
2. Insert hosted image
Upload the image to the server (might be Imgur or GitHub) and enter the image URL in the Markdown:
![alternative text goes here](path-to-the-image)
3. Insert local file image
You can use the above method with a local image but it needs to have the same root directory as the notebook.
In the Markdown:
![alternative text](path-to-image)
or you can use Python:
from IPython import display
display.Image("path-to-image")
Convert image to Base64 (only for small images)
Similar to the first step with a difference that conversion to Base64 is done manually.
The Python code:
from IPython import display
from base64 import b64decode
base64_data = "iVBORw0KGgoAAAANSUhEUgAABL ... the rest of data "
display.Image(b64decode(base64_data))
Should be used with rather small images.
I'm personally using the second method with images hosted in the GitHub repository.
For some reason no other solutions work for me. Here what is working:
Change cell to markdown
Copy past:
<div>
<img src="attachment:name.png" width="600"/>
</div>
Make an empty line after </div> in the same cell and drop your image using mouse into this line.
You will see something like that:
<div>
<img src="attachment:name.png" width="600"/>
</div>
![yourname.jpg](attachment:yourname.jpg)
Copy yourname.jpg and past it instead of name.png
Remove ![yourname.jpg](attachment:yourname.jpg).
The final result should look like:
<div>
<img src="attachment:yourname.jpg" width="600"/>
</div>
Run sell (shift+enter) and you will see your image
One thing I found is the path of your image must be relative to wherever the notebook was originally loaded from. if you cd to a different directory, such as Pictures your Markdown path is still relative to the original loading directory.
I'm assuming you mean in a markdown cell, inside a jupyter notebook. If so, here are the easiest ways for both:
From the local computer:
With your file browser, navigate to the directory containing your picture. Then, drag and drop it into the cell you want to embed the picture. It must already be in markdown mode or nothing will happen.
The pros of doing it this way are:
It's simple, just drag and drop
The cons of doing it this way:
You cannot copy and paste that link, and use it in any other cell. If you want to use the same picture in any other notebook cell, you will need to find the picture, drag and drop into the cell again.
You should consider a file management scheme that will allow you to find a picture again, if you plan on using allot of pictures in your notebooks.
This is an effective way of doing things that's worked for me.
From the web:
The easiest way is to simply find a picture using your web browser, right click and "Copy image", paste this into the cell you want the image displayed.
As above this does come with caveats in that you can't copy this and link and paste it into another cell. So if you plan on using the image again, you'll have to either paste into another cell, or save this image locally.
The images are embedded in the notebook so should work anywhere the notebook is opened.
Insert images that are present locally
Quick Solution : Watch this Video (1:36) https://www.youtube.com/watch?v=Vw1EZh1My8s
In short :
Go to the location where your .ipynb is present.
Create folder called images and paste image that you want insert in jupyter in that images folder
Open .ipynb file on browser and there click on cell and type :
There is a dropdown button which is mentioned as Code. Change that to Markdown
Hurray!! Now run the code by hitting shift + Enter
Reference image
Agreed, i had the same issues and this is what worked and what did not:
WORKED: <img src="Docs/pinoutDOIT32devkitv1.png" width="800"/>
*DOES NOT WORK: <img src="/Docs/pinoutDOIT32devkitv1.png" width="800"/>
DOES NOT WORK: <img src="./Docs/pinoutDOIT32devkitv1.png" width="800"/>*

Downloading a section of an Image from a website in python?

I am currently able to download an entire image from a URL using
from PIL import Image, ImageTk
import cStringIO
import urllib
url = http://prestigemgmt.us/wp-content/uploads/2013/03/dog.jpg
self.image = Image.open(cStringIO.StringIO(urllib.urlopen(url).read()))
It works fine and gets the whole image from the website. My question is there any way to get, lets say only the right half of the image.
I understand I could edit the image after it is downloaded, but speed is an important aspect, so ideally I would download only what I need.
It is not possible to do this.
The common image file formats like PNG and JPEG are encoded in a manner that you cannot download an arbitrary part of the picture without downloading the full picture.
You need to download the whole picture, decode it and edit it after the download.
For the advanced knowledge you can always study PNG and JPEG file formats.
If you are in the control of the server providing the images you can write a server-side script which edits the image on the server and then sends the edit over the wire.

Image preview with Reportlab?

I'm generating some pdf's with Reportlab and Django using a web interface. The pdf's are always going to be a single page. I'd like to generate a png or jpg image of the generated page and return that to the browser for the user to preview before saving the final pdf and delivering it to the end user. Is there anyway to do this?
This answer explains that you can use ghostscript to convert pdf to png. Depending of the requirements of your app (traffic, response time, nb of pdfs ...) it may or may not be a solution for you.
This is just an idea, but may be you can generate the preview image in parallel using PIL ImageDraw and get rid of the pdf-to-png conversion.
I hope it helps

Categories