changeset 2050:dbbf0100f249

Update img2tyles to use Python 3
author Michael Pavone <pavone@retrodev.com>
date Fri, 31 Dec 2021 12:27:14 -0800
parents 460e14497120
children 97bfb6089274
files img2tiles.py
diffstat 1 files changed, 36 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/img2tiles.py	Tue Sep 21 23:03:18 2021 -0700
+++ b/img2tiles.py	Fri Dec 31 12:27:14 2021 -0800
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 from PIL import Image
 
 def gchannel(Val):
@@ -68,30 +68,30 @@
 	glist = [(gencolors[color][0] * gencolors[color][1], color) for color in gencolors]
 	glist.sort()
 	glist.reverse()
-	
+
 	return glist
 
 def make_palette(im, trans_thresh, max_global, max_line):
 	pixels = im.getdata()
 	(width, height) = im.size
-	colors = get_color_info(im, pixels, xrange(0, height * width), trans_thresh)
-	print len(colors), 'distinct 9-bit colors in image'
+	colors = get_color_info(im, pixels, range(0, height * width), trans_thresh)
+	print(len(colors), 'distinct 9-bit colors in image')
 	glob_pal = {}
-	print 'Static Palette:'
+	print('Static Palette:')
 	while len(glob_pal) < max_global and len(colors):
 		idx = len(glob_pal)
 		(count, color) = colors[0]
-		print str(idx) + ':', color
+		print(str(idx) + ':', color)
 		glob_pal[color] = idx
-		colors = get_color_info_both(im, pixels, xrange(0, height * width), trans_thresh, glob_pal)
+		colors = get_color_info_both(im, pixels, range(0, height * width), trans_thresh, glob_pal)
 	line_pals = []
 	if max_global < len(colors):
-		for line in xrange(0, height):
+		for line in range(0, height):
 			linestart = line * width
 			if len(glob_pal):
-				linecolors = get_color_info_both(im, pixels, xrange(linestart, linestart+width), trans_thresh, glob_pal)
+				linecolors = get_color_info_both(im, pixels, range(linestart, linestart+width), trans_thresh, glob_pal)
 			else:
-				linecolors = get_color_info(im, pixels, xrange(linestart, linestart+width), trans_thresh)
+				linecolors = get_color_info(im, pixels, range(linestart, linestart+width), trans_thresh)
 			line_pal = {}
 			while len(line_pal) < max_line and len(linecolors):
 				(score, color) = linecolors[0]
@@ -100,8 +100,8 @@
 					combo = dict(glob_pal)
 					for color in line_pal:
 						combo[color] = line_pal[color]
-					linecolors = get_color_info_both(im, pixels, xrange(linestart, linestart+width), trans_thresh, combo)
-			#for idx in xrange(0, min(max_line, len(linecolors))):
+					linecolors = get_color_info_both(im, pixels, range(linestart, linestart+width), trans_thresh, combo)
+			#for idx in range(0, min(max_line, len(linecolors))):
 			#	(count, color) = linecolors[idx]
 			#	line_pal[color] = idx + max_global
 			line_pals.append(line_pal)
@@ -116,7 +116,7 @@
 	bestdist = color_dist((0,0,0), (15, 15, 15))
 	bestpalidx = 0
 	bestcolor = (0,0,0)
-	for i in xrange(0, len(pals)):
+	for i in range(0, len(pals)):
 		pal = pals[i]
 		for cur in pal:
 			curdist = color_dist(gpixel, cur)
@@ -142,7 +142,7 @@
 			x = 0
 			y += 1
 			if width % 8 and not chunky:
-				for i in xrange(0, 8-(width%8)):
+				for i in range(0, 8-(width%8)):
 					gpixels.append(0)
 		gpixel = get_gcolor(im, trans_thresh, color=pixel)
 		if type(gpixel) == tuple:
@@ -158,12 +158,12 @@
 			gpixels.append(gpixel)
 		x += 1
 	if width % 8 and not chunky:
