Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. private static int imageX;
  2. private static int imageY;
  3. private static int[][] imageConverted;
  4.  
  5. //How the program starts, it does not grab the image from discord yet
  6. else if(command[0].equals("pic2asc"))
  7. {
  8. if(command.length == 1)
  9. imageToASCII();
  10. else
  11. channel.sendMessage("Incorrect usage, please use: <pic2asc");
  12. return;
  13. }
  14.  
  15. //what you call
  16. public static void imageToASCII() throws IOException
  17. {
  18. //IMPORTANT!!!!!!!!!!!!!!!!!!!!! the width and height conversion is manual, you need to do math to get the correct percision of
  19. //it, I remember mixing up width/height or something? but idk
  20. convertToGrey();
  21. int wD = 280;
  22. int hT = 950;
  23. int width = imageX / wD;
  24. int height = imageY / hT;
  25. int avg = width * height;
  26. char[][] asciiPrint = new char[hT][wD];
  27.  
  28. int a = 0;
  29. int b = 0;
  30. int loopCount = 0;
  31. int loopEnd = wD * hT;
  32.  
  33. while (loopCount < loopEnd)
  34. {
  35. int rgbVal = 0;
  36.  
  37. for (int c = a, x = (a + width); c < x; c++)
  38. {
  39. for (int d = b, y = (b + height); d < y; d++)
  40. {
  41. rgbVal += getRGBAvg(imageConverted[c][d]);
  42. }
  43. }
  44.  
  45. int getAsciiAvg = 0;
  46. if(avg != 0)
  47. getAsciiAvg = rgbVal / avg;
  48. asciiPrint[loopCount / wD][loopCount % wD] = getAscii(getAsciiAvg);
  49.  
  50. a += width;
  51. // check if next iteration needs to be done
  52. if((loopCount + 1) % wD == 0)
  53. {
  54. b += height;
  55. a = 0;
  56. }
  57.  
  58. loopCount++;
  59. }
  60.  
  61. String superAwesomeAscii = "";
  62. for(int y = 0; y < wD; y++)
  63. {
  64. for(int x = 0; x < hT; x++)
  65. {
  66. superAwesomeAscii += asciiPrint[x][y];
  67. }
  68. superAwesomeAscii += '\n';
  69. }
  70. //superAwesomeAscii += "```";
  71.  
  72. //channel.sendMessage(superAwesomeAscii);
  73. System.out.print(superAwesomeAscii);
  74.  
  75.  
  76. String FILENAME = "c:\\Users\\Beni\\workspace\\DiscordBot\\src\\Objects\\sanchez.txt";
  77. try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) {
  78.  
  79. bw.write(superAwesomeAscii);
  80.  
  81. System.out.println("Done");
  82.  
  83. } catch (IOException e) {
  84.  
  85. e.printStackTrace();
  86.  
  87. }
  88.  
  89. }
  90.  
  91. public static char getAscii(int rgbVal)
  92. {
  93. char str;
  94.  
  95. if (rgbVal <= 20.0) {
  96. str = '@';
  97. } else if (rgbVal <= 55.0) {
  98. str = '#';
  99. } else if (rgbVal <= 75.0) {
  100. str = '8';
  101. } else if (rgbVal <= 90.0) {
  102. str = '&';
  103. } else if (rgbVal <= 120.0) {
  104. str = 'o';
  105. } else if (rgbVal <= 155.0) {
  106. str = ':';
  107. } else if (rgbVal <= 180.0) {
  108. str = '*';
  109. } else if (rgbVal <= 215.0) {
  110. str = '.';
  111. } else {
  112. str = ' ';
  113. }
  114. return str;
  115. }
  116.  
  117. public static BufferedImage convertToGrey() throws IOException
  118. {
  119. BufferedImage image = null;
  120. imageConverted = null;
  121.  
  122. // Attempt to read image and change to greyscale
  123. try
  124. {
  125. // Read in Image (This is how I did it, just change this to discord image)
  126. image = ImageIO.read(CommandProccessor.class.getResource("/Objects/luis.jpg"));
  127.  
  128. if(image == null)
  129. {
  130. System.out.println("you done goofed up getting image");
  131. return null;
  132. }
  133. imageConverted = convertTo2D(image);
  134. if(imageConverted == null)
  135. {
  136. System.out.println("you done goofed up converting");
  137. return null;
  138. }
  139. } catch (IOException e)
  140. {
  141. e.printStackTrace();
  142. }
  143.  
  144. // Convert image to 2d array of int rgb values
  145. int x = imageConverted.length;
  146. int y = imageConverted[0].length;
  147. imageX = x;
  148. imageY = y;
  149. System.out.println("int x: " + x + "\nint y: " + y);
  150. BufferedImage toSend = intToImg(imageConverted);
  151. ByteArrayOutputStream os = new ByteArrayOutputStream();
  152. ImageIO.write(toSend,"jpeg", os);
  153. InputStream is = new ByteArrayInputStream(os.toByteArray());
  154. //channel.sendFile("", is, "result.jpeg");
  155. return toSend;
  156. }
  157. private static BufferedImage intToImg(int[][] pixels)
  158. {
  159. int width = pixels[0].length;
  160. int height = pixels.length;
  161. BufferedImage pixelImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  162. pixelImage.setRGB(0, 0, width, height, twoDtoOneD(pixels), 0, width);
  163. return pixelImage;
  164. }
  165.  
  166. private static int[] twoDtoOneD(int[][] pixels)
  167. {
  168. int[] pixelArray = new int[(pixels.length)*(pixels[0].length)];
  169. int x = pixels.length;
  170. int y = pixels[0].length;
  171. int count = 0;
  172. for(int i = 0; i < x; i++)
  173. {
  174. for(int j = 0; j < y; j++)
  175. {
  176. pixelArray[count] = pixels[i][j];
  177. count++;
  178. }
  179. }
  180. return pixelArray;
  181. }
  182.  
  183. private static int getRGBAvg(int toConvert)
  184. {
  185. return ( (((byte)(toConvert >> 16)) & 0xFF) + (((byte)(toConvert >> 8)) & 0xFF) + (((byte)toConvert) & 0xFF)) / 3;
  186. }
  187.  
  188. private static int[][] convertTo2D(BufferedImage image)
  189. {
  190.  
  191. final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
  192. final int width = image.getWidth();
  193. final int height = image.getHeight();
  194. int argb = 0;
  195. int grayLevel;
  196.  
  197. int[][] result = new int[height][width];
  198.  
  199. final int pixelLength = 3;
  200.  
  201. // loop through all pixels and convert to int value
  202. for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength)
  203. {
  204. argb = 0;
  205. // this gray level is calculated through human perception of colors rather than computer, so its more accurate
  206. grayLevel = (int)(pixels[pixel] * 0.114 + pixels[pixel + 1] * 0.587 + pixels[pixel + 2] * 0.299);
  207. argb += (grayLevel & 0xff); // blue
  208. argb += (((int) grayLevel & 0xff) << 8); // green
  209. argb += (((int) grayLevel & 0xff) << 16); // red
  210.  
  211. result[row][col] = argb;
  212.  
  213. col++;
  214.  
  215. // who needs 2 for loops :)
  216. if (col == width)
  217. {
  218. col = 0;
  219. row++;
  220. }
  221.  
  222. if(pixel + 5 >= pixels.length)
  223. break;
  224. }
  225.  
  226. return result;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement