I am new to pyton and i want to prank my friends by make a specific picture to pop up. I have set up everything up but I don't understand how to open a picture link, like in javascript (I think). Any solution to this problem is acceptable!
Your question is very confusing and it is more on the "can you code this for me" rather than you showing a problem you are having.
Nevertheless, a solution would be the following:
import os
from PIL import Image,ImageFont,ImageDraw
import time
import psutil
filedir=os.path.dirname(os.path.abspath(__file__))
while True:
picture=Image.open(filedir+'/picture_name.jpeg')
draw = ImageDraw.Draw(picture)
picture.show()
time.sleep(30)
peicture.close()
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
time.sleep(1200)
Now obviously for this you need to have the picture locally saved and know the path to it.
If you want to load one from the internet then I assume you could use
import urllib.request
url = urllib.request.urlopen('URL')
Or something of that form. I have never tried it.
Related
Whenever I try to display symbolic math in Spyder via the IPython console, several black console windows pop up and then disappear in quick succession. It prints the expression, but I'd like to know if there is a way to get rid of these windows. The windows have the title "C:\Program Files\MikTex 2.9..." if that helps.
It looks like someone already figured it out and posted a solution on GitHub. This is the link: https://github.com/sympy/sympy/issues/11882
It took me (as a novice) some time to figure out exactly what he did, so the following is just a more detailed explanation:
You first need to find the compatibility module in the sympy package. For me, it was located at "C:\Users\Lucas\Anaconda3\Lib\site-packages\sympy\core\compatibility.py". Next, you need to search (in the source code of that module) for the check_output function. The surrounding code should look something like:
# check_output() is new in Python 2.7
import os
try:
try:
from subprocess import check_output
Finally, you need to get rid of the last line, and replace it with the code found in the GitHub link. The resulting block should look like:
# check_output() is new in Python 2.7
import os
try:
try:
from subprocess import check_output as subprocess_check_output
def check_output(*args, **kwargs):
return subprocess_check_output(*args, **kwargs, creationflags=0x08000000) # CREATE_NO_WINDOW
It appears to me that he defines a function which takes the place of check_output, except that the argument to suppress the output windows is always fed in. Hope this helps anyone else having this problem, and I appreciate the fix from Adam on GitHub.
I submitted a pull request to fix this for good:
https://github.com/sympy/sympy/pull/12391
I am using PIL to open a single image in the default image viewer:
from PIL import Image
img = Image.open('example.jpg')
img.show()
Does any Python module contain a function enabling opening multiple images in the current system's default image viewer? For instance when on OS X, Preview.app should open with the list of images in the sidebar. From the command line this is no problem at all:
$ open my_picture_number_*
Use case is that users should just be able to explore a few dozen images.
Use subprocess.run to run the operating system's default image viewing app. subprocess.run works a lot like a command line. You just need to know what the command is for the operating system you're on. For windows, "explorer" will do; for OS X, as you point out, it's "open." I'm not sure what it is for Linux, maybe "eog"?
So, your code would look like this:
import sys
import subprocess
def openImage(path):
imageViewerFromCommandLine = {'linux':'xdg-open',
'win32':'explorer',
'darwin':'open'}[sys.platform]
subprocess.run([imageViewerFromCommandLine, path])
I've tried to use #jgfoot's answer, which worked, but made my program hang after the viewer was launched. I've solved this issue by using subprocess.Popen instead, like this:
import sys
import subprocess
def openImage(path):
imageViewerFromCommandLine = {'linux':'xdg-open',
'win32':'explorer',
'darwin':'open'}[sys.platform]
subprocess.Popen([imageViewerFromCommandLine, path])
I would like to know if there is a way to type things with Python using the win32api module. For example, if I want to type in the phrase "Happy Pi Day" into Microsoft Word every three seconds, I would have something like
import time
while 1:
#types Happy Pi Day
time.sleep(3)
However, I do not know what the command is for the "type" function.
Is using win32api module a hard requirement? If not, you can use this:
https://code.google.com/p/pywinauto/
Then its easy (more examples here...):
from pywinauto import application
app = application.Application.start("notepad.exe")
app.notepad.TypeKeys("%FX")
The type function is keyboard here is a link to help with that
https://www.geeksforgeeks.org/keyboard-module-in-python/
and to have something typed repeatedly you have this code
import pyautogui
import time
time.sleep(10)
for line in open("auto file.txt", "r"):
pyautogui.typewrite(line)
pyautogui.press("enter")
here is a link
https://www.askpython.com/python/examples/auto-type-text-using-python
So basically I am trying to open an image and then close it after few seconds with time.sleep.
First I tried using
import Image
import time
myImage = Image.open("C:\\Users\\User\\Desktop\\image.jpg")
myImage.show()
time.sleep(5)
but this didn't work out well, since the image didn't even open because Windows Photo Viewer couldn't find the file. However when I use webbrowser.open like this
import webbrowser
webbrowser.open('C:\\Users\\User\\Desktop\\image.jpg')
webbrowser.close()
it successfully opens the file in Windows Photo Viewer, but closing doesn't seem to work. It gives me the following error:
AttributeError: 'module' object has no attribute 'close'
I've been searching for 2 days now with no working solution. The image is .jpg incase that matters. Also I don't want to change my default image viewer or modify things that other people who use this would have to modify as well. Using Python 2.7.
Any help would be very much appreciated!
This is my way to do it in windows_xp_sp3.
import os, time
pic=["C:\\19.jpg","C:\\20.jpg","C:\\21.jpg","C:\\22.jpg"]
for p in pic:
os.startfile(p)
time.sleep(3)
os.system("taskkill /IM rundll32.exe")
Or, try this:
import subprocess, time
pic=["C:\\19.jpg","C:\\20.jpg","C:\\21.jpg","C:\\22.jpg","C:\\23.jpg","C:\\24.jpg","C:\\25.jpg","C:\\26.jpg","C:\\27.jpg"]
for p in pic:
r=subprocess.Popen(p,shell=True)
time.sleep(3)
# r.kill() #It won't work, because "shell=True" is set.If you need to kill the "subprocess",just don't use it.
Like This:
import os,subprocess, time
pic=["C:\\19.jpg","C:\\20.jpg","C:\\21.jpg","C:\\22.jpg","C:\\23.jpg","C:\\24.jpg","C:\\25.jpg","C:\\26.jpg","C:\\27.jpg"]
for p in pic:
r=subprocess.Popen(["rundll32.exe","shimgvw.dll,ImageView_Fullscreen",p])
time.sleep(3)
r.kill()
You are on the wrong track here, to close it you would have to instruct the browser to close that tab, or window, or whatever.
The way to instruct a browser to do that, is not standardized, and the same holds for something which is different for each browser, or indeed for each photo viewer.
If you want to control both opening and closing of a photo, you are better off doing that from within your Python program, instead of calling upon a different program such as a browser.
Im using Ubuntu 12.04. I want to access Rhythymbox using Python .
This is how I've proceeded so far:
Ive gone through this site
https://live.gnome.org/RhythmboxPlugins/WritingGuide , but it gives details on how to write plugins , which Im not interested in right now. Ive gone through a few tutorials which tells me to do this.
import dbus
session_bus = dbus.SessionBus()
proxy_obj = session_bus.get_object(
'org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
But I am getting the following error
DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Rhythmbox was not provided by any .service files.
Could someone please point me in the right direction of what I would like to achieve?
A workaround, used by lyricsdownloader.py, is:
import subprocess
import shlex
proc = subprocess.Popen(shlex.split('rhythmbox-client --no-start --print-playing-format %tt')))
title, err = proc.communicate()
Note: This does not work with Ubuntu 11.10, which shipped without rhythmbox-client.
This might be useful. https://github.com/aliva/rhythmbox-microblogger
It is a twitter plugin for RhythmBox. So instead of twitter and Gtk, you can just take the current song.
from gi.repository import RB
RB.RhythmDBPropType.TITLE will give enum which you can use to get the title.
I think that you've encountered a bug in Rhythmbox DBus interface described on Launchpad. Tracker says that fix is committed, but possibly your version doesn't have that fix.