def connectStream(self): dlg = QInputDialog(self) dlg.setInputMode(QInputDialog.TextInput) dlg.setLabelText("URL:") dlg.setTextEchoMode(QLineEdit.Normal) dlg.setTextValue(self.current_stream) dlg.resize(400, 100) dlg.exec() if dlg.result() and validators.url( dlg.textValue()) and dlg.textValue() != self.current_stream: self.current_stream = dlg.textValue() self.player.setMedia(QUrl(self.current_stream)) elif dlg.result() and not validators.url(dlg.textValue()): msg_box = QMessageBox() msg_box.setText("Error URL. Please try again") msg_box.setWindowTitle("Error URL") msg_box.exec() self.connectStream()
def snip(bounding_box=None, file_name_pattern=DEFAULT_OUTPUT_FILE_NAME_PATTERN, override=False, show_time=1000, assign_description=0): """ Snip screen image and save it to file. :param bounding_box: [tuple] The image rectangle in screen, formatted in (left, upper, width, height). :param file_name_pattern: [string] The file name pattern (absolute path or relative path to this python script file). For example: "ScreenSnippingImages/ScreenSnippingImage_%Y-%m-%d_%H-%M-%S.png". :param override: [bool] Whether to override the output file if it exists before. :param show_time: [int] Milliseconds time to show the screen image (if 0, the image won't be shown). :param assign_description: [int] Whether to assign description to the screen image (1 for manually input, 2 for OCR, others for no description). """ logging.info("Started snipping screen.") screen_image = get_screen_image(bounding_box=bounding_box) file_name = save_image(image=screen_image, file_name_pattern=file_name_pattern, override=override) if file_name: logging.info( "Screen image has been saved in file: {}".format(file_name)) if show_time > 0: image_dialog = QDialog() image_dialog.setWindowTitle(APP_DESCRIPTION + " " + APP_VERSION) image_dialog.setWindowFlags(Qt.WindowStaysOnTopHint) image_dialog.setFixedSize(640, 360) image_dialog.setContentsMargins(0, 0, 0, 0) image_label = QLabel() image_dialog_layout = QGridLayout(image_dialog) image_dialog_layout.setContentsMargins(0, 0, 0, 0) image_dialog_layout.addWidget(image_label) image_label.setPixmap(screen_image) image_label.setScaledContents(True) QTimer().singleShot(10, image_dialog.activateWindow) QTimer().singleShot(show_time, image_dialog.close) image_dialog.exec() if assign_description == 1: description_input_dialog = QInputDialog() description_input_dialog.setWindowTitle(APP_DESCRIPTION + " " + APP_VERSION) description_input_dialog.setWindowFlags( Qt.WindowStaysOnTopHint) description_input_dialog.setFixedSize(400, 200) description_input_dialog.setInputMode( description_input_dialog.TextInput) description_input_dialog.setLabelText( "Please input description:") QTimer().singleShot(10, description_input_dialog.activateWindow) description_input_dialog.exec() description = description_input_dialog.textValue() if description: description_file_name = file_name + ".txt" with open(description_file_name, "w") as file: file.write(description) logging.info( "Assigned a description for screen image file: {}". format(file_name)) logging.debug("Description: {}".format(description)) elif assign_description == 2: import pytesseract text_from_image = pytesseract.image_to_string( Image.open(file_name)) description_file_name = file_name + "-OCR.txt" with open(description_file_name, "w") as file: file.write(text_from_image) os.startfile(description_file_name) else: pass else: logging.error("Error occurred.")