开发者

Python code to open image in browser?

I want to open an image in the default browser with Python. I thought it might be as simple as webbrowser.open(path_to_file), but on XP at l开发者_开发问答east that opens the Windows Picture and Fax Viewer instead.


Well, it should be that simple (in my opinion), but the problem is with how the webbrowser module sets up the default browser on Windows. Because of this, when you type in

webbrowser.open(path_to_file)

what is then called is

os.startfile(url)

which works fine for url's, but for files, it uses the Windows Picture and Fax Viewer unless you've associated some other program with the image file type. Basically, if you use the webbrowser.get() command to get an actual browser it will be fine. Here's a way to do it with internet explorer on Windows (which has been set-up in the webbrowser module to be the hardest to get) :

import os,webbrowser
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
                        "Internet Explorer\\IEXPLORE.EXE")
browser = webbrowser.get(iexplore)
browser.open(path_to_file)

other ones like firefox are easier to get:

browser = webbrowser.get("firefox")


you can also use selenium

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(r'path\chromedriver.exe')
driver.get(r"img_path\1.png")


This is a slightly difficult question to answer with the current information. It would be helpful if you could clarify which browser you are using and what image format you are trying to display.

Without that information, I can provide the following:

Here I will assume that you will be using a Firefox browser and a jpg image.

import os
os.system('"C:\\Program Files\\ Mozilla Firefox\\Firefox.exe" "path_to_file.jpg"')

This works on my WinXP system.

Now to explain the code. the os module in python has some nifty OperatingSystem tools. os.system executes the input string as a command just as you would if you tried to do so from cmd.

Firefox may not be part of the path variables and may therefore need to be called explicitly from where it lives. This is why I have "C:\\Progam Files...".

You will notice that I have two sets of double quotes in the input parameter to os.system This is because the path to firefox and your jpg might have spaces in them and the Windows command line is fussy about this.

Also, you might notice that there are double-backslashes in the double-quotes. This is because in Python, a backslash is an escape character and is used to give special meaning to the following character (for example "\t" is tab, etc). Therefore, in order to get an actual backslash, we need to escape the escaping nature of the backslash and do "\\".


Looks like that particular installation of Windows is set to use that viewer program as the "default browser" (for information with that content type, at least). To check, what happens when (at Windows' cmd.exe prompt) you type start path_to_file?

If this confirms that this is indeed the installation's choice, then if you want to open that file with some other program then you cannot at the same time want to be using the default browser for that installation (because that viewer, which you apparently do not want to open, is set as the default browser for that file yet -- so you must be wanting to open some other browser instead of the default one!).

If that's indeed the case, I recommend trying webbrowser.get to get a controller for the browser with the given name (e.g. try c = webbrowser.get('windows-default') -- or, if that, as is quite possible, reproduces the behavior you don't want, try get('firefox') instead), then c.open(path_to_file) to open the file in question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