basiczoqa.blogg.se

Writing scripts in python
Writing scripts in python











Let's use comments to explain what our code does: I haven't put any comments in my previous example, so this can be the first step to improving our Python script. If you decide to over-engineer your code and make it highly optimized at the expense of readability, you must be thorough in your comments.

writing scripts in python

In other words, your code must be well-commented, readable, and understandable by anyone who’ll look at your Python script. Instead, think about your future self – or, if you write Python code for someone else, about the person who will maintain the code after you. My first tip is not to write scripts for yourself, even if you are the only one using them. Now that we have something running, let's see how we can improve this script with the first Python script tip. Gray_img_small = cv2.resize(gray_img, (960, 540))Ĭv2.imshow('Grayscale Image', gray_img_small) Gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) Image_files = sorted(glob.glob("./data/formula_one/*.jpg")) It will read the images, convert them to grayscale and display them on our screen. If you do not have OpenCV installed, you can do it easily with pip:įor this example, I have downloaded 50 images of Formula One cars saved in data/formula_one with files from 000000.jpg to 000049.jpg.įirst, let's create a Python file called gray.py and execute it in the terminal. 10 Useful Tips for Better Python Scriptsįor a starter, we will write a script that reads an RGB image and converts it to grayscale, a basic pre-processing step in computer vision. Its 118 interactive exercises will give you the opportunity to work with lists, nested lists, sets, tuples, and dictionaries.įor this article, we will start with a simple use case and improve our script as we implement the different tips for writing useful Python scripts. If you’re not familiar with them, consider taking our Python Data Structures course. A Python script is a set of instructions written in Python to ask the computer to perform some tasks.īefore we get started with the Python tips, make sure that you understand Python data structures first. In this article, I will give you some Python tips and tricks alongside script examples to help you make the best out of Python.įirst, let's define what a Python script is.

writing scripts in python

Would you like some helpful tips for Python scripts? Read on to get some advice from a pro!













Writing scripts in python