1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#!/usr/bin/env python
# coding: utf-8
# Copyright © Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from PIL import Image
def premultiply(image):
pixels = image.load()
for i in range(image.size[0]):
for j in range(image.size[1]):
orig = pixels[i,j]
m = orig[3] / 255.0
pixels[i,j] = (int(orig[0] * m) , int(orig[1] * m), int(orig[2] * m), orig[3])
def tocstring(data):
result = ''
line_chars = 0
line_limit = 80
for c in data:
if line_chars == 0:
result += ' "'
s = '\\%o' % c
result += s
line_chars += len(s)
if line_chars >= line_limit:
result += '"\n'
line_chars = 0
if line_chars != 0:
result += '"'
return result
def bytes_per_pixel(image):
if image.mode == 'RGBA':
return 4
elif image.mode == 'RGB':
return 3
else:
raise "Unsupported image mode %s" % image.mode
def export(image, variable_name):
image_info = (image.size[0], image.size[1], bytes_per_pixel(image))
print("static const struct {")
print(" unsigned int width;")
print(" unsigned int height;")
print(" unsigned int bytes_per_pixel; /* 3:RGB, 4:RGBA */")
print(" unsigned char pixel_data[%d * %d * %d + 1];" % image_info)
print("} %s = {" % variable_name)
print(" %d, %d, %d," % image_info)
print(tocstring(image.tobytes()))
print("};")
def show_usage():
print("Usage: ./png2header.py PNGFILE VARNAME > HEADER_FILE", file=sys.stderr)
print("Convert a PNG image to an embeddable C/C++ header file", file=sys.stderr)
if len(sys.argv) < 3:
show_usage()
sys.exit(1)
image_filename = sys.argv[1]
variable_name = sys.argv[2]
image = Image.open(image_filename)
if image.mode == 'RGBA':
premultiply(image)
export(image, variable_name)
|