I need to display images within a tk window, but can only use these functions to do it;
from urllib.request import urlopen
from re import findall, MULTILINE, DOTALL
from webbrowser import open as webopen
from os import getcwd
from os.path import normpath
from tkinter import *
from sqlite3 import *
from datetime import datetime
I was thinking that there might be some way I can do this just through tkinter's standard functions, but have yet to find any possible way.
This or I could try and find a way to convert the image to a GIF. Note that no images can be stored on the system.
The images I need to display are either JPGs or PNGs, such as this one.
Any help is appreciated.
As is stated here:
The PhotoImage class can read GIF and PGM/PPM images from files:
photo = PhotoImage(file="image.gif")
photo = PhotoImage(file="lenna.pgm")
The PhotoImage can also read base64-encoded GIF files from strings. You can use this to embed images in Python source code (use functions in the base64 module to convert binary data to base64-encoded strings):
And as was pointed out in the comments below and here you can also use PNG images with PhotoImage.
Images in tkinter have to be passed in as PhotoImage objects to be drawn and in order to get a PhotoImage object, the image has to be one of the above formats.
So you will need to find a way to get the images in one of those formats using the modules you have available.
The Tcl/Tk module Img allow you to load JPEG and PNG (among other formats) with PhotoImage. This module is included in some packaged distributions of Tcl/Tk. So it is worth checking whether it is installed on the computer you are using. To do so, try to load it with:
widget.tk.eval('package require Img')
widget can be any Tkinter widget.
If it returns a version number, then it is installed and loaded so you can use it and open JPEG images simply doing
PhotoImage(file='/my/image.jpg')
Related
I am using Visual Studio Code, Python, and Tkinter in this program and I want to import and display an image from my computer as a screen(it is in the end). I tried to import the image by copying a statement from a video example I However, when I run the program, it says
tkinter.TclError: bitmap "IMAGE.jpg" not defined
import tkinter
from tkinter import Tk
from PIL import ImageTk, Image
screen = Tk()
screen.iconbitmap("IMAGE.jpg")
As far as I know, .jpg is not supported. Try converting it to .ico format. There are online tools, just upload your .jpg and download .ico.
I found a new solution. see this -
instead of screen.iconbitmap("IMAGE.jpg")
use
root.iconphoto(False, PhotoImage(file='IMAGE.png'))
NOTE
Supports png only
Import PhotoImage from tkinter package
For more detail see my github repo - here
I am using PyCharm in order to download an image from a fixed URL
This is the code I'm using in order to do it:
import urllib.request
import random
def download_web_image(url):
name=random.randrange(0,1000)
fullname=str(name)+".jpg"
urllib.request.urlretrieve(url,fullname)
download_web_image('imagizer.imageshack.com/v2/626x626q90/673/MT82dR.jpg')
This is what happens when I double click the downloaded image:
But as you see the image is already downloaded in the Python directory and it is a proper image:
What do I have to do in order for the image to be properly displayed in Pycharm?
This has nothing to do with the fact that the image was downloaded using Python. You will see the same behaviour if you copy an image into your project folder. You can fix it by telling PyCharm about which file types should be displayed as images.
In Settings (File->Settings on Windows) expand Editor then select File Types
Choose Image from the list of Regognized File Types (scroll down...) then make sure that Registered Patterns contains all the file types you want to be displayed as images. If *.jpg is not listed there, add it using the green + on the right.
Source code of a page is all I have.
tree = etree.HTML(source_page_text)
image_list = tree.xpath('//img[#src]')
By using xpath, I can find all the 'img' tag with a 'src' attribute as above. But information of size of a image are in css. In javascript, I can find the size easily by using e.g.
document.querySelectorAll("img")[83].height
since it is an object.
So how do I find the size of a image in python?
Since you're on the server and not in the browser, you'll have to (re)download the image and use a library like PIL to get its size.
from PIL import Image
import urllib.request
import io
def image_size(url)
with urllib.request.urlopen(url) as u:
f = io.BytesIO(u.read())
img = Image.open(f)
return img.size # (width, height) tuple
NOTE: You'll need the PIL(pillow fork) library installed on your system.
Another option is to download the style sheets and use a parser like tinycss to try and correlation css rules with selectors to derive the size. I think this would be tricky though.
Python itself does not provide means to render web pages applying stylesheets, scripts etc.
You could try to use GUI frameworks that have a webbrowser built in (PyQt, PyGTK) that allow execution of JS code in corresponding widgets.
I'm on python 3.5 on a Ubuntu machine. I'm writing a script where I want to grab the screen and search for certain pixel colors in the image.
Since it's not windows, PIL.ImageGrab doesn't work and after some research I started using pyscreenshot.
The following works:
import pyscreenshot as ImageGrab
im = ImageGrab.grab(bbox(1,1,100,100))
Now my problem is that the type of im is PIL.PngImagePlugin.PngImageFile which doesn't have the method .getpixel like PIL.Image does.
While I could save it to a file and load it again with PIL that seems super ugly and not efficient. How do I make a PIL.Image out of this?
I thought something along the lines of
im = Image.new(ImageGrab.grab(bbox(1,1,100,100)))
but that's obviously not it ;)
(Sidenote: If there are other/easier ways than pyscreenshot to get screenshots on Ubuntu, that's fine too)
I am trying to open an image using python; I wrote the following code :
from PIL import Image
im=Image.open("IMG_1930.jpg")
im.show()
But the windows photo viewer opens but it shows the following message instead of the photos:
"windows photo viewer can not open this picture because either the picture is deleted , or it isn't in a location that is accessible."
The show method in PIL is a poor's man way of viewing an image - it has got a hardcoded image viewer application, and writes your image data to a temporary file before calling that as an external application.
What is happening there is that you are either having problems with Windows' uneven access rights policies, and the viewer can't open the file in Python's temporary directory, or there is a problem with Window's problematic path specifications - it might even be a bug in PIL, that renders the temporary paht generated by PIL unusable by the image viewer.
If you are using show in a windowing application, use your tookit's way of viewing images to display it instead - otherwise, if it is a simpler application, build up a Tkitner Window and put the image in it, instead of show.
import sys
import Tkinter
from PIL import Image, ImageTk
window = Tkinter.Tk()
img = Image.open("bla.png")
img.load()
photoimg = ImageTk.PhotoImage(img)
container = Tkinter.Label(window, image=photoimg)
container.pack()
Tkinter.mainloop()
(Linux users: some distributions require the separate install of Tkinter support for PIL/PILLOW. In Fedora, for example, one has to install the python-pillow-tk package )
I also had problems with this. Take a look at this post it fixed my problem: PIL image show() doesn't work on windows 7
Good luck fixing it.