Advertisement
Kitomas

it2c.py

Aug 14th, 2023
2,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.52 KB | None | 0 0
  1. #(this was last edited 2021-6-28, so this is not up to my standards today)
  2. dohardargs=True
  3. #order goes: file,dither?,width,height
  4. hardargs=[0,'asdf.jpg',1,500,500] #none,file,dither,width,height
  5. ''' following text yoinked from colorama's page:
  6. ESC [ 0 m       # reset all (colors and brightness)
  7. ESC [ 1 m       # bright
  8. ESC [ 2 m       # dim (looks same as normal brightness)
  9. ESC [ 22 m      # normal brightness
  10.  
  11. # FOREGROUND:
  12. ESC [ 30 m      # black
  13. ESC [ 31 m      # red
  14. ESC [ 32 m      # green
  15. ESC [ 33 m      # yellow
  16. ESC [ 34 m      # blue
  17. ESC [ 35 m      # magenta
  18. ESC [ 36 m      # cyan
  19. ESC [ 37 m      # white
  20. ESC [ 39 m      # reset
  21.  
  22. # BACKGROUND
  23. ESC [ 40 m      # black
  24. ESC [ 41 m      # red
  25. ESC [ 42 m      # green
  26. ESC [ 43 m      # yellow
  27. ESC [ 44 m      # blue
  28. ESC [ 45 m      # magenta
  29. ESC [ 46 m      # cyan
  30. ESC [ 47 m      # white
  31. ESC [ 49 m      # reset
  32.  
  33. # cursor positioning
  34. ESC [ y;x H     # position cursor at x across, y down
  35. ESC [ y;x f     # position cursor at x across, y down
  36. ESC [ n A       # move cursor n lines up
  37. ESC [ n B       # move cursor n lines down
  38. ESC [ n C       # move cursor n characters forward
  39. ESC [ n D       # move cursor n characters backward
  40.  
  41. # clear the screen
  42. ESC [ mode J    # clear the screen
  43.  
  44. # clear the line
  45. ESC [ mode K    # clear the line
  46.  
  47. \x1b[6;30;42m
  48. \x1b[0m
  49. '''
  50. from sys import argv as args
  51. from os import system as cmd, getcwd as cwd
  52. from os.path import exists
  53. from colorama import init as colorama_init
  54. from time import time
  55. from math import floor, sqrt
  56. from PIL import Image
  57. colorama_init()
  58. rpalette={
  59. '051051051':'\x1b[1;30;40m',
  60. '094034039':'\x1b[1;31;40m',
  61. '015081012':'\x1b[1;32;40m',
  62. '100097069':'\x1b[1;33;40m',
  63. '029052103':'\x1b[1;34;40m',
  64. '075007066':'\x1b[1;35;40m',
  65. '043087087':'\x1b[1;36;40m',
  66. '098098098':'\x1b[1;37;40m',
  67. '012012012':'\x1b[2;30;40m',
  68. '081013019':'\x1b[2;31;40m',
  69. '014067012':'\x1b[2;32;40m',
  70. '079066007':'\x1b[2;33;40m',
  71. '007028089':'\x1b[2;34;40m',
  72. '058016064':'\x1b[2;35;40m',
  73. '029063090':'\x1b[2;36;40m',
  74. '084084084':'\x1b[2;37;40m',
  75. '167053063':'\x1b[1;30;41m',
  76. '209036051':'\x1b[1;31;41m',
  77. '131083023':'\x1b[1;32;41m',
  78. '216099081':'\x1b[1;33;41m',
  79. '145054115':'\x1b[1;34;41m',
  80. '190009078':'\x1b[1;35;41m',
  81. '159089099':'\x1b[1;36;41m',
  82. '213100110':'\x1b[1;37;41m',
  83. '127013023':'\x1b[2;30;41m',
  84. '197015031':'\x1b[2;31;41m',
  85. '130069024':'\x1b[2;32;41m',
  86. '195067019':'\x1b[2;33;41m',
  87. '123030101':'\x1b[2;34;41m',
  88. '174018076':'\x1b[2;35;41m',
  89. '144065102':'\x1b[2;36;41m',
  90. '199085095':'\x1b[2;37;41m',
  91. '056144053':'\x1b[1;30;42m',
  92. '098127041':'\x1b[1;31;42m',
  93. '020174013':'\x1b[1;32;42m',
  94. '105191070':'\x1b[1;33;42m',
  95. '034145104':'\x1b[1;34;42m',
  96. '079100068':'\x1b[1;35;42m',
  97. '048180089':'\x1b[1;36;42m',
  98. '102191099':'\x1b[1;37;42m',
  99. '016105013':'\x1b[2;30;42m',
  100. '085106020':'\x1b[2;31;42m',
  101. '019161014':'\x1b[2;32;42m',
  102. '084159008':'\x1b[2;33;42m',
  103. '011121090':'\x1b[2;34;42m',
  104. '062109065':'\x1b[2;35;42m',
  105. '033156091':'\x1b[2;36;42m',
  106. '088177085':'\x1b[2;37;42m',
  107. '164141044':'\x1b[1;30;43m',
  108. '207124032':'\x1b[1;31;43m',
  109. '128171004':'\x1b[1;32;43m',
  110. '214187061':'\x1b[1;33;43m',
  111. '142142095':'\x1b[1;34;43m',
  112. '188097059':'\x1b[1;35;43m',
  113. '157177080':'\x1b[1;36;43m',
  114. '211188090':'\x1b[1;37;43m',
  115. '125102004':'\x1b[2;30;43m',
  116. '194103011':'\x1b[2;31;43m',
  117. '127157005':'\x1b[2;32;43m',
  118. '193156000':'\x1b[2;33;43m',
  119. '120118081':'\x1b[2;34;43m',
  120. '171106057':'\x1b[2;35;43m',
  121. '142153082':'\x1b[2;36;43m',
  122. '197174076':'\x1b[2;37;43m',
  123. '044078180':'\x1b[1;30;44m',
  124. '086061168':'\x1b[1;31;44m',
  125. '008108140':'\x1b[1;32;44m',
  126. '093124198':'\x1b[1;33;44m',
  127. '022079231':'\x1b[1;34;44m',
  128. '067034195':'\x1b[1;35;44m',
  129. '036114216':'\x1b[1;36;44m',
  130. '090125227':'\x1b[1;37;44m',
  131. '004038140':'\x1b[2;30;44m',
  132. '073040147':'\x1b[2;31;44m',
  133. '007094141':'\x1b[2;32;44m',
  134. '072092136':'\x1b[2;33;44m',
  135. '000055218':'\x1b[2;34;44m',
  136. '051043193':'\x1b[2;35;44m',
  137. '021090219':'\x1b[2;36;44m',
  138. '076110212':'\x1b[2;37;44m',
  139. '129058139':'\x1b[1;30;45m',
  140. '171041127':'\x1b[1;31;45m',
  141. '093088099':'\x1b[1;32;45m',
  142. '178104156':'\x1b[1;33;45m',
  143. '107059190':'\x1b[1;34;45m',
  144. '152014154':'\x1b[1;35;45m',
  145. '121094175':'\x1b[1;36;45m',
  146. '175105185':'\x1b[1;37;45m',
  147. '089018099':'\x1b[2;30;45m',
  148. '158020106':'\x1b[2;31;45m',
  149. '092074100':'\x1b[2;32;45m',
  150. '157072095':'\x1b[2;33;45m',
  151. '085035176':'\x1b[2;34;45m',
  152. '136023152':'\x1b[2;35;45m',
  153. '106070177':'\x1b[2;36;45m',
  154. '161090171':'\x1b[2;37;45m',
  155. '080138182':'\x1b[1;30;46m',
  156. '122120170':'\x1b[1;31;46m',
  157. '044168142':'\x1b[1;32;46m',
  158. '129184200':'\x1b[1;33;46m',
  159. '058138233':'\x1b[1;34;46m',
  160. '103093197':'\x1b[1;35;46m',
  161. '072174218':'\x1b[1;36;46m',
  162. '127184228':'\x1b[1;37;46m',
  163. '040098142':'\x1b[2;30;46m',
  164. '110099149':'\x1b[2;31;46m',
  165. '043154143':'\x1b[2;32;46m',
  166. '108152138':'\x1b[2;33;46m',
  167. '036114219':'\x1b[2;34;46m',
  168. '087102195':'\x1b[2;35;46m',
  169. '058150221':'\x1b[2;36;46m',
  170. '112170214':'\x1b[2;37;46m',
  171. '171171171':'\x1b[1;30;47m',
  172. '214154159':'\x1b[1;31;47m',
  173. '135201132':'\x1b[1;32;47m',
  174. '220217189':'\x1b[1;33;47m',
  175. '149172223':'\x1b[1;34;47m',
  176. '195127186':'\x1b[1;35;47m',
  177. '163207207':'\x1b[1;36;47m',
  178. '218218218':'\x1b[1;37;47m',
  179. '132132132':'\x1b[2;30;47m',
  180. '201133139':'\x1b[2;31;47m',
  181. '134187132':'\x1b[2;32;47m',
  182. '199186127':'\x1b[2;33;47m',
  183. '127148209':'\x1b[2;34;47m',
  184. '178136184':'\x1b[2;35;47m',
  185. '149183210':'\x1b[2;36;47m',
  186. '204204204':'\x1b[2;37;47m',}
  187. wpalette=[ #this list is for the image library
  188. 51, 51, 51,
  189. 94, 34, 39,
  190. 15, 81, 12,
  191. 100, 97, 69,
  192. 29, 52, 103,
  193. 75, 7, 66,
  194. 43, 87, 87,
  195. 98, 98, 98,
  196. 12, 12, 12,
  197. 81, 13, 19,
  198. 14, 67, 12,
  199. 79, 66, 7,
  200. 7, 28, 89,
  201. 58, 16, 64,
  202. 29, 63, 90,
  203. 84, 84, 84,
  204. 167, 53, 63,
  205. 209, 36, 51,
  206. 131, 83, 23,
  207. 216, 99, 81,
  208. 145, 54, 115,
  209. 190, 9, 78,
  210. 159, 89, 99,
  211. 213, 100, 110,
  212. 127, 13, 23,
  213. 197, 15, 31,
  214. 130, 69, 24,
  215. 195, 67, 19,
  216. 123, 30, 101,
  217. 174, 18, 76,
  218. 144, 65, 102,
  219. 199, 85, 95,
  220. 56, 144, 53,
  221. 98, 127, 41,
  222. 20, 174, 13,
  223. 105, 191, 70,
  224. 34, 145, 104,
  225. 79, 100, 68,
  226. 48, 180, 89,
  227. 102, 191, 99,
  228. 16, 105, 13,
  229. 85, 106, 20,
  230. 19, 161, 14,
  231. 84, 159, 8,
  232. 11, 121, 90,
  233. 62, 109, 65,
  234. 33, 156, 91,
  235. 88, 177, 85,
  236. 164, 141, 44,
  237. 207, 124, 32,
  238. 128, 171, 4,
  239. 214, 187, 61,
  240. 142, 142, 95,
  241. 188, 97, 59,
  242. 157, 177, 80,
  243. 211, 188, 90,
  244. 125, 102, 4,
  245. 194, 103, 11,
  246. 127, 157, 5,
  247. 193, 156, 0,
  248. 120, 118, 81,
  249. 171, 106, 57,
  250. 142, 153, 82,
  251. 197, 174, 76,
  252. 44, 78, 180,
  253. 86, 61, 168,
  254. 8, 108, 140,
  255. 93, 124, 198,
  256. 22, 79, 231,
  257. 67, 34, 195,
  258. 36, 114, 216,
  259. 90, 125, 227,
  260. 4, 38, 140,
  261. 73, 40, 147,
  262. 7, 94, 141,
  263. 72, 92, 136,
  264. 0, 55, 218,
  265. 51, 43, 193,
  266. 21, 90, 219,
  267. 76, 110, 212,
  268. 129, 58, 139,
  269. 171, 41, 127,
  270. 93, 88, 99,
  271. 178, 104, 156,
  272. 107, 59, 190,
  273. 152, 14, 154,
  274. 121, 94, 175,
  275. 175, 105, 185,
  276. 89, 18, 99,
  277. 158, 20, 106,
  278. 92, 74, 100,
  279. 157, 72, 95,
  280. 85, 35, 176,
  281. 136, 23, 152,
  282. 106, 70, 177,
  283. 161, 90, 171,
  284. 80, 138, 182,
  285. 122, 120, 170,
  286. 44, 168, 142,
  287. 129, 184, 200,
  288. 58, 138, 233,
  289. 103, 93, 197,
  290. 72, 174, 218,
  291. 127, 184, 228,
  292. 40, 98, 142,
  293. 110, 99, 149,
  294. 43, 154, 143,
  295. 108, 152, 138,
  296. 36, 114, 219,
  297. 87, 102, 195,
  298. 58, 150, 221,
  299. 112, 170, 214,
  300. 171, 171, 171,
  301. 214, 154, 159,
  302. 135, 201, 132,
  303. 220, 217, 189,
  304. 149, 172, 223,
  305. 195, 127, 186,
  306. 163, 207, 207,
  307. 218, 218, 218,
  308. 132, 132, 132,
  309. 201, 133, 139, ###
  310. 134, 187, 132,
  311. 199, 186, 127,
  312. 127, 148, 209,
  313. 178, 136, 184,
  314. 149, 183, 210,
  315. 204, 204, 204,
  316. ] + [204,] *384 #(256-128)*3=384
  317. #lol look at all of dat data ^^
  318. #args=[0,'lenna.png',1,256,256] #none,file,dither,width,height
  319. #args[2]=int(args[2])
  320. if dohardargs:
  321.     args=hardargs
  322. else:
  323.     args[2]=int(args[2])
  324. if len(args) < 2:
  325.     print("Please give me one file path argument")
  326.     cmd('pause')
  327.     exit()
  328. if args[1][1]!=':':
  329.     args[1]=cwd()+'\\'+args[1]
  330. if not exists(args[1]):
  331.     print('"'+args[1]+'"'+" doesn't exist!")
  332. if len(args) < 3:
  333.     args.append(1)
  334.  
  335. def rnd(num):
  336.     #i heard that python's round() is weird so i made my own just in case
  337.     return floor(num+.5)
  338. def cut(num):
  339.     return floor(num*1000)/1000
  340.  
  341. def fpd(pixel): #fulfill pixel digits
  342.     string=''
  343.     for i in range(0,3):
  344.         sbuffer=str(pixel[i])
  345.         while len(sbuffer) < 3:
  346.             sbuffer='0'+sbuffer
  347.         string=string+sbuffer
  348.     return string
  349. def loadimg(lpath):
  350.     img=Image.open(lpath)
  351.     return img.convert('RGB')
  352. def resize(imgobj,size):
  353.     return imgobj.resize(size,1) #Image.LANCZOS=1
  354. def quant(img,dodither):
  355.     global wpalette
  356.  
  357.     img.load()
  358.     width,height=img.size
  359.  
  360.     pimage = Image.new("P", (1, 1), 0)
  361.     pimage.putpalette(wpalette)
  362.     pimage.load()
  363.  
  364.     qimage=img._new(img.im.convert("P", dodither, pimage.im))
  365.     img.close()
  366.     pimage.close()
  367.  
  368.     return qimage
  369.  
  370. def convert_quant(imgobj):
  371.     global wpalette
  372.     img=imgobj.getdata()
  373.     converted=[]
  374.     b=0
  375.     for i in range(0,len(img)):
  376.         b=img[i]*3
  377.         converted.append((wpalette[b],wpalette[b+1],wpalette[b+2]))
  378.     return converted
  379.  
  380. def assembleline(img,width,offset=0):
  381.     global rpalette
  382.     string=''
  383.     buffera=''
  384.     bufferb=''
  385.     offset
  386.     for ix in range(0,width):
  387.         buffera=fpd(img[offset+ix])
  388.         bufferb=rpalette[buffera]
  389.         string=string+bufferb+'@'
  390.     return string+'\x1b[0m'
  391.  
  392. start=time()
  393.  
  394. qimg=loadimg(args[1])
  395. isize=qimg.size
  396. if len(args) >= 5:
  397.     isize=(int(args[3]),int(args[4]))
  398.     qimg=resize(qimg,isize)
  399. qimg=quant(qimg,args[2])
  400. qimg.convert('RGB')
  401. finalout=args[1][0:len(args[1])-4]+'OUT.png'
  402. qimg.save(finalout)
  403. qimg=convert_quant(qimg)
  404. cmd('cls')
  405. for iy in range(0,isize[1]):
  406.     print(assembleline(qimg,isize[0],iy*isize[0]))
  407. #print('Saved to file: "'+finalout+'"')
  408. print("Processed in: " + str(cut(time()-start)) + " seconds")
  409. cmd('pause')
  410.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement