Cross-platform screenshot in Python 3 - python-3.x

There are quite a few questions like this one, but none of them seem to be both cross-platform and specifically for Python 3, and I'm struggling to find a reliable solution.
How can I take a cross-platform screenshot in Python 3?

My current solution has been to use the ImageGrab function from the PIL library, like so:
from PIL import ImageGrab
image = ImageGrab.grab()

You can use platform.system() to find the current OS, and then use a different solution depending on the operating system:
import platform
if platform.system()=="Windows":
...
elif platform.system()=="Darwin": #Mac
...
elif plarform.system()=="Linux":
...

You can use PrtSc Library.
Command : pip3 install PrtSc
Code :
import PrtSc.PrtSc as Screen
screen=Screen.PrtSc(True,"file.png")

Related

Import Numpy library in flutter using starflut

I want to run a python script in my flutter app. Thanks to starflut package which helped to do this. Now I want to run a python script that uses NumPy and OpenCV libraries. I want to import these libraries into my python script. I researched a lot about how can I achieve this I couldn't find a way. So I'm posting here so that whoever got the solution for this problem can suggest to me how to do this.

How Can I Import A Python 3 Module With A Period In The Filename?

What is the proper way to import a script that contains a period, such as program_1.4.py, ideally using importlib?
(Now that the imp module is deprecated, this this answer no longer applies: How to reference python package when filename contains a period .)
After looking through the CPython quite a lot and coming back to some other solutions (especially Import arbitrary python source file. (Python 3.3+)), I realized that I needed to pass the full path to my module. Here is the cross-platform, call-location-independent solution:
"""
import os, sys # For running from Notepad++ shortcut, etc
import importlib.machinery
program_1_4 = importlib.machinery.SourceFileLoader('program_1.4', os.path.join(sys.path[0], 'program_1.4.py')).load_module()
print(program_1_4)
program_1_4.main()
"""

What are Python3 libraries which replace "from scikits.audiolab import Format, Sndfile"

Hope you'll are doing good. I am new to python. I am trying to use audio.scikits library in python3 verion. I have a working code version in 2.7(with audio.scikits) . While I am running with python3 version I am getting the Import Error: No Module Named 'Version' error. I get to know that python3 is not anymore supporting audio.scikits(If I am not wrong). Can anyone suggest me replacing library for audio.scikits where I can use all the functionalities like audio.scikits do OR any other solution which might helps me. Thanks in advance.
2.7 Version Code :
from scikits.audiolab import Format, Sndfile
from scipy.signal import firwin, lfilter
array = np.array(all)
fmt = Format('flac', 'pcm16')
nchannels = 1
cd, FileNameTmp = mkstemp('TmpSpeechFile.wav')
# making the file .flac
afile = Sndfile(FileNameTmp, 'w', fmt, nchannels, RawRate)
#writing in the file
afile.write_frames(array)
SendSpeech(FileNameTmp)
To check entire code please visit :Google Asterisk Reference Code(modifying based on this code)
I want to modify this code with python3 supported libraries. Here I am doing this for Asterisk-Microsoft-Speech To Text SDK.
Firstly the link code you paste is Asterisk-Google-Speech-Recognition, it's not the Microsoft-Speech-To-Text, if you want get a sample about Microsoft-Speech-To-Text you could refer to the official doc:Recognize speech from an audio file.
And about your problem you said, yes it's not completely compatible, in the github issue there is a solution for it, you could refer to this comment.

Is there a way to programmatically clear the terminal across platforms in Python 3?

Long-time lurker, first time asker.
Is there a way to automatically clear the terminal in Python 3 regardless of what platform the app is being used in?
I've come across the following (from this answer) which utilises ANSI escape codes:
import sys
sys.stderr.write("\x1b[2J\x1b[H")
But for it to work cross-platform it requires the colorama module which appears to only work on python 2.7.
For context I'm learning Python by building a game of battleships, but after each guess I want to be able to clear the screen and re-print the board.
Any help is appreciated!
Cheers
I use a single snippet for all the platforms:
import subprocess
clear = lambda: subprocess.call('cls' if os.name=='nt' else 'clear')
clear()
Same idea but with a spoon of syntactic sugar:
import subprocess
clear = lambda: subprocess.call('cls||clear', shell=True)
clear()
I know of this method
import os
clear = lambda: os.system('cls')
clear()
I'm not sure if it works with other platforms, but it's working in windows python 3.x
import os
clear = lambda: os.system('clear')
clear()
That might work for linux and OS X, but I can't test.

how to get down and up interfaces in linux by using python script

from collections import namedtuple
import re
import subprocess
def get_interfaces(external=False, ip=False):
pass
I am new with Python and was hoping someone could help me:
Create an interface.
find down and up interfaces in Linux by using a python script.
As far as I know, there is no standard library in python to manage network interfaces.
The standard tool under Linux is ip. You can wrap it using subprocess. Like this :
import subprocess
for r in subprocess.check_output(["/bin/ip","-o","link"]).split('\n'):
F = r.split(" ")
if len(F)>1:
z=zip(F,F[1:])
s= filter(lambda (k,v): k=="state", z)
print z[0][1][:-1], s[0][1]
Try netifaces, I think it can do exactly what you want
import netifaces
netifaces.interfaces()

Resources