Skip to content

Instantly share code, notes, and snippets.

@xylcbd xylcbd/show_image.py
Created Sep 18, 2018

Embed
What would you like to do?
#!/usr/local/bin/python
#coding: utf-8
import os
import sys
import fnmatch
import cv2
def files_in_dir(root, pattern):
all_files = []
for path, subdirs, files in os.walk(root):
for name in files:
if fnmatch.fnmatchcase(name, pattern):
all_files.append(os.path.join(path, name))
return all_files
def main(argv):
if len(argv) != 2:
print('usage:\n\t%s dir_path' % argv[0])
sys.exit(0)
dir_path = argv[1]
print('scaning %s ...' % dir_path)
files = files_in_dir(dir_path, "*.jpg") + files_in_dir(dir_path, "*.jpeg") + files_in_dir(dir_path, "*.png") + files_in_dir(dir_path, "*.bpm")
print('preview every image...')
for i, file_path in enumerate(files):
print('[%d/%d] %s' % (i+1, len(files), file_path))
img = cv2.imread(file_path)
if img is None:
print('%s is not an image' % file_path)
continue
h, w, c = img.shape
print('channels:%d width:%d height:%d' % (c, w, h))
max_size = 1000
if max(h, w) > max_size:
scale = float(max_size) / float(max(h, w))
dh = max(int(h * scale), 1)
dw = max(int(w * scale), 1)
img = cv2.resize(img, dsize=(dw,dh))
h, w, c = img.shape
print('image is too large, after scaled it, channels:%d width:%d height:%d' % (c, w, h))
cv2.imshow('image', img)
key = cv2.waitKey(0)
if key == 27:
break
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.