Skip to content

Instantly share code, notes, and snippets.

@mintisan
Created August 4, 2016 11:54
Show Gist options
  • Save mintisan/1724b0c56218d6c10454d48bcaf78049 to your computer and use it in GitHub Desktop.
Save mintisan/1724b0c56218d6c10454d48bcaf78049 to your computer and use it in GitHub Desktop.
convert all raw images in current directory recursively to bmp(or png/jpg) image files
# -*- coding: utf-8 -*-
# @author linjinhui
# @time 2016/04/01
# @version 1.0
# How can I save an image with PIL?
# http://stackoverflow.com/questions/14452824/how-can-i-save-an-image-with-pil
import numpy as np
from PIL import Image
import os
import re
from tqdm import tqdm
raw = 64
col = 64
total_raw_num = 0
total_bmp_num = 0
raw_re = re.compile(r'.raw$')
bmp_re = re.compile(r'.bmp$')
def raw2bmp(fn):
fn_handler = open(fn,'rb')
a = np.fromfile(fn_handler,dtype='>u1')
img = Image.fromarray(a.reshape(col,row))
img.save(fn[:-4] + '.bmp')
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_raw_num -total_bmp_num))
pbar = tqdm(total = total_raw_num -total_bmp_num)
for root, dirs, files in os.walk(os.getcwd(), topdown=True):
for file in files:
if raw_re.search(file) is not None:
if os.path.isfile(os.path.join(root,file[:-4] + '.bmp')) == False:
raw2bmp(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