-		for i in xrange(0, 8-(width%8)):
+		for i in range(0, 8-(width%8)):
 			gpixels.append(0)
 		width += 8-(width%8)
 	if height % 8 and not chunky:
-		for y in xrange(0, 8-(height%8)):
-			for x in xrange(0, width):
+		for y in range(0, 8-(height%8)):
+			for x in range(0, width):
 				gpixels.append(0)
 		height += 8-(height%8)
 
@@ -183,37 +183,37 @@
 		for pixel in pixels:
 			b.append(pixel)
 	else:
-		cwidth = width/8
-		cheight = height/tile_height
-		words = len(pixels)/4
+		cwidth = width//8
+		cheight = height//tile_height
+		words = len(pixels)//4
 		if not raw:
 			appendword(b, words)
 			appendword(b, cwidth)
 			appendword(b, cheight)
 
 		if sprite_order:
-			for cx in xrange(0, cwidth):
+			for cx in range(0, cwidth):
 				xstart = cx * 8
-				for cy in xrange(0, cheight):
+				for cy in range(0, cheight):
 					startoff = cy*tile_height*width + xstart
-					for row in xrange(0, tile_height):
+					for row in range(0, tile_height):
 						rowoff = startoff + row*width
-						for bytecol in xrange(0, 4):
+						for bytecol in range(0, 4):
 							boff = bytecol * 2 + rowoff
-							#print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight
-							#print 'pixels[boff]:', pixels[boff]
+							#print('boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight)
+							#print('pixels[boff]:', pixels[boff])
 							b.append(pixels[boff] << 4 | pixels[boff+1])
 		else:
-			for cy in xrange(0, cheight):
+			for cy in range(0, cheight):
 				ystart = cy*tile_height*width
-				for cx in xrange(0, cwidth):
+				for cx in range(0, cwidth):
 					startoff = (cx*8) + ystart
-					for row in xrange(0, tile_height):
+					for row in range(0, tile_height):
 						rowoff = startoff + row*width
-						for bytecol in xrange(0, 4):
+						for bytecol in range(0, 4):
 							boff = bytecol * 2 + rowoff
-							#print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight
-							#print 'pixels[boff]:', pixels[boff]
+							#print('boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight)
+							#print('pixels[boff]:', pixels[boff])
 							b.append(pixels[boff] << 4 | pixels[boff+1])
 	return b
 
@@ -247,7 +247,7 @@
 	options = {}
 	tile_height = 8
 	sprite_order = False
-	for i in xrange(1, len(argv)):
+	for i in range(1, len(argv)):
 		if argv[i].startswith('-'):
 			if argv[i] == '-r':
 				raw = True
@@ -262,7 +262,7 @@
 			elif argv[i] == '-s' or argv[i] == '--spec':
 				expect_option = 'specfile'
 			else:
-				print 'Unrecognized switch', argv[i]
+				print('Unrecognized switch', argv[i])
 				return
 		elif not expect_option is None:
 			options[expect_option] = argv[i]
@@ -270,7 +270,7 @@
 		else:
 			posargs.append(argv[i])
 	if len(posargs) < 2 and not ('specfile' in options and len(posargs) >= 1):
-		print "Usage: img2tiles.py [OPTIONS] infile outfile [STATIC_COLORS [DYNAMIC_COLORS]]"
+		print("Usage: img2tiles.py [OPTIONS] infile outfile [STATIC_COLORS [DYNAMIC_COLORS]]")
 		return
 	if 'specfile' in options:
 		props = open(options['specfile']).read().strip().split(',')
@@ -300,7 +300,7 @@
 		if len(posargs) > 3:
 			dynamic_colors = int(posargs[3])
 	if dynamic_colors + static_colors > 16:
-		print "No more than 16 combined dynamic and static colors are allowed"
+		print("No more than 16 combined dynamic and static colors are allowed")
 		return
 	im = Image.open(fname)
 	pal = make_palette(im, threshold, static_colors, dynamic_colors)