Skip to content

Commit

Permalink
Small formatting improvements and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
miawgogo committed Feb 10, 2019
1 parent 8b17bb4 commit 49e93eb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
31 changes: 31 additions & 0 deletions mBitImageConverter/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from PIL import ImageOps

def convertoGrayScaleMBITIMG(img, w, h, varname):
grayimg = ImageOps.grayscale(img)
px = grayimg.load()
var = 'MicroBitImage {}("'.format(varname)
for y in range(0, h):
for x in range(0, w):
pixelVal = px[x, y]
var+="{}".format(pixelVal)
if x == w-1:
var+="\\n"
else:
var+=(",")
var += '");'
return var

def convertoBlackWhiteMBITIMG(img, w, h, varname):
grayimg = ImageOps.grayscale(img)
px = grayimg.load()
var = 'MicroBitImage {}("'.format(varname)
for y in range(0, h):
for x in range(0, w):
pixelVal = px[x, y]
var+="{}".format(int(pixelVal>0))
if x == w-1:
var+="\\n"
else:
var+=(",")
var += '");'
return var
44 changes: 6 additions & 38 deletions mBitImageConverter/mBitImageConverter.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
#!/usr/bin/env python3
import argparse, os
from PIL import Image, ImageOps
from PIL import Image
from mBitImageConverter.functions import convertoBlackWhiteMBITIMG, convertoGrayScaleMBITIMG

parser = argparse.ArgumentParser(description='Coverts Images to MicroBitImage.')
parser.add_argument('files', metavar='FILES', type=Image.open, nargs='+', help='a list of images to be convered')
parser.add_argument('files', metavar='FILES', type=Image.open, nargs='+', help='a list of images to be convered', )
parser.add_argument('--BW', help='converts the images to black and white', action='store_true')
args=parser.parse_args()

def convertoGrayScaleMBITIMG(img, w, h,path):
grayimg = ImageOps.grayscale(img)
px = grayimg.load()
varname = os.path.basename(path).split(".")[0]
var = 'MicroBitImage {}("'.format(varname)
for y in range(0, h):
for x in range(0, w):
pixelVal = px[x, y]
var+="{}".format(pixelVal)
if x == w-1:
var+="\\n"
else:
var+=(",")
var += '");'
return var

def convertoBlackWhiteMBITIMG(img, w, h, path):
grayimg = ImageOps.grayscale(img)
px = grayimg.load()
varname = os.path.basename(path).split(".")[0]
var = 'MicroBitImage {}("'.format(varname)
for y in range(0, h):
for x in range(0, w):
pixelVal = px[x, y]
var+="{}".format(int(pixelVal>0))
if x == w-1:
var+="\\n"
else:
var+=(",")
var += '");'
return var


def main():
files = args.files

for photo in files:
img = photo
h = img.height
w = img.width
path = img.filename
varname = os.path.basename(img.filename).split(".")[0]
if args.BW:
var = convertoBlackWhiteMBITIMG(img,w,h,path)
var = convertoBlackWhiteMBITIMG(img,w,h,varname)
else:
var = convertoGrayScaleMBITIMG(img,w,h,path)
var = convertoGrayScaleMBITIMG(img,w,h,varname)
print(var)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='mBitImageConverter',
version='1.0',
version='1.1',
description='Converts any image format understood by PILLOW into a MicroBitImage',
author='Ioan Loosley',
author_email='legit.ioangogo@gmail.com',
Expand Down

0 comments on commit 49e93eb

Please sign in to comment.