diff --git a/ASCII/plugin.py b/ASCII/plugin.py index e2b65e4..649603c 100644 --- a/ASCII/plugin.py +++ b/ASCII/plugin.py @@ -14,10 +14,13 @@ import supybot.callbacks as callbacks import supybot.ircmsgs as ircmsgs import os import requests -from PIL import Image, ImageFilter +from PIL import Image import numpy as np import sys, math from fake_useragent import UserAgent +from colormath.color_objects import sRGBColor, LabColor +from colormath.color_conversions import convert_color +from colormath.color_diff import delta_e_cie1976 try: from supybot.i18n import PluginInternationalization @@ -96,105 +99,130 @@ class ASCII(callbacks.Plugin): # get average return np.average(im.reshape(w*h)) - def getAverageC(self, pixel): + def getAverageC(self, pixel,speed): """ Given PIL Image, return average RGB value """ - pixel = pixel[:3] - colors = { - "16":(71,0,0), - "17":(71,33,0), - "18":(71,71,0), - "19":(50,71,0), - "20":(0,71,0), - "21":(0,71,44), - "22":(0,71,71), - "23":(0,39,71), - "24":(0,0,71), - "25":(46,0,71), - "26":(71,0,71), - "27":(71,0,42), - "28":(116,0,0), - "29":(116,58,0), - "30":(116,116,0), - "31":(81,116,0), - "32":(0,116,0), - "33":(0,116,73), - "34":(0,116,116), - "35":(0,64,116), - "36":(0,0,116), - "37":(75,0,116), - "38":(116,0,116), - "39":(116,0,69), - "40":(181,0,0), - "41":(181,99,0), - "42":(181,181,0), - "43":(125,181,0), - "44":(0,181,0), - "45":(0,181,113), - "46":(0,181,181), - "47":(0,99,181), - "48":(0,0,181), - "49":(117,0,181), - "50":(181,0,181), - "51":(181,0,107), - "52":(255,0,0), - "53":(255,140,0), - "54":(255,255,0), - "55":(178,255,0), - "56":(0,255,0), - "57":(0,255,160), - "58":(0,255,255), - "59":(0,140,255), - "60":(0,0,255), - "61":(165,0,255), - "62":(255,0,255), - "63":(255,0,152), - "64":(255,89,89), - "65":(255,180,89), - "66":(255,255,113), - "67":(207,255,96), - "68":(111,255,111), - "69":(101,255,201), - "70":(109,255,255), - "71":(89,180,255), - "72":(89,89,255), - "73":(196,89,255), - "74":(255,102,255), - "75":(255,89,188), - "76":(255,156,156), - "77":(255,211,156), - "78":(255,255,156), - "79":(226,255,156), - "80":(156,255,156), - "81":(156,255,219), - "82":(156,255,255), - "83":(156,211,255), - "84":(156,156,255), - "85":(220,156,255), - "86":(255,156,255), - "87":(255,148,211), - "88":(0,0,0), - "89":(19,19,19), - "90":(40,40,40), - "91":(54,54,54), - "92":(77,77,77), - "93":(101,101,101), - "94":(129,129,129), - "95":(159,159,159), - "96":(188,188,188), - "97":(226,226,226), - "98":(255,255,255)} - manhattan = lambda x,y : abs(x[0] - y[0]) + abs(x[1] - y[1]) + abs(x[2] - y[2]) - distances = {k: manhattan(v, pixel) for k, v in colors.items()} - color = min(distances, key=distances.get) - return color + speed = speed + ircColors = {LabColor(lab_l=1993.20515351498,lab_a=2324.0983231476234,lab_b=1950.0779087841693):16, + LabColor(lab_l=2302.159132332113,lab_a=1476.5998328806731,lab_b=2094.817440516089):17, + LabColor(lab_l=3267.109569734987,lab_a=-625.5719921685046,lab_b=2741.5824054551854):18, + LabColor(lab_l=3117.7906623358294,lab_a=-1524.3182007475084,lab_b=2563.4872768819705):19, + LabColor(lab_l=2994.225776415631,lab_a=-2500.8929369695743,lab_b=2413.738641368567):20, + LabColor(lab_l=3026.0461300232373,lab_a=-2106.053265646869,lab_b=1016.9116217498115):21, + LabColor(lab_l=3092.2778750318885,lab_a=-1395.2984432353226,lab_b=-410.2808083391245):22, + LabColor(lab_l=2082.37407522394,lab_a=439.80707297830793,lab_b=-1983.2971244240234):23, + LabColor(lab_l=1385.5715072995845,lab_a=2298.009119093381,lab_b=-3130.08523478244):24, + LabColor(lab_l=1761.7248957199076,lab_a=2495.1524432892943,lab_b=-2494.7963736847237):25, + LabColor(lab_l=2198.7916393341657,lab_a=2850.6336820262986,lab_b=-1765.3360075524952):26, + LabColor(lab_l=2055.801787008181,lab_a=2491.6677227532045,lab_b=-158.86279448684633):27, + LabColor(lab_l=2958.9498780747585,lab_a=3441.1996261238046,lab_b=2887.4025267278366):28, + LabColor(lab_l=3490.7754041570597,lab_a=1995.586399321347,lab_b=3144.023619818683):29, + LabColor(lab_l=4845.169302249827,lab_a=-926.259480557718,lab_b=4059.3516438937377):30, + LabColor(lab_l=4620.482019554244,lab_a=-2281.747228870049,lab_b=3791.325963678294):31, + LabColor(lab_l=4441.121161001645,lab_a=-3702.9723544655367,lab_b=3573.926467529447):32, + LabColor(lab_l=4489.963665823597,lab_a=-3098.035370892731,lab_b=1453.7194043799368):33, + LabColor(lab_l=4586.302989902044,lab_a=-2065.9627148173527,lab_b=-607.4864175067376):34, + LabColor(lab_l=3098.2589552331815,lab_a=634.8559351078204,lab_b=-2924.8401977939):35, + LabColor(lab_l=2059.250990402592,lab_a=3402.5703829703853,lab_b=-4634.592277094812):36, + LabColor(lab_l=2613.821127903904,lab_a=3692.817296863682,lab_b=-3697.953525308647):37, + LabColor(lab_l=3263.3535821224173,lab_a=4220.81951658537,lab_b=-2613.862567115958):38, + LabColor(lab_l=3052.7880497642973,lab_a=3692.3353415279757,lab_b=-255.20463970260963):39, + LabColor(lab_l=4230.166177007632,lab_a=4911.647610760525,lab_b=4121.209247509218):40, + LabColor(lab_l=5140.420692098141,lab_a=2469.29414128601,lab_b=4576.863521782283):41, + LabColor(lab_l=6922.379978784402,lab_a=-1322.0564509214903,lab_b=5793.940186325644):42, + LabColor(lab_l=6595.196756134379,lab_a=-3301.6293161796852,lab_b=5403.579861388848):43, + LabColor(lab_l=6345.679321104374,lab_a=-5285.277604778105,lab_b=5101.0895334333245):44, + LabColor(lab_l=6414.068737559316,lab_a=-4437.401859025286,lab_b=2114.577589536168):45, + LabColor(lab_l=6552.8983320653,lab_a=-2948.7626219415083,lab_b=-867.069491832234):46, + LabColor(lab_l=4407.084374518514,lab_a=955.4196746325267,lab_b=-4210.022191539629):47, + LabColor(lab_l=2946.0198408020497,lab_a=4856.511829505751,lab_b=-6614.98505109478):48, + LabColor(lab_l=3737.0822740921226,lab_a=5270.450895642849,lab_b=-5278.9196336943):49, + LabColor(lab_l=4664.64365234563,lab_a=6024.404378260297,lab_b=-3730.783804336487):50, + LabColor(lab_l=4362.152983533213,lab_a=5264.9896683827,lab_b=-330.4757953207826):51, + LabColor(lab_l=5569.417504595725,lab_a=6460.793430576633,lab_b=5421.048850085641):52, + LabColor(lab_l=6775.61797290782,lab_a=3226.238340029962,lab_b=6025.75867889489):53, + LabColor(lab_l=9110.762206548767,lab_a=-1739.0363295306556,lab_b=7621.363269416317):54, + LabColor(lab_l=8688.56571353183,lab_a=-4286.355755413659,lab_b=7117.728526067228):55, + LabColor(lab_l=8352.168733274038,lab_a=-6952.267249978224,lab_b=6709.985804801282):56, + LabColor(lab_l=8443.195089050523,lab_a=-5824.430407957365,lab_b=2749.492570275423):57, + LabColor(lab_l=8624.745133456714,lab_a=-3878.809655324538,lab_b=-1140.5453567984496):58, + LabColor(lab_l=5814.154106925882,lab_a=1229.6993153257815,lab_b=-5518.472254545321):59, + LabColor(lab_l=3880.248233844353,lab_a=6388.267687373855,lab_b=-8701.367717798912):60, + LabColor(lab_l=4922.735757149115,lab_a=6934.097154957253,lab_b=-6940.674046100774):61, + LabColor(lab_l=6140.930251611103,lab_a=7924.516438217041,lab_b=-4907.482255272506):62, + LabColor(lab_l=5746.387693687689,lab_a=6934.366712732735,lab_b=-492.7614873204078):63, + LabColor(lab_l=6074.110810805622,lab_a=5313.137095279206,lab_b=3103.1603383735187):64, + LabColor(lab_l=7549.6166232716405,lab_a=1638.3506351207516,lab_b=4766.116472071773):65, + LabColor(lab_l=9144.22544761627,lab_a=-1470.995605268513,lab_b=5615.164875231808):66, + LabColor(lab_l=8851.666121034279,lab_a=-3149.3613711592443,lab_b=5791.451685441684):67, + LabColor(lab_l=8500.557984917883,lab_a=-5550.555360677123,lab_b=4790.802527852141):68, + LabColor(lab_l=8594.087862835102,lab_a=-4460.502732282599,lab_b=1202.8053973127812):69, + LabColor(lab_l=8724.82598229055,lab_a=-3235.6496256362348,lab_b=-980.5575778174244):70, + LabColor(lab_l=6874.520048611572,lab_a=-289.5387261981774,lab_b=-3821.4400161526414):71, + LabColor(lab_l=4916.297192060128,lab_a=4307.513997705836,lab_b=-6974.795972582456):72, + LabColor(lab_l=5819.517276565197,lab_a=5772.740696787963,lab_b=-5464.249084289627):73, + LabColor(lab_l=6666.663989554589,lab_a=6410.850719920749,lab_b=-4071.3110342969558):74, + LabColor(lab_l=6280.324414591433,lab_a=6015.204491438752,lab_b=-1476.4275931956176):75, + LabColor(lab_l=7180.305703653475,lab_a=3151.7678127953095,lab_b=1373.1975029781495):76, + LabColor(lab_l=8248.198938024443,lab_a=714.6748507078441,lab_b=2817.036007030724):77, + LabColor(lab_l=9182.988945110867,lab_a=-1167.764518966436,lab_b=4015.5942289850245):78, + LabColor(lab_l=9007.020388812489,lab_a=-2093.713817441767,lab_b=3756.923645906545):79, + LabColor(lab_l=8680.802265994806,lab_a=-4081.390060312401,lab_b=3273.7168714125773):80, + LabColor(lab_l=8780.409047221645,lab_a=-3130.134177574469,lab_b=700.5659508608431):81, + LabColor(lab_l=8857.679484481941,lab_a=-2443.415301399,lab_b=-768.5984857936376):82, + LabColor(lab_l=7839.323835639718,lab_a=-570.9192199992401,lab_b=-2304.6246202346338):83, + LabColor(lab_l=6625.666242680838,lab_a=2027.7311898612672,lab_b=-4194.929725399932):84, + LabColor(lab_l=7108.652147423247,lab_a=3498.6937253971073,lab_b=-3402.110197127864):85, + LabColor(lab_l=7434.816150050725,lab_a=4358.722695392508,lab_b=-2869.8331598451205):86, + LabColor(lab_l=7167.213158394653,lab_a=4091.808365037888,lab_b=-1234.4936780628914):87, + LabColor(lab_l=0.0,lab_a=0.0,lab_b=0.0):88, + LabColor(lab_l=1158.5273281050518,lab_a=-0.004651426605661868,lab_b=-0.08668679001253565):89, + LabColor(lab_l=2112.0476709317145,lab_a=-0.008427609405003977,lab_b=-0.15706200883656152):90, + LabColor(lab_l=2688.734626403421,lab_a=-0.010711436251753526,lab_b=-0.19962478265682648):91, + LabColor(lab_l=3575.6716231579267,lab_a=-0.014223932083723412,lab_b=-0.26508577224220176):92, + LabColor(lab_l=4445.718937221886,lab_a=-0.017669540483211676,lab_b=-0.32930020728230147):93, + LabColor(lab_l=5409.950553527043,lab_a=-0.021488142643022456,lab_b=-0.4004659789487164):94, + LabColor(lab_l=6397.481176248715,lab_a=-0.025399014791815944,lab_b=-0.47335134966175474):95, + LabColor(lab_l=7317.0369197362015,lab_a=-0.0290406891458872,lab_b=-0.5412197880815484):96, + LabColor(lab_l=8480.24950353612,lab_a=-0.03364730649479952,lab_b=-0.6270714856782433):97, + LabColor(lab_l=9341.568974319263,lab_a=-0.037058350415009045,lab_b=-0.6906417562959177):98} + colors = list(ircColors.keys()) + closest_colors = sorted(colors, key=lambda color: self.distance(color, pixel, speed)) + closest_color = closest_colors[0] + return ircColors[closest_color] + def distance(self, c1, c2, speed): + if speed == 'fast': + rgb2 = sRGBColor(c2[0], c2[1], c2[2]) + lab1 = c1 + lab2 = convert_color(rgb2, LabColor) + (r1,g1,b1) = lab1.lab_l, lab1.lab_a, lab1.lab_b + (r2,g2,b2) = lab2.lab_l, lab2.lab_a, lab2.lab_b + return math.sqrt((r1 - r2)**2 + (g1 - g2) ** 2 + (b1 - b2) **2) + elif speed == 'medium': + c2 = sRGBColor(c2[0],c2[1],c2[2]); + c2 = convert_color(c2, LabColor); + delta_e = delta_e_cie1976(c1, c2); + return delta_e - def img(self, irc, msg, args, url): - """ + def img(self, irc, msg, args, optlist, url): + """[--cols ] [--invert] [--slow] () Image to ANSI Art """ + optlist = dict(optlist) + if 'slow' in optlist: + speed = 'medium' + else: + speed = 'fast' + if 'cols' in optlist: + cols = optlist.get('cols') + else: + cols = 100 + if 'invert' in optlist: + gscale = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " + else: + gscale = " .'`^\",:;Il!i><~+_-?][}{1)(|\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$" path = os.path.dirname(os.path.abspath(__file__)) filepath = "{0}/tmp".format(path) filename = "{0}/{1}".format(filepath, url.split('/')[-1]) @@ -204,15 +232,14 @@ class ASCII(callbacks.Plugin): if response.status_code == 200: with open("{0}".format(filename), 'wb') as f: f.write(response.content) - gscale = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " # open image and convert to grayscale image = Image.open(filename).convert('L') image2 = Image.open(filename) os.remove(filename) + irc.reply("Please be patient while I render the image into ASCII characters and colorize the output.") # store dimensions W, H = image.size[0], image.size[1] # compute width of tile - cols = 80 w = W/cols # compute tile height based on aspect ratio and scale scale = 0.5 @@ -223,13 +250,13 @@ class ASCII(callbacks.Plugin): if cols > W or rows > H: print("Image too small for specified cols!") exit(0) - if image2.mode != 'RGBA': - image2 = image2.convert('RGBA') + image2 = image2.convert('RGBA') image2 = image2.resize((cols, rows), Image.LANCZOS) colormap = np.array(image2) # ascii image is a list of character strings aimg = [] # generate list of dimensions + old_color = None for j in range(rows): y1 = int(j*h) y2 = int((j+1)*h) @@ -247,20 +274,23 @@ class ASCII(callbacks.Plugin): x2 = W # crop image to extract tile img = image.crop((x1, y1, x2, y2)) - img2 = image2.crop((x1, y1, x2, y2)) # get average luminance avg = int(self.getAverageL(img)) # look up ascii char gsval = gscale[int((avg*69)/255)] # get color value - color = self.getAverageC(colormap[j][i].tolist()) - # append ascii char to string - aimg[j] += "\x03{0}{1}".format(color, gsval) + color = self.getAverageC(colormap[j][i].tolist(),speed) + if color != old_color or gsval != " ": + old_color = color + # append ascii char to string + aimg[j] += "\x03{0}{1}".format(color, gsval) + else: + aimg[j] += "{0}".format(gsval) # return txt image output = aimg for line in output: - irc.reply(line, prefixNick=False) - img = wrap(img, ['text']) + irc.reply(line, prefixNick=False, noLengthCheck=False) + img = wrap(img,[getopts({'cols':'int', 'invert':'', 'slow':''}), ('text')]) def fontlist(self, irc, msg, args): @@ -287,3 +317,4 @@ class ASCII(callbacks.Plugin): Class = ASCII +