def __init__(self, filename, aug=False, transform=None): # For now it just reads the h5 file # Will eventually add path functionality if filename.endswith('h5'): f = h5py.File(filename, "r") self.images = f["images"][()] self.category = f["category"][()] self.category_names = f["category_names"][()] f.close() else: self.root_path = '/'.join(filename.split('/')[:-1]) + '/images' self.file_names = pd.read_csv(filename, header=None, names=['ims']) self.file_names = self.file_names.apply( lambda x: x + '.jpg' if not x.ims.endswith('.jpg') else x, axis=1) self.category_names = np.array( list( unique_everseen( self.file_names.apply(lambda x: x.ims.split('/')[0], axis=1).tolist()))) self.file_names = self.file_names.ims.values for file in self.file_names: piexif.remove(os.path.join(self.root_path, file)) self.transform = transform self.aug = aug
def retag_date(file): filename = os.path.basename(file) exif_dict = piexif.load(file) if piexif.ImageIFD.DateTime not in exif_dict['0th']: date_file_name = try_parsing_date(file) if date_file_name is not None: date_b = date_file_name.strftime("%Y:%m:%d %H:%M:%S") exif_dict['0th'][piexif.ImageIFD.DateTime] = date_b exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = date_b exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = date_b exif_bytes = piexif.dump(exif_dict) piexif.insert(exif_bytes, file) global MODIFIED_FILES MODIFIED_FILES = MODIFIED_FILES + 1 else: try: #catch dates not well formated old_date_b = exif_dict['0th'][piexif.ImageIFD.DateTime] old_date = datetime.strptime(old_date_b.decode("utf-8"), '%Y:%m:%d %H:%M:%S') except ValueError: piexif.remove(file) global INCONSISTENT_EXIF INCONSISTENT_EXIF = INCONSISTENT_EXIF + 1 print('Invalid date format in EXIF found for ' + file) retag_date(file) pass return True
def writeEXIF(self, file_paths, values): newexif = self.exif.copy() # UPDATE EXIF DATA WITH GPS DATA if values: del newexif["GPS"] gps = {} if "position_lat" in values: latitude = self.semicircles2dd(int(values["position_lat"][0])) longitude = self.semicircles2dd(int(values["position_long"][0])) gps[piexif.GPSIFD.GPSLatitudeRef] = 'N' if latitude > 0 else 'S' gps[piexif.GPSIFD.GPSLongitudeRef] = 'E' if longitude > 0 else 'W' gps[piexif.GPSIFD.GPSLatitude] = self.dd2dms(abs(latitude)) gps[piexif.GPSIFD.GPSLongitude] = self.dd2dms(abs(longitude)) if "enhanced_altitude" in values: elev = values["enhanced_altitude"][0] gps[piexif.GPSIFD.GPSAltitudeRef] = 0 if elev >= 0 else 1 gps[piexif.GPSIFD.GPSAltitude] = (abs(int(elev*1000000)),10000000) newexif["GPS"] = gps else: del newexif["GPS"] # STRIP ANY EXISTING EXIF DATA for file_path in file_paths: piexif.remove(file_path) # CONVERT EXIF DICT TO BYTES exif_bytes = piexif.dump(newexif) # INSERT EXIF DATA INTO IMAGE FILE for file_path in file_paths: piexif.insert(exif_bytes, file_path)
def remove_all_exif_data(dir_path): ''' remove currept exif warning. ''' file_list = find_all_files_sub(dir_path) for file in file_list: piexif.remove(file) # first to clear out all the exif data
def __getitem__(self, index): assert index <= len(self), 'index range error' img_name = self.paths_index[index][0] try: img = Image.open(img_name) except ValueError: print('\t \t [Removing XIF infos from image ...] ') x = None piexif.remove(img.__getexif(), x) img = Image.frombytes(x) if img.mode == 'L': img = img.convert('RGB') img = np.asarray(img) gt_dmap = np.load(os.path.join(self.paths_index[index][1])) # gt_dmap=(gt_dmap-np.min(gt_dmap))/(np.max(gt_dmap)-np.min(gt_dmap)) gt_dmap = gt_dmap[np.newaxis, :, :] # print(np.min(gt_dmap),np.max(gt_dmap)) # img_tensor=torch.tensor(img,dtype=torch.float).permute((2,0,1)) img_tensor = transforms.Compose([ transforms.ToTensor(), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip() ])(img) # gt_dmap_tensor=transforms.Compose([transforms.ToTensor()])(gt_dmap) # img_tensor=torch.from_numpy(img).permute((2,0,1)) gt_dmap_tensor = torch.tensor(gt_dmap, dtype=torch.float) del img, gt_dmap return img_tensor.detach(), gt_dmap_tensor.detach()
def upload_from_url(name, url): print('upload from url') x = requests.get(url) print('got content') tempname = name.replace("/", "_") with open(tempname, "wb") as file: for chunk in r.iter_content(1024): f.write(chunk) if tempname.split('.')[-1] in ['jpg', 'jpeg']: piexif.remove(tempname) S3.upload_file(tempname, Bucket=BUCKET, Key=name, ExtraArgs={ 'ACL': 'public-read', "ContentType": "image/png", "StorageClass": "INTELLIGENT_TIERING" }) remove(tempname)
def upload_image(): if request.method == 'POST': img_data = request.files['src'] filename = request.form['date'] location = request.form['location'] full_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) img_data.save(full_path) # resize the image to fit from PIL import Image import piexif basewidth = 480 img = Image.open(full_path) wpercent = (basewidth / float(img.size[0])) hsize = int((float(img.size[1]) * float(wpercent))) img = img.resize((basewidth, hsize), Image.ANTIALIAS) img.save(full_path) # update metadata to store location piexif.remove(full_path) exif_dict = piexif.load(full_path) exif_dict['Exif'] = {piexif.ExifIFD.UserComment: str(location)} exif_bytes = piexif.dump(exif_dict) piexif.insert(exif_bytes, full_path) return 'SUCCESS'
def remove_exif(filename): image = Image.open(filename) if "exif" in image.info: exif_dict = piexif.load(image.info["exif"]) # rotate accordingly if piexif.ImageIFD.Orientation in exif_dict["0th"]: orientation = exif_dict["0th"].pop(piexif.ImageIFD.Orientation) if orientation == 2: image = image.transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 3: image = image.rotate(180) elif orientation == 4: image = image.rotate(180).transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 5: image = image.rotate(-90, expand=True).transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 6: image = image.rotate(-90, expand=True) elif orientation == 7: image = image.rotate(90, expand=True).transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 8: image = image.rotate(90, expand=True) newfilename = filename.split('.')[0] + "_1" + '.' + filename.split('.')[1] image.save(newfilename, quality=95) piexif.remove(newfilename) else: #TODO add information on the interface for the user to understand print("Image skipped")
def strip_metadata(filename): img1 = Image.open(filename) if "exif" in img1.info: exif_dict = piexif.load(img1.info["exif"]) if piexif.ImageIFD.Orientation in exif_dict["0th"]: orientation = exif_dict["0th"].pop(piexif.ImageIFD.Orientation) exif_bytes = piexif.dump(exif_dict) piexif.remove(filename) img = Image.open(filename) if orientation == 2: img = img.transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 3: img = img.rotate(180) elif orientation == 4: img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 5: img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 6: img = img.rotate(-90) elif orientation == 7: img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT) elif orientation == 8: img = img.rotate(90) return img
def load_data(data_directory): directories = [ d for d in os.listdir(data_directory) if os.path.isdir(os.path.join(data_directory, d)) ] label_converters = [] labels = [] images = [] i = 0 while i < SAMPLE_TYPES: d = directories[i] ct = 0 label_converters.append(d) label_directory = os.path.join(data_directory, d) file_names = [ os.path.join(label_directory, f) for f in os.listdir(label_directory) if f.endswith(".jpg") ] while ct < len(file_names) and ct < SAMPLES_END: if ct < SAMPLES_BEGIN: ct = ct + 1 continue f = file_names[ct] piexif.remove(f) images.append(skimage.data.imread(f)) labels.append(i) ct = ct + 1 i = i + 1 print("Finished directory ", i) return images, labels, label_converters
def fix_missing_exif(directories, v=True, prog_bar=None): """remove piexifs from images takes a list of keras image generators note that these are directory iterators so you must call flow_from_directory method on the generators first. Args: image_generators (TYPE): Description v (bool, optional): verbose opt prog_bar (None, optional): if provided uses a progress bar such as tqdm """ try: import piexif except ImportError: print('You must install the piexif library "pip install piexif"') for d in directories: filenames =os.listdir(d) if v: print('removing exifs from {} images at {}...'.format(len(filenames), d)) if prog_bar: filenames = prog_bar(filenames) for imfile in filenames: p = os.path.join(d, imfile) try: piexif.remove(p) except Exception: print('caught error InvalidImageDataError for file at {} ignoring...') pass if v: print('complete!')
def clean_wildfish(path): # scan the image folder to clean each .jpg, jpeg file count = 0 for part in path: files = os.listdir(part) for f in files: image_file = os.path.join(part, f) if f.endswith('.jpg') or f.endswith('.JPG') or f.endswith( '.jpeg') or f.endswith('.JPEG'): if f.endswith('.jpeg') or f.endswith('.JPEG'): piexif.remove(image_file) # following is the alternative approach try: image = Image.open(image_file) if image.mode != 'RGB': image = image.convert("RGB") image.save(image_file) except: # delete this file print('corrupted file deleted: ', image_file) os.remove(image_file) continue count += 1 if count % 1000 == 0: print(count, 'images processed!')
def load_data(data_directory): directories = [ d for d in os.listdir(data_directory) if os.path.isdir(os.path.join(data_directory, d)) ] label_converters = [] labels = [] images = [] i = 0 for d in directories: ct = 0 label_converters.append(d) label_directory = os.path.join(data_directory, d) file_names = [ os.path.join(label_directory, f) for f in os.listdir(label_directory) if f.endswith(".jpg") ] for f in file_names: piexif.remove(f) images.append(skimage.data.imread(f)) labels.append(i) ct = ct + 1 if ct > SAMPLES_PER_TYPE: break i = i + 1 print("Finished directory ", i) return images, labels, label_converters
def delete_exif(): root.initial_photo = filedialog.askopenfilename( initialdir="/", title="Select file", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))) piexif.remove(root.initial_photo) print("Done")
def check_image_format_convert_2(arg): ''' 1. check image format and convert images in png to jpeg. 2. check image for tfrecord. ''' file_list, check_dir = arg img_format = ['JPEG'] for file in file_list: try: try: img = PIL.Image.open(file) except: img = cv2.imread(file) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) im_pil = PIL.Image.fromarray(img) im_pil.save(file) img = PIL.Image.open(file) # check img mode if img.format in img_format: piexif.remove(file) # clear out all the exif data else: new_img = os.path.abspath(file) img.convert('RGB').save(new_img, "JPEG") piexif.remove(new_img) # clear out all the exif data # check_image_tf_format(file) except: file_name = os.path.basename(file) os.rename(os.path.abspath(file), os.path.join(check_dir, file_name))
def checkImage(): # read data from 'cap_json_path' with open(cap_json_path, 'r', encoding='utf-8') as f: data = json.load(f) f.close() print("Checking and fixing images...") # recognize 'UserWarning' as error, for finding images with corrupt EXIF info warnings.filterwarnings("error", category=UserWarning) for cnt, imgID in enumerate(tqdm(data, desc='Checked images: ')): image_path = os.path.join(image_dir_path, imgID) while True: try: # verify that it is in fact an validate image img = Image.open(image_path).resize((256, 256)) # image corrupt except (IOError, SyntaxError, ValueError) as e: print('Bad image:', imgID) # download it again os.remove(image_path) getImage(data[imgID]['image_url'], imgID) # EXIF info corrupt, remove EXIF info except warning as e: print('Bad EXIF info:', imgID) piexif.remove(image_path) else: # validate image, go to the next break
def upload_file(): if syndbb.request.method == 'POST': image_types = [".jpg", ".jpeg", ".jpe"] if 'logged_in' in syndbb.session: userid = check_session_by_id(str(syndbb.session['logged_in'])) uploader = syndbb.request.form['uploader'] if 'anonymous' in syndbb.request.form: anonymous = 1 else: anonymous = 0 if 'timedelete' in syndbb.request.form: timedelete = 1 else: timedelete = 0 if userid: user = d2_user.query.filter_by(user_id=userid).first() if anonymous: uploadfolder = syndbb.app.static_folder + "/data/uploads/" + d2_hash( user.username + user.password)[:10] + "/" else: uploadfolder = syndbb.app.static_folder + "/data/uploads/" + user.username + "/" if not syndbb.os.path.exists(uploadfolder): syndbb.os.makedirs(uploadfolder) if 'file' not in syndbb.request.files: syndbb.flash('No file selected.', 'danger') return syndbb.redirect(syndbb.url_for(uploader)) file = syndbb.request.files['file'] if file.filename == '': syndbb.flash('No file selected.', 'danger') return syndbb.redirect(syndbb.url_for(uploader)) if file: filename = secure_filename(file.filename) extension = syndbb.os.path.splitext(filename)[1] newname = ''.join( random.sample( "-_" + string.ascii_uppercase + string.ascii_lowercase + string.digits, 20)) + extension file.save(syndbb.os.path.join(uploadfolder, newname)) if extension in image_types: piexif.remove(uploadfolder + newname) if uploader == 'upload_simple': return "/upload/simple/?file=" + newname else: syndbb.flash('File uploaded successfully.', 'success') syndbb.cache.delete_memoized( syndbb.views.upload.get_user_files) if anonymous: fpath = d2_hash(user.username + user.password)[:10] + "/" + newname else: fpath = user.username + "/" + newname return syndbb.redirect('/upload/view?file=' + fpath)
def delete_metadata(full_path_to_img): """ This function used for remove metadata only from documents, if you send image 'as image' Telegram automatically removes all metadata at sending. This function removes all metadata via 'piexif' lib, saved image in '/app' folder, and after that move it to 'documents' folder. :param full_path_to_img: path to folder with documents e.g.'documents/image.jpg' """ piexif.remove(full_path_to_img, "clean_image.jpg") move("clean_image.jpg", "documents/clean_image.jpg")
def copy_random_images(source, destination, num = 50, copier = get_random_images): images = copier(source, num) for i in images: f = path(i) try: piexif.remove(i) except (IOError, piexif.InvalidImageDataError, UnboundLocalError): pass f.copy(destination + "/" + f.namebase + str(random.randint(1,1000000000)) + f.ext)
def queue_folder(myblog, myblogname, folder, tags=[]): myfolder = path(folder) for f in myfolder.files(): if isimage(f) == True: try: piexif.remove(f) except (piexif.InvalidImageDataError, UnboundLocalError): pass myblog.create_photo(myblogname, state="queue", tags=tags, data=str(f))
def test_04_remove_dir(self): target = "../samples" directory = os.fsencode(target) for file in os.listdir(directory): filename = os.fsdecode(file) if filename.endswith(".jpg") or filename.endswith(".jpeg"): piexif.remove(target + '/' + filename) for _ in os.listdir(directory): for i in ("0th", "Exif", "GPS", "Interop", "1st"): exif_dict = piexif.load(target + '/' + filename) self.assertEqual(exif_dict[i], {})
def main(): i = 0 for filename in os.listdir("test"): dst = f"IMGA{i}.jpg" src = 'test/' + filename dst = 'test/' + dst piexif.remove(src) os.rename(src, dst) setFileDates(dst) print(f"File name '{src}' has been changed to '{dst}'.") i += 1
def set_metadata(file, keywords='', authors='', comment=''): try: exif = piexif.load(file) except piexif._exceptions.InvalidImageDataError: return exif["0th"][piexif.ImageIFD.XPKeywords] = exif_str_encode(keywords) exif["0th"][piexif.ImageIFD.XPAuthor] = exif_str_encode(authors) exif["0th"][piexif.ImageIFD.XPComment] = exif_str_encode(comment) piexif.remove(file) piexif.insert(piexif.dump(exif), file)
def get_image(image_name, raw_images_folder, save_images_folder): print(image_name) if image_name == '.DS_Store': return 0 image_path = os.path.join(raw_images_folder, '%s.jpg' % (image_name)) #本次原始图片jpg路径 save_file_path = os.path.join(save_images_folder, '%s.jpg' % (image_name)) #本次保存图片jpg路径 image = cv2.imread(image_path) cv2.imwrite(save_file_path, image) piexif.remove(save_file_path)
def remove_exif_data(self, train_path, test_path, dir): _, _, train_images = next(os.walk(train_path)) for img in train_images: try: piexif.remove(train_path + dir + img) except: pass _, _, test_images = next(os.walk(test_path)) for img in test_images: try: piexif.remove(test_path + dir + img) except: pass
def remove_exif_data(src_folder): _, _, cat_images = next(os.walk(src_folder + 'Cat/')) for img in cat_images: try: piexif.remove(src_folder + 'Cat/' + img) except: pass _, _, dog_images = next(os.walk(src_folder + 'Dog/')) for img in dog_images: try: piexif.remove(src_folder + 'Dog/' + img) except: pass
def upload_file_external(): if syndbb.request.method == 'POST': image_types = [".jpg", ".jpeg", ".jpe"] username = syndbb.request.form['username'] if 'auth' in syndbb.request.form: password = syndbb.request.form['auth'] elif 'password' in syndbb.request.form: password = syndbb.request.form['password'] else: return "No password set." user = d2_user.query.filter_by(username=username).filter_by( upload_auth=password).first() if user: # ban_check = check_ban_by_id(user.user_id) # if ban_check: # return "This user or IP address is banned." uploadfolder = syndbb.app.static_folder + "/data/uploads/" + user.username + "/" if not syndbb.os.path.exists(uploadfolder): syndbb.os.makedirs(uploadfolder) if 'file' not in syndbb.request.files: return "No file selected." file = syndbb.request.files['file'] if file.filename == '': return "No file selected." if file: filename = secure_filename(file.filename) extension = syndbb.os.path.splitext(filename)[1] newname = ''.join( random.sample( "-_" + string.ascii_uppercase + string.ascii_lowercase + string.digits, 20)) + extension file.save(syndbb.os.path.join(uploadfolder, newname)) if extension in image_types: piexif.remove(uploadfolder + newname) uploadurl = user.upload_url if uploadurl == "local": uploadurl = cdn_path( ) + "/data/uploads/" + user.username + "/" + newname else: uploadurl = "https://" + uploadurl + "/" + user.username + "/" + newname syndbb.cache.delete_memoized( syndbb.views.upload.get_user_files) return uploadurl else: return "Invalid details or not logged in." else: return "Invalid request, must be POST."
def task_proc(task_list, fpath_list): ''' :param task_list: :param fpath_list: :return: ''' for img_path in fpath_list: # check if the image file is valid if '1' in task_list: im = Image.open(img_path) try: im.load() except: print('removing the corrupted image file: {}'.format(img_path)) os.system('rm {}'.format(img_path)) # remove EXIF info from the img file if '2' in task_list: try: piexif.remove(img_path) except: print('piexif raise exceptions when processing {}'.format( img_path)) # convert image to RGB and 3-channel mode if '3' in task_list: saving_flag = False pil_im = Image.open(img_path) if pil_im.mode != "RGB": pil_im = pil_im.convert("RGB") saving_flag = True print("RGB issue: %s" % img_path) im = np.array(pil_im) if len(im.shape) < 2: im = im[0] pil_im = Image.fromarray(im) pil_im.save(img_path, "JPEG") print("too few channel: %s" % img_path) elif len(im.shape) == 3 and im.shape[2] >= 4: im = im[:, :, :3] pil_im = Image.fromarray(im) pil_im.save(img_path, "JPEG") print("too much channel: %s" % img_path) else: if saving_flag: pil_im.save(img_path, "JPEG")
def test_remove(self): piexif.remove(INPUT_FILE1, "remove.jpg") exif_dict = piexif.load("remove.jpg") none_dict = {"0th":{}, "Exif":{}, "GPS":{}, "Interop":{}, "1st":{}, "thumbnail":None} self.assertEqual(exif_dict, none_dict) piexif.remove("remove.jpg") exif_dict = piexif.load("remove.jpg") self.assertEqual(exif_dict, none_dict) os.remove("remove.jpg")
def test_remove2(self): with open(INPUT_FILE1, "rb") as f: data = f.read() with open("remove2.jpg", "wb+") as f: f.write(data) piexif.remove("remove2.jpg") exif_dict = piexif.load("remove2.jpg") none_dict = {"0th":{}, "Exif":{}, "GPS":{}, "Interop":{}, "1st":{}, "thumbnail":None} self.assertEqual(exif_dict, none_dict) os.remove("remove2.jpg")
def remove_exif(dir, phase): dir = os.path.expanduser(dir) for target in sorted(os.listdir(dir)): d = os.path.join(dir, target) d = os.path.join(d, phase) if not os.path.isdir(d): continue for root, _, fnames in sorted(os.walk(d)): for fname in sorted(fnames): if not is_image_file(fname): continue path = os.path.join(root, fname) try: piexif.remove(path) except: print("Failed to remove exif for: " + path)
def test_remove_m(self): """'remove' on memory. """ o = io.BytesIO() with self.assertRaises(ValueError): piexif.remove(I1) piexif.remove(I1, o) exif_dict = piexif.load(o.getvalue()) none_dict = {"0th":{}, "Exif":{}, "GPS":{}, "Interop":{}, "1st":{}, "thumbnail":None} self.assertEqual(exif_dict, none_dict) Image.open(o).close()
#!/usr/bin/env python import sys import piexif from PIL import Image def isJPEG(filename): try: img = Image.open(filename) return img.format == 'JPEG' except IOError: return False usageString = """ Usage: ./remove-exif.py name.jpg ./remove-exif.py *.jpg """ if len(sys.argv) == 1: print(usageString) for arg in sys.argv[1:]: if isJPEG(arg) == True: piexif.remove(arg) print(arg + ' -> has been removed Exif.') else: print(arg + ' -> is not a JPEG image!')
def remove_sample(): piexif.remove(os.path.join("tests", "images", "01.jpg"), "remove_sample.jpg")