Created
August 4, 2016 11:53
-
-
Save mintisan/7457dd81b4ecde1a725877a1662172ee to your computer and use it in GitHub Desktop.
convert all bmp(or png, jpg) image in current direcory recursively to raw image Raw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# @author linjinhui | |
# @time 2016/08/04 | |
# @version 1.0 | |
import numpy as np | |
from PIL import Image | |
import os | |
import re | |
from tqdm import tqdm | |
total_raw_num = 0 | |
total_bmp_num = 0 | |
raw_re = re.compile(r'.raw$') | |
bmp_re = re.compile(r'.bmp$') | |
def bmp2raw(fn): | |
a = Image.open(fn) | |
a = np.array(a) | |
a.tofile(fn[:-4] + '.raw') | |
for root, dirs, files in os.walk(os.getcwd(), topdown=True): | |
for file in files: | |
if raw_re.search(file) != None: | |
total_raw_num += 1 | |
if bmp_re.search(file) != None: | |
total_bmp_num += 1 | |
print (" raw_num is %d , bmp_num is %d " \ | |
%(total_raw_num, total_bmp_num )) | |
if total_bmp_num == total_raw_num: | |
print 'no new raw files need to be transfered' | |
exit(0) | |
print (" to_be_transfered_num = %d " %(total_bmp_num - total_raw_num)) | |
pbar = tqdm(total = total_bmp_num - total_raw_num) | |
for root, dirs, files in os.walk(os.getcwd(), topdown=True): | |
for file in files: | |
if bmp_re.search(file) is not None: | |
if os.path.isfile(os.path.join(root,file[:-4] + '.raw')) == False: | |
bmp2raw(os.path.join(root,file)) | |
pbar.update(1) | |
print '\n 100% completely' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment