Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #(this was last edited 2021-6-28, so this is not up to my standards today)
- dohardargs=True
- #order goes: file,dither?,width,height
- hardargs=[0,'asdf.jpg',1,500,500] #none,file,dither,width,height
- ''' following text yoinked from colorama's page:
- ESC [ 0 m # reset all (colors and brightness)
- ESC [ 1 m # bright
- ESC [ 2 m # dim (looks same as normal brightness)
- ESC [ 22 m # normal brightness
- # FOREGROUND:
- ESC [ 30 m # black
- ESC [ 31 m # red
- ESC [ 32 m # green
- ESC [ 33 m # yellow
- ESC [ 34 m # blue
- ESC [ 35 m # magenta
- ESC [ 36 m # cyan
- ESC [ 37 m # white
- ESC [ 39 m # reset
- # BACKGROUND
- ESC [ 40 m # black
- ESC [ 41 m # red
- ESC [ 42 m # green
- ESC [ 43 m # yellow
- ESC [ 44 m # blue
- ESC [ 45 m # magenta
- ESC [ 46 m # cyan
- ESC [ 47 m # white
- ESC [ 49 m # reset
- # cursor positioning
- ESC [ y;x H # position cursor at x across, y down
- ESC [ y;x f # position cursor at x across, y down
- ESC [ n A # move cursor n lines up
- ESC [ n B # move cursor n lines down
- ESC [ n C # move cursor n characters forward
- ESC [ n D # move cursor n characters backward
- # clear the screen
- ESC [ mode J # clear the screen
- # clear the line
- ESC [ mode K # clear the line
- \x1b[6;30;42m
- \x1b[0m
- '''
- from sys import argv as args
- from os import system as cmd, getcwd as cwd
- from os.path import exists
- from colorama import init as colorama_init
- from time import time
- from math import floor, sqrt
- from PIL import Image
- colorama_init()
- rpalette={
- '051051051':'\x1b[1;30;40m',
- '094034039':'\x1b[1;31;40m',
- '015081012':'\x1b[1;32;40m',
- '100097069':'\x1b[1;33;40m',
- '029052103':'\x1b[1;34;40m',
- '075007066':'\x1b[1;35;40m',
- '043087087':'\x1b[1;36;40m',
- '098098098':'\x1b[1;37;40m',
- '012012012':'\x1b[2;30;40m',
- '081013019':'\x1b[2;31;40m',
- '014067012':'\x1b[2;32;40m',
- '079066007':'\x1b[2;33;40m',
- '007028089':'\x1b[2;34;40m',
- '058016064':'\x1b[2;35;40m',
- '029063090':'\x1b[2;36;40m',
- '084084084':'\x1b[2;37;40m',
- '167053063':'\x1b[1;30;41m',
- '209036051':'\x1b[1;31;41m',
- '131083023':'\x1b[1;32;41m',
- '216099081':'\x1b[1;33;41m',
- '145054115':'\x1b[1;34;41m',
- '190009078':'\x1b[1;35;41m',
- '159089099':'\x1b[1;36;41m',
- '213100110':'\x1b[1;37;41m',
- '127013023':'\x1b[2;30;41m',
- '197015031':'\x1b[2;31;41m',
- '130069024':'\x1b[2;32;41m',
- '195067019':'\x1b[2;33;41m',
- '123030101':'\x1b[2;34;41m',
- '174018076':'\x1b[2;35;41m',
- '144065102':'\x1b[2;36;41m',
- '199085095':'\x1b[2;37;41m',
- '056144053':'\x1b[1;30;42m',
- '098127041':'\x1b[1;31;42m',
- '020174013':'\x1b[1;32;42m',
- '105191070':'\x1b[1;33;42m',
- '034145104':'\x1b[1;34;42m',
- '079100068':'\x1b[1;35;42m',
- '048180089':'\x1b[1;36;42m',
- '102191099':'\x1b[1;37;42m',
- '016105013':'\x1b[2;30;42m',
- '085106020':'\x1b[2;31;42m',
- '019161014':'\x1b[2;32;42m',
- '084159008':'\x1b[2;33;42m',
- '011121090':'\x1b[2;34;42m',
- '062109065':'\x1b[2;35;42m',
- '033156091':'\x1b[2;36;42m',
- '088177085':'\x1b[2;37;42m',
- '164141044':'\x1b[1;30;43m',
- '207124032':'\x1b[1;31;43m',
- '128171004':'\x1b[1;32;43m',
- '214187061':'\x1b[1;33;43m',
- '142142095':'\x1b[1;34;43m',
- '188097059':'\x1b[1;35;43m',
- '157177080':'\x1b[1;36;43m',
- '211188090':'\x1b[1;37;43m',
- '125102004':'\x1b[2;30;43m',
- '194103011':'\x1b[2;31;43m',
- '127157005':'\x1b[2;32;43m',
- '193156000':'\x1b[2;33;43m',
- '120118081':'\x1b[2;34;43m',
- '171106057':'\x1b[2;35;43m',
- '142153082':'\x1b[2;36;43m',
- '197174076':'\x1b[2;37;43m',
- '044078180':'\x1b[1;30;44m',
- '086061168':'\x1b[1;31;44m',
- '008108140':'\x1b[1;32;44m',
- '093124198':'\x1b[1;33;44m',
- '022079231':'\x1b[1;34;44m',
- '067034195':'\x1b[1;35;44m',
- '036114216':'\x1b[1;36;44m',
- '090125227':'\x1b[1;37;44m',
- '004038140':'\x1b[2;30;44m',
- '073040147':'\x1b[2;31;44m',
- '007094141':'\x1b[2;32;44m',
- '072092136':'\x1b[2;33;44m',
- '000055218':'\x1b[2;34;44m',
- '051043193':'\x1b[2;35;44m',
- '021090219':'\x1b[2;36;44m',
- '076110212':'\x1b[2;37;44m',
- '129058139':'\x1b[1;30;45m',
- '171041127':'\x1b[1;31;45m',
- '093088099':'\x1b[1;32;45m',
- '178104156':'\x1b[1;33;45m',
- '107059190':'\x1b[1;34;45m',
- '152014154':'\x1b[1;35;45m',
- '121094175':'\x1b[1;36;45m',
- '175105185':'\x1b[1;37;45m',
- '089018099':'\x1b[2;30;45m',
- '158020106':'\x1b[2;31;45m',
- '092074100':'\x1b[2;32;45m',
- '157072095':'\x1b[2;33;45m',
- '085035176':'\x1b[2;34;45m',
- '136023152':'\x1b[2;35;45m',
- '106070177':'\x1b[2;36;45m',
- '161090171':'\x1b[2;37;45m',
- '080138182':'\x1b[1;30;46m',
- '122120170':'\x1b[1;31;46m',
- '044168142':'\x1b[1;32;46m',
- '129184200':'\x1b[1;33;46m',
- '058138233':'\x1b[1;34;46m',
- '103093197':'\x1b[1;35;46m',
- '072174218':'\x1b[1;36;46m',
- '127184228':'\x1b[1;37;46m',
- '040098142':'\x1b[2;30;46m',
- '110099149':'\x1b[2;31;46m',
- '043154143':'\x1b[2;32;46m',
- '108152138':'\x1b[2;33;46m',
- '036114219':'\x1b[2;34;46m',
- '087102195':'\x1b[2;35;46m',
- '058150221':'\x1b[2;36;46m',
- '112170214':'\x1b[2;37;46m',
- '171171171':'\x1b[1;30;47m',
- '214154159':'\x1b[1;31;47m',
- '135201132':'\x1b[1;32;47m',
- '220217189':'\x1b[1;33;47m',
- '149172223':'\x1b[1;34;47m',
- '195127186':'\x1b[1;35;47m',
- '163207207':'\x1b[1;36;47m',
- '218218218':'\x1b[1;37;47m',
- '132132132':'\x1b[2;30;47m',
- '201133139':'\x1b[2;31;47m',
- '134187132':'\x1b[2;32;47m',
- '199186127':'\x1b[2;33;47m',
- '127148209':'\x1b[2;34;47m',
- '178136184':'\x1b[2;35;47m',
- '149183210':'\x1b[2;36;47m',
- '204204204':'\x1b[2;37;47m',}
- wpalette=[ #this list is for the image library
- 51, 51, 51,
- 94, 34, 39,
- 15, 81, 12,
- 100, 97, 69,
- 29, 52, 103,
- 75, 7, 66,
- 43, 87, 87,
- 98, 98, 98,
- 12, 12, 12,
- 81, 13, 19,
- 14, 67, 12,
- 79, 66, 7,
- 7, 28, 89,
- 58, 16, 64,
- 29, 63, 90,
- 84, 84, 84,
- 167, 53, 63,
- 209, 36, 51,
- 131, 83, 23,
- 216, 99, 81,
- 145, 54, 115,
- 190, 9, 78,
- 159, 89, 99,
- 213, 100, 110,
- 127, 13, 23,
- 197, 15, 31,
- 130, 69, 24,
- 195, 67, 19,
- 123, 30, 101,
- 174, 18, 76,
- 144, 65, 102,
- 199, 85, 95,
- 56, 144, 53,
- 98, 127, 41,
- 20, 174, 13,
- 105, 191, 70,
- 34, 145, 104,
- 79, 100, 68,
- 48, 180, 89,
- 102, 191, 99,
- 16, 105, 13,
- 85, 106, 20,
- 19, 161, 14,
- 84, 159, 8,
- 11, 121, 90,
- 62, 109, 65,
- 33, 156, 91,
- 88, 177, 85,
- 164, 141, 44,
- 207, 124, 32,
- 128, 171, 4,
- 214, 187, 61,
- 142, 142, 95,
- 188, 97, 59,
- 157, 177, 80,
- 211, 188, 90,
- 125, 102, 4,
- 194, 103, 11,
- 127, 157, 5,
- 193, 156, 0,
- 120, 118, 81,
- 171, 106, 57,
- 142, 153, 82,
- 197, 174, 76,
- 44, 78, 180,
- 86, 61, 168,
- 8, 108, 140,
- 93, 124, 198,
- 22, 79, 231,
- 67, 34, 195,
- 36, 114, 216,
- 90, 125, 227,
- 4, 38, 140,
- 73, 40, 147,
- 7, 94, 141,
- 72, 92, 136,
- 0, 55, 218,
- 51, 43, 193,
- 21, 90, 219,
- 76, 110, 212,
- 129, 58, 139,
- 171, 41, 127,
- 93, 88, 99,
- 178, 104, 156,
- 107, 59, 190,
- 152, 14, 154,
- 121, 94, 175,
- 175, 105, 185,
- 89, 18, 99,
- 158, 20, 106,
- 92, 74, 100,
- 157, 72, 95,
- 85, 35, 176,
- 136, 23, 152,
- 106, 70, 177,
- 161, 90, 171,
- 80, 138, 182,
- 122, 120, 170,
- 44, 168, 142,
- 129, 184, 200,
- 58, 138, 233,
- 103, 93, 197,
- 72, 174, 218,
- 127, 184, 228,
- 40, 98, 142,
- 110, 99, 149,
- 43, 154, 143,
- 108, 152, 138,
- 36, 114, 219,
- 87, 102, 195,
- 58, 150, 221,
- 112, 170, 214,
- 171, 171, 171,
- 214, 154, 159,
- 135, 201, 132,
- 220, 217, 189,
- 149, 172, 223,
- 195, 127, 186,
- 163, 207, 207,
- 218, 218, 218,
- 132, 132, 132,
- 201, 133, 139, ###
- 134, 187, 132,
- 199, 186, 127,
- 127, 148, 209,
- 178, 136, 184,
- 149, 183, 210,
- 204, 204, 204,
- ] + [204,] *384 #(256-128)*3=384
- #lol look at all of dat data ^^
- #args=[0,'lenna.png',1,256,256] #none,file,dither,width,height
- #args[2]=int(args[2])
- if dohardargs:
- args=hardargs
- else:
- args[2]=int(args[2])
- if len(args) < 2:
- print("Please give me one file path argument")
- cmd('pause')
- exit()
- if args[1][1]!=':':
- args[1]=cwd()+'\\'+args[1]
- if not exists(args[1]):
- print('"'+args[1]+'"'+" doesn't exist!")
- if len(args) < 3:
- args.append(1)
- def rnd(num):
- #i heard that python's round() is weird so i made my own just in case
- return floor(num+.5)
- def cut(num):
- return floor(num*1000)/1000
- def fpd(pixel): #fulfill pixel digits
- string=''
- for i in range(0,3):
- sbuffer=str(pixel[i])
- while len(sbuffer) < 3:
- sbuffer='0'+sbuffer
- string=string+sbuffer
- return string
- def loadimg(lpath):
- img=Image.open(lpath)
- return img.convert('RGB')
- def resize(imgobj,size):
- return imgobj.resize(size,1) #Image.LANCZOS=1
- def quant(img,dodither):
- global wpalette
- img.load()
- width,height=img.size
- pimage = Image.new("P", (1, 1), 0)
- pimage.putpalette(wpalette)
- pimage.load()
- qimage=img._new(img.im.convert("P", dodither, pimage.im))
- img.close()
- pimage.close()
- return qimage
- def convert_quant(imgobj):
- global wpalette
- img=imgobj.getdata()
- converted=[]
- b=0
- for i in range(0,len(img)):
- b=img[i]*3
- converted.append((wpalette[b],wpalette[b+1],wpalette[b+2]))
- return converted
- def assembleline(img,width,offset=0):
- global rpalette
- string=''
- buffera=''
- bufferb=''
- offset
- for ix in range(0,width):
- buffera=fpd(img[offset+ix])
- bufferb=rpalette[buffera]
- string=string+bufferb+'@'
- return string+'\x1b[0m'
- start=time()
- qimg=loadimg(args[1])
- isize=qimg.size
- if len(args) >= 5:
- isize=(int(args[3]),int(args[4]))
- qimg=resize(qimg,isize)
- qimg=quant(qimg,args[2])
- qimg.convert('RGB')
- finalout=args[1][0:len(args[1])-4]+'OUT.png'
- qimg.save(finalout)
- qimg=convert_quant(qimg)
- cmd('cls')
- for iy in range(0,isize[1]):
- print(assembleline(qimg,isize[0],iy*isize[0]))
- #print('Saved to file: "'+finalout+'"')
- print("Processed in: " + str(cut(time()-start)) + " seconds")
- cmd('pause')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement