def test_convert_png_to_jpg(self): self.base_path = pathlib.Path(os.path.dirname(os.path.abspath(__file__))) self.jpg_dir = self.base_path.joinpath("TestImages").joinpath("JPGImages") try: os.remove(str(self.base_path.joinpath("TestImages/JPGImages/Mario.png"))) except Exception as e: print(e) try: os.remove(str(self.base_path.joinpath("TestImages/JPGImages/Dice.png"))) except Exception as e: print(e) self.png_one_loc = self.base_path.joinpath("TestImages/PNGImages/Dice.png") self.png_two_loc = self.base_path.joinpath("TestImages/PNGImages/Mario.png") self.copied_png_one_loc = self.base_path.joinpath("TestImages/JPGImages/Dice.png") self.copied_png_two_loc = self.base_path.joinpath("TestImages/JPGImages/Mario.png") self.jpg_one_loc = self.base_path.joinpath("TestImages/JPGImages/Dice.jpg") self.jpg_two_loc = self.base_path.joinpath("TestImages/JPGImages/Mario.jpg") if not verify_file_exists(str(self.copied_png_one_loc)): copy_file(str(self.png_one_loc), str(self.jpg_dir)) if not verify_file_exists(str(self.copied_png_two_loc)): copy_file(str(self.png_two_loc), str(self.jpg_dir)) convert_png_to_jpg(str(self.copied_png_one_loc)) convert_png_to_jpg(str(self.copied_png_two_loc)) self.assertTrue(verify_file_exists(str(self.jpg_one_loc))) self.assertTrue(verify_file_exists(str(self.jpg_two_loc)))
def copy_png_files_and_convert_to_jpg(png_folder, jpg_folder, gps_lats, gps_longs, gps_alts = [], sorting_lambda = lambda file_name: int(file_name.split('_')[1].replace('.png','').replace('.jpg',''))): '''Given a folder containing png files and a corresponding csv file containing lat, long, alt for images, will create a new folder in specified location, copy png files to jpg and write GPS details to exif. If a file in png_folder is not png it will be copied to to jpg folder but not converted.''' #check that jpg_folder exists if not verify_dir_exists(jpg_folder): print('Creating folder: {}'.format(jpg_folder)) os.makedirs(jpg_folder) else: if os.listdir(jpg_folder) != []: print('{} is not an empty directory, may accidentally overwrite images'.format(jpg_folder)) print(os.listdir(png_folder)) #first copy files from png_folder to jpg_folder if they are .png format for png_file in os.listdir(png_folder): try: copy_file(png_folder + png_file, jpg_folder) except Exception as e: #don't try and deal with exception (may change this) raise e #now convert files to jpg for unconverted_png_file in os.listdir(jpg_folder): try: convert_png_to_jpg(jpg_folder + unconverted_png_file) except Exception as e: #don't try and deal with exception (may change this) raise e #process images by adding gps exif data. Assume that gps_lats and gps_longs are valid counter = 0 for jpg_file in sorted(os.listdir(jpg_folder), key = sorting_lambda): write_exif_gps_data(jpg_folder + jpg_file, gps_lats[counter], gps_longs[counter], gps_alts[counter]) counter += 1
def process_image(from_file_location: str, to_dir_location: str, comment: str, tags: "list of strings", date: str, gps_lat: float, gps_long: float, gps_alt: float = 32): '''Given a raw png image and metadata about that image, copies it to a specified location in jpg format and writes metadata to exif''' copy_file(from_file_location, to_dir_location) file_name = os.path.basename(from_file_location) copied_png_location = pathlib.Path(to_dir_location).joinpath(file_name) print("converting {} to a jpg".format(str(copied_png_location))) jpg_location = convert_png_to_jpg(str(copied_png_location)) write_exif_gps_data(str(jpg_location), gps_lat, gps_long, gps_alt) write_exif_comment(str(jpg_location), comment) write_exif_date_original(str(jpg_location), str(date))