grab image from clipbroad fuction pyhon in linux - python-3.x

I am looking for a method in which I can grab the image which is on the clipboard and assign it to the background of a pygame screen. I am trying to make an app in which a user can quickly annotate a captured image in linux. Below is a an example of what I am talking about. Unfortunately these modules do not work in linux. It's not the complete code and I have just included it to make my question clearer.
import pygame
from PIL import ImageTk, ImageGr
pygame.init()
def backgroundimage():
while True:
clipimage = ImageGrab.grabclipboard()
screen = pygame.display.set_mode(([800,800]), pygame.NOFRAME)
screen.fill((clipimage))
if __name__ == '__main__':
backgroundimage()

Related

How do i close a figure shown using matplotlib in python ? And What is difference among image, figure and picture?

I am using ubuntu. I want to close a figure shown using matplotlib after few seconds without using keyboard or mouse. I am able to close an image shown using PIL after few seconds by getting its process id and then kill it.
And i am also little bit confused among terms figure, image and picture in matplotlib.
Thank you so much in advance.
Regarding part 1.
i have used plt.close(), plt.close("all") as well as 'psutil' library to fetch process ID and kill. But none of them worked. I got only solution of closing an image opened via 'PIL'.
link :-
How can I close an image shown to the user with the Python Imaging Library?
Regarding part 2.
Actually, at some pages, i found the terms 'figure','picture' and 'image' were used interchangeably; and at some pages they were not. I saw 'plt.imshow()' is used for image and picture and 'plt.show()' is used for figure. But, what is difference between figure, image and picture. And when to use these functions?
link :-
Why plt.imshow() doesn't display the image?
# for graphing
import matplotlib.pyplot as plt
import time
# for process
import psutil
# importing for image processing
from PIL import Image
#### closing an image which was opened via PIL
#### working perfectly
filename = "check.jpg"
img = Image.open(filename)
img.show()
time.sleep(5)
# for killing process such that image viewer
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
#### closing an image/figure which was opened via matplotlib
#### unable to close without keyboard or mouse
x = [[1,2,3,4],[11,22,33,44],[9,8,7,6]]
print (x)
plt.imshow(x)
plt.colorbar()
plt.title("a")
plt.xlabel('b')
plt.ylabel('c')
a = plt.show()
time.sleep(2)
## not working
plt.close()
## not working
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
## not working
plt.close("all")
i expect that my shown figure closes automatically after a few seconds,
instead of any manual intervention.

How do I increase the speed of RawTurtle?

I'm currently using an embedded turtle canvas in a tkinter window. While it's intuitive that all I need to do is set my turtle to turtle.RawTurtle(canvas), there are some functions that just don't work, and I can't figure out why.
t.clear();t.pu();t.speed(0);t.ht();t.tracer(0)
But I get the error:
AttributeError: 'RawTurtle' object has no attribute 'tracer'
Despite this, many other functions work, such as clear, penup, speed, and hideturtle.
Is there any way of disabling screen updates until the drawing is finished, then manually updating the canvas, with RawTurtle?
The tracer() method is a method of the turtle's screen, not the turtle itself. To get access to it, when embedded under a tkinter window, wrap the canvas in a turtle screen:
screen = turtle.TurtleScreen(canvas)
t = turtle.RawTurtle(screen)
which should give you access to the various screen methods. Then you should be able to use screen.tracer(0) to turn off drawing updates and screen.update() to show the finished drawing. A more complete example:
import tkinter as tk
import turtle
root = tk.Tk()
canvas = turtle.ScrolledCanvas(root)
canvas.pack(side=tk.LEFT)
screen = turtle.TurtleScreen(canvas)
t = turtle.RawTurtle(screen)
t.hideturtle()
# t.speed('fastest')
screen.tracer(0)
t.penup()
t.sety(-100)
t.pendown()
t.circle(100)
screen.update()
screen.mainloop()

Im having trouble trying to time the update of a tkinter label image

I am trying to make a basic 4 frame animation, I cannot use a tkinter canvas as I want it to use the art which I have drawn (the files). There is nothing wrong with the file type as I have tested it on its own. However the code seems to just remove the window for the 6 seconds and then show the final frame.
import time
import tkinter
window=tkinter.Tk()
frame1=tkinter.PhotoImage(file="file1.ppm")
frame2=tkinter.PhotoImage(file="file2.ppm")
frame3=tkinter.PhotoImage(file="file3.ppm")
frame4=tkinter.PhotoImage(file="file4.ppm")
image=tkinter.Label(window,image=frame1)
image.pack()
time.sleep(2)
image.configure(image=frame2)
time.sleep(2)
image.configure(image=frame3)
time.sleep(2)
image.configure(image=frame4)
I'm not sure whether it is the "time.sleep" or the "image.configure" that is the problem but I have tried to mess around with different types of timing methods which also seem to fail.
import tkinter
window=tkinter.Tk()
frame1=tkinter.PhotoImage(file="file1.ppm")
frame2=tkinter.PhotoImage(file="file2.ppm")
frame3=tkinter.PhotoImage(file="file3.ppm")
frame4=tkinter.PhotoImage(file="file4.ppm")
image=tkinter.Label(window,image=frame1)
image.pack()
def loop(n):
frame = [frame1, frame2, frame3, frame4][n]
window.after(2000, lambda : image.configure(image=frame))
window.after(2000, lambda : loop((n+1)%4))
loop(1)

How to play sounds on python with tkinter

I have been working on a sort of 'Piano' on python. I have used tkinter as the ui,
and this is how it goes:
from tkinter import*
tk =Tk()
btn = Button(tk, text="c note", command = play)
How do I define the function play to play a sound when I press it?
Please Help.
Add these two pieces of code:
from winsound import *
&
command = lambda: Playsound("click_one.wav", SND_FILENAME)
If you don't like lambda then you can define a function before the creation of the button:
def play():
return PlaySound("click_one.wav", SND_FILENAME)
You can also define a lambda function:
play = lambda: PlaySound("click_one.wav", SND_FILENAME)
You can use pygame! It will not create a different window.
Check out Pygame's official site for more amazing functions like getting length.
There are two types of sound you can play sound or even music. Each supports pros and cons.
Tkinter doesn't support audio. You can even use pyglet or other modules.
Example code:
import pygame
from tkinter import *
root = Tk()
pygame.init()
def play():
pygame.mixer.music.load("Musics/example.mp3") #Loading File Into Mixer
pygame.mixer.music.play() #Playing It In The Whole Device
Button(root,text="Play",command=play).pack()
root.mainloop()

Custom sound in a message dialog box in wxPython

I was wondering is there any way to get a custom sound to play as soon as a message dialog box comes up? my only restriction is that I can only use wxPython for this, for arguments sake lets call the sound file 'music.wav' Here is my code so far (ignore the stuff about playing with text, that was me creating a dummy GUI for it to load):
import wx
import time
import winsound, sys
class ButtonTest(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,None,id,'Button/Text test frame',size=(800,500))
panel=wx.Panel(self)
button=wx.Button(panel, label='Exit', pos=(200,50), size=(-1,-1))
self.Bind(wx.EVT_BUTTON, self.closer, button)
self.Bind(wx.EVT_CLOSE, self.wincloser)
ape=wx.StaticText(panel, -1, 'This text is STATIC', (200,80))
ape.SetFont(wx.Font(25, wx.SWISS, wx.ITALIC, wx.BOLD, True,'Times New Roman'))
def beep(sound):
winsound.PlaySound('%s.wav'%sound, winsound.SND_FILENAME)
#wx.FutureCall(1000, beep('C:\Users\Chris\Desktop\music'))
box=wx.MessageDialog(None,'Is this a good test?','Query:',wx.ICON_ERROR)
answer=box.ShowModal()
box.Destroy
beep('C:\Users\Chris\Desktop\music')
def closer(self,event):
self.Close(True)
def wincloser(self,event):
self.Destroy()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=ButtonTest(None,id=-1)
frame.Show()
app.MainLoop()
For Windows, there's winsound, which is built in to Python. Otherwise, you'll need an external library like pyAudio or Snack Sound. See also Play a Sound with Python

Resources