Created
November 7, 2015 10:07
-
-
Save mieki256/5a8d36e2526a8dc96721 to your computer and use it in GitHub Desktop.
fake anti jaggies - アンチジャギの実験
This file contains hidden or 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; Encoding: utf-8; coding: utf-8 -*- | |
# Last updated: <2015/11/07 17:23:31 +0900> | |
""" | |
fakeantijaggies | |
anti jaggies test | |
usage: python fakeantijaggies.py -i INPUT.png -o OUT.png -c 1 | |
use: Python 2.7.10 + Pillow | |
by mieki256 | |
License: CC0 | |
""" | |
__version__ = '0.0.1' | |
from optparse import OptionParser, OptionValueError | |
from PIL import Image | |
import sys | |
import os | |
def get_value(r0, v0, r1, v1, r2, v2): | |
v = int(float(r0) * v0 + float(r1) * v1 + float(r2) * v2) | |
if v < 0: | |
v = 0 | |
elif v > 255: | |
v = 255 | |
return v | |
def anti_jaggies_sub(im): | |
w, h = im.size | |
y = 0 | |
while y < h - 2: | |
x = 0 | |
while x < w - 2: | |
c0 = im.getpixel((x, y)) | |
c1 = im.getpixel((x + 1, y)) | |
if c0 != c1: | |
c2 = im.getpixel((x, y + 1)) | |
c3 = im.getpixel((x + 1, y + 1)) | |
if c0 != c2: | |
c = get_value(c0, 0.5, c1, 0.25, c2, 0.25) | |
im.putpixel((x, y), c) | |
elif c1 != c3: | |
c = get_value(c0, 0.25, c1, 0.5, c2, 0.25) | |
im.putpixel((x + 1, y), c) | |
x = x + 1 | |
x = x + 1 | |
y = y + 1 | |
return im | |
def anti_jaggies(imgfile, outfile, count, tcolor): | |
img = Image.open(imgfile) | |
if img.mode != "RGBA": | |
img = img.convert("RGBA") | |
w, h = img.size | |
if tcolor is not None: | |
ttr, ttg, ttb = tcolor # transparent color | |
for y in xrange(h): | |
for x in xrange(w): | |
r, g, b, a = img.getpixel((x, y)) | |
if r == ttr and g == ttg and b == ttb: | |
img.putpixel((x, y), (0, 0, 0, 0)) | |
for i in xrange(count): | |
cr, cg, cb, ca = img.split() | |
new_r = anti_jaggies_sub(cr) | |
new_g = anti_jaggies_sub(cg) | |
new_b = anti_jaggies_sub(cb) | |
new_a = anti_jaggies_sub(ca) | |
img = Image.merge("RGBA", (new_r, new_g, new_b, new_a)) | |
img.save(outfile) | |
def main(imgfile, outfile, count, tcolor): | |
anti_jaggies(imgfile, outfile, count, tcolor) | |
if __name__ == '__main__': | |
p = OptionParser(version=__version__) | |
p.add_option( | |
"-i", "--input", dest="imgfile", default=None, help="Input image file") | |
p.add_option( | |
"-o", "--output", dest="outfile", default=None, help="Output image file") | |
p.add_option( | |
"-c", "--count", type="int", dest="count", default=1, help="Count") | |
p.add_option("-n", "--nottransparent", action="store_false", | |
dest="tp_fg", help="disable transparent") | |
p.add_option("-t", "--transparentcolor", dest="tcolor", | |
default="255,255,255", help="transparent color [255,255,255]") | |
opts, args = p.parse_args() | |
if opts.imgfile is None: | |
p.error("require Input image file") | |
p.print_help() | |
sys.exit | |
elif opts.outfile is None: | |
root, ext = os.path.splitext(opts.imgfile) | |
opts.outfile = "%s_new%s" % (root, ext) | |
if opts.tp_fg is False: | |
tcol = None | |
else: | |
a = opts.tcolor.split(",") | |
tcol = (int(a[0]), int(a[1]), int(a[2])) | |
main(opts.imgfile, opts.outfile, opts.count, tcol) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment