uploader = widgets.FileUpload()
uploader
Produces a box that you can click and upload a file (a jpg or png for example). How do I code to access this and use it in future code?
For example, if I want to print the uploaded image.
Thanks!!
code
This is a pretty general question, so I can only provide a general answer. This would be the full uploader code to print the image properties:
import ipywidgets as widgets
uploader = widgets.FileUpload()
uploader
from PIL import Image
for name, file_info in uploader.value.items():
img = Image.open(io.BytesIO(file_info['content']))
print(img)
From there you can pass the image to cv or other image processors as well.
Related
I have a chart function that saves the end figure as a file. After I run the function, I also want it to display the figure at the end. So, I use this:
from PIL import Image
filepath = 'image.png'
img = Image.open(filepath)
img.show()
It works just fine, but when the file opens, it opens with a random file name, not the actual file name.
This can get troublesome as I have a lot of different chart functions that work in a similar fashion, so having logical names is a plus.
Is there a way I can open an image file with Python and have it display it's original file name?
EDIT
I'm using Windows, btw.
EDIT2
Updated the example with code that shows the same behaviour.
Instead of PIL you could use this:-
import os
filepath = "path"
os.startfile(filepath)
Using this method will open the file using system editor.
Or with PIL,
import Tkinter as tk
from PIL import Image, ImageTk # Place this at the end (to avoid any conflicts/errors)
window = tk.Tk()
#window.geometry("500x500") # (optional)
imagefile = {path_to_your_image_file}
img = ImageTk.PhotoImage(Image.open(imagefile))
lbl = tk.Label(window, image = img).pack()
window.mainloop()
The function img.show() opens a Windows utility to display the image. The image is first written to a temporary file before it is displayed. Here is the section from the PIL docs.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.show
Image.show(title=None, command=None)[source] Displays this image. This
method is mainly intended for debugging purposes.
This method calls PIL.ImageShow.show() internally. You can use
PIL.ImageShow.register() to override its default behaviour.
The image is first saved to a temporary file. By default, it will be
in PNG format.
On Unix, the image is then opened using the display, eog or xv
utility, depending on which one can be found.
On macOS, the image is opened with the native Preview application.
On Windows, the image is opened with the standard PNG display utility.
Parameters title – Optional title to use for the image window, where
possible.
"
The issue is that PIL uses a quick-and-dirty method for showing your image, and it's not intended for serious application use.
I have saved some images of my work in .pdf format using matplotlib, I know this is my fault from the beginning and I should save it directly as image but I did not know that I can not display pdf files on colab. To get these results I need another 10 days which is not good choice for me.
Actually I have found this which express my problem precisely but there was not answer.
It just seems strange to me that using matplotlib I can save pdf files but I can not load them using it again.
I just need to display the pdf file in colab cell ,I have tried:
import subprocess
subprocess.Popen(['myfile.pdf'],shell=True)
and this was the result:
<subprocess.Popen at 0x7f4d6a395978>
another methods as in this page do not work for me
Ok this works for me, maybe there are a simpler solution but for now this works
from pdf2image import convert_from_path
from IPython.display import display, Image
images = convert_from_path("myfile.pdf")
for i, image in enumerate(images):
fname = "image" + str(i) + ".png"
image.save(fname, "PNG")
Image(fname, width=600, height=300)
In a Jupyter notebook / Colab you can simply
from pdf2image import convert_from_path
images = convert_from_path("myfile.pdf")
images[0] # first page
The image will be able to render as the cell output. No need for IPython.display
I'm trying to save images from the Spotify API
I get album art in the form of a link:
https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce
I think it's a jpg file.
I run into errors in trying to display or save this in python.
I'm not even sure how I'm meant to format something like:
Do I need str around the link?
str(https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce)
Or should I create a new variable e.g.
image_path = 'https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce'
And then:
im1 = im1.save(image_path)
Your second suggestion should work with an addition of actually downloading the image using urllib.request:
import urllib.request
image_path = 'https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce'
urllib.request.urlretrieve(image_path, "image.jpg")
When i open an image in image viewer the displayed image name is wrong (not the same as loaded image). Orginal image = 'image.PNG', name in image viewer='tmpy4uvijg0.BMP' (the new name always changeds, see in image below)
from PIL import Image
imName='image.PNG'
try:
with Image.open(imName) as im:
print(imName)
im.show()
except IOError:
pass
image.png
new image
What do i wrong? Why is the name not the same?
It is because the show method save the image to a temporary file, as say in the documentation:
Displays this image. This method is mainly intended for
debugging purposes.
On Unix platforms, this method saves the image to a temporary
PPM file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses
the standard BMP display utility to show it (usually Paint).
:param title: Optional title to use for the image window,
where possible.
:param command: command used to show the image
You can try to change the title by passing a string in parameter to show.
I'm looking for a way to download an 640x640 image from a URL, resize the image to 180x180 and append the word small to the end of the resized image filename.
For example, the image is located at this link
http://0height.com/wp-content/uploads/2013/07/18-japanese-food-instagram-1.jpg
Once resized, I would like to append the world small to the end of the filename like so:
18-japanese-food-instagram-1small.jpeg
How can this be done? Also will the downloaded image be saved to memory or will it save to the actual drive? If it does save to the drive, is it possible to delete the original image and keep the resized version?
Why don't you try urllib?
import urllib
urllib.urlretrieve("http://0height.com/wp-content/uploads/2013/07/18-japanese-food-instagram-1.jpg", "18-japanese-food-instagram-1.jpg")
Then, to resize this you can use PIL or another library
import Image
im1 = Image.open("18-japanese-food-instagram-1.jpg")
im_small = im1.resize((width, height), Image.ANTIALIAS)
im_small.save("18-japanese-food-instagram-1_small.jpg")
References:
http://www.daniweb.com/software-development/python/code/216637/resize-an-image-python
Downloading a picture via urllib and python