Best Python code snippet using avocado_python
camera_bitdepth_tests.py
Source:camera_bitdepth_tests.py
...26 ax = plt.gca()27 ax.set_yscale('log')28 plt.show()29@timeit30def _save_to_file(data: np.ndarray, fname) -> None:31 """Saves a 3D RGB numpy array to a png"""32 shifted_data = np.left_shift(data, 4)33 #write_png(fname, shifted_data, bitdepth=16)34 cv2.imwrite(fname, data,)35##@timeit36##def _save_to_file(data: np.ndarray, fname) -> None:37## data = np.reshape(data, (-1, data.shape[2]))38## png.from_array(data, 'RGB;16').save(fname)39##@timeit40##def _save_to_file(data: np.ndarray, fname) -> None:41## z=data42## print(data)43## # Use pypng to write z as a color PNG.44## with open(fname, 'wb') as f:45## writer = png.Writer(width=z.shape[0], height=z.shape[1], bitdepth=16)46## # Convert z to the Python list of lists expected by47## # the png writer.48## z2list = np.transpose(z, axes=((0, 2, 1)))49## #z2list = z2list.reshape(-1, z.shape[0]).tolist()50## z2list = z.tolist()51## print(z2list)52## writer.write(f, z2list)53 54@timeit55def _open_file(f) -> np.ndarray:56 #Tried Pillow/PIL, cv2, scipy, imageio57 """Loads an image file and returns the data as a 3d array"""58 #reader = png.Reader(f)59 #ny, nx, image_gen, *_ = reader.read()60 #data = np.array(list(map(np.uint16, image_gen)))61 #data = data.reshape(nx, ny, 3)62 63 data = cv2.imread(f, -1)64 65 shifted_data = np.right_shift(data, 4)66 67 return shifted_data68def _describe_array(arr: np.ndarray) -> None:69 """prints array properties"""70 print("Array shape: {}".format(np.shape(arr)))71 print("Array dtype: {}".format(arr.dtype))72 print("All bits or'd: {}".format(bin(_determine_bit_depth(arr))))73 print("Max: {}".format(np.max(arr)))74 print("Min: {}".format(np.min(arr)))75 print()76def _determine_bit_depth(numbers: np.ndarray) -> np.uint16:77 """Performs a bitwise or operator between all elements in the array and returns the result"""78 numbers_flat = np.ndarray.flatten(numbers)79 return np.bitwise_or.reduce(numbers_flat)80def _generate_toy_image() -> np.ndarray:81 """Generates a random color image"""82 shape = (1216, 1936, 3)83 #shape = (4, 5, 3)84 data = np.random.randint(low=0, high=2**16, size=np.prod(shape), dtype=np.uint16).reshape(shape)85 #make data 12 bit86 data = np.right_shift(data, 4)87 return data88def _test_save_and_load():89 #generate data to save90 toy_data = _generate_toy_image()91 _describe_array(toy_data)92 #setup file to save the data as93 f1 = os.path.join("./", "test1.png")94 f2 = os.path.join("./", "test2.png")95 #save the data96 _save_to_file(data=toy_data, fname=f1)97 #load the data98 toy_data_load = _open_file(f1)99 _describe_array(toy_data_load)100 print("Loaded and save arrays are:")101 if np.array_equal(toy_data, toy_data_load):102 print("Identical")103 else:104 print("Different")105 #save the data106 _save_to_file(data=toy_data_load, fname=f2)107 108def _np_load_save_benchmark():109 f = os.path.join("./", "test.npy")110 data = _aquire_image()111 @timeit112 def save():113 np.save(f, data, allow_pickle=False)114 @timeit115 def load():116 return(np.load(f))117 save()118 out = load()119 assert np.array_equal(data, out), "arrays are not the same"120 ...
FileHandler.py
Source:FileHandler.py
...4from typing import List5class FileHandler:6 @staticmethod7 def write_append_to_file(path: str, text: str) -> bool:8 return FileHandler._save_to_file(path, "a", text)9 10 @staticmethod11 def write_to_file(path: str, text: str) -> bool:12 return FileHandler._save_to_file(path, "w", text)13 @staticmethod14 def read_from_file(path: str) -> List[str]:15 return FileHandler._load_from_file_get_list(path, "r")16 @staticmethod17 def _save_to_file(path, mode, text) -> bool:18 try:19 with io.open(path, mode, encoding='utf8') as f:20 f.write(text)21 f.close()22 except:23 return False24 return True25 26 27 @staticmethod28 def _load_from_file_get_list(path, mode) -> List[str]:29 cont = []30 try:31 with io.open(path, mode, encoding='utf8') as f:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!