Created
December 30, 2015 11:48
-
-
Save mieki256/8cb7f142ce56aa9e4dc7 to your computer and use it in GitHub Desktop.
インデックスカラー画像のパレット値をGIMP Palette(.gpl)に変換
This file contains 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
#!/usr/bin/env python | |
# -*- mode: python; coding: utf-8 -*- | |
# Last updated: <2015/12/30 20:23:02 +0900> | |
""" | |
dump GIMP Palette (.gpl) format from index color image | |
by mieki256 | |
License: CC0 / Public Domain | |
""" | |
import sys | |
import os | |
from PIL import Image | |
def dump_palette(fn, file_output_enable=False): | |
im = Image.open(fn) | |
bsname = os.path.basename(fn) | |
title, ext = os.path.splitext(bsname) | |
path, ext = os.path.splitext(fn) | |
if im.mode != "P": | |
print "%s is not index color image." % (bsname) | |
return | |
value_mode, paldata = im.palette.getdata() | |
pal = list(paldata) | |
gpl_list = [] | |
cnt = 0 | |
for i in range(0, len(pal), 3): | |
r = ord(pal[i]) | |
g = ord(pal[i + 1]) | |
b = ord(pal[i + 2]) | |
name = "#%02x%02x%02x" % (r, g, b) | |
gpl_list.append("%3d %3d %3d\t%s # %d" % (r, g, b, name, cnt)) | |
cnt += 1 | |
if len(gpl_list) > 0: | |
r = [] | |
r.append("GIMP Palette") | |
r.append("Name: %s" % (title)) | |
r.append("Columns: 16") | |
r.append("#") | |
for s in gpl_list: | |
r.append(s) | |
gpl_fn = path + ".gpl" | |
if file_output_enable: | |
f = open(gpl_fn, "w") | |
f.write("\n".join(r) + "\n") | |
f.close() | |
print "Output %s" % (gpl_fn) | |
else: | |
for s in r: | |
print s | |
def main(): | |
argv = sys.argv | |
if len(argv) != 2: | |
print "usage:" | |
print " python %s INPUT.png" % (os.path.basename(__file__)) | |
print "INPUT.png is index color image file." | |
sys.exit() | |
# for i, v in enumerate(argv): | |
# print "%d %s" % (i, v) | |
fn = argv[1] | |
if os.path.exists(fn): | |
dump_palette(argv[1]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
python image2gpl.py INPUT.png
INPUT.png : index color image.