├── README.md ├── ASCII art IMAGE.cpp └── ASCII art VIDEO.cpp /README.md: -------------------------------------------------------------------------------- 1 | Важно! 2 | Один файл тут только для работы с изображениями, а другой - только для видео. Я так специально сделал, чтобы искусственно не переусложнять код, так что не перепутайте 3 | И также вам необходимо установить opencv, иначе это работать не будет! 4 | -------------------------------------------------------------------------------- /ASCII art IMAGE.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/highgui.hpp" 2 | #include "opencv2/imgproc.hpp" 3 | #include 4 | #include 5 | 6 | using namespace cv; 7 | using namespace std; 8 | 9 | void SetWindow(int Width, int Height) 10 | { 11 | _COORD coord; 12 | coord.X = Width; 13 | coord.Y = Height; 14 | _SMALL_RECT Rect; 15 | Rect.Top = 0; 16 | Rect.Left = 0; 17 | Rect.Bottom = Height - 1; 18 | Rect.Right = Width - 1; 19 | HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); 20 | SetConsoleScreenBufferSize(Handle, coord); 21 | SetConsoleWindowInfo(Handle, TRUE, &Rect); 22 | string str = "mode con cols=" + to_string(Width) + " lines=" + to_string(Height); 23 | system(str.c_str()); 24 | } 25 | 26 | int main(void) 27 | { 28 | int nwidth = 170; // При необходимости можно изменить размер 29 | float aspect = 11.0 / 24.0; 30 | string path = "C:\\Users\\rchek\\OneDrive\\Desktop\\2.jpg"; 31 | // Вставьте сюда путь к фото 32 | Mat image = imread(path, IMREAD_COLOR); 33 | 34 | Size sz = image.size(); 35 | int im_width = sz.width, im_height = sz.height; 36 | float imasp = (float)im_width / nwidth; 37 | int nheight = round(im_height * aspect / imasp); 38 | 39 | resize(image, image, { nwidth, nheight }, 0, 0, INTER_NEAREST); 40 | SetWindow(nwidth, nheight); 41 | 42 | wchar_t* screen = new wchar_t[nwidth * nheight + 1]; 43 | for (int i = 0; i < nwidth * nheight + 1; i++) screen[i] = ' '; 44 | HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL); 45 | SetConsoleActiveScreenBuffer(hConsole); 46 | DWORD dwBytesWritten = 0; 47 | 48 | const char* gradient = " .:!/r(lZ4H9W8$@"; 49 | 50 | for (int x = 0; x < image.rows; x++) { 51 | for (int y = 0; y < image.cols; y++) { 52 | Vec3b pixel = image.at(x, y); 53 | int color = round((pixel[0] + pixel[1] + pixel[2]) / 3.0); 54 | screen[x * nwidth + y] = gradient[color / 16]; 55 | } 56 | } 57 | 58 | while (1) WriteConsoleOutputCharacter(hConsole, screen, nwidth * nheight, { 0, 0 }, &dwBytesWritten); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /ASCII art VIDEO.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/highgui.hpp" 2 | #include "opencv2/imgproc.hpp" 3 | #include "opencv2/videoio.hpp" 4 | #include 5 | #include 6 | 7 | using namespace cv; 8 | using namespace std; 9 | 10 | void SetWindow(int Width, int Height) 11 | { 12 | _COORD coord; 13 | coord.X = Width; 14 | coord.Y = Height; 15 | _SMALL_RECT Rect; 16 | Rect.Top = 0; 17 | Rect.Left = 0; 18 | Rect.Bottom = Height - 1; 19 | Rect.Right = Width - 1; 20 | HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); 21 | SetConsoleScreenBufferSize(Handle, coord); 22 | SetConsoleWindowInfo(Handle, TRUE, &Rect); 23 | string str = "mode con cols=" + to_string(Width) + " lines=" + to_string(Height); 24 | system(str.c_str()); 25 | } 26 | 27 | int main(void) 28 | { 29 | int nwidth = 245; // При необходимости можно изменить размер 30 | float aspect = 11.0 / 24.0; 31 | string path = ""; 32 | // Вставьте сюда путь к видео 33 | VideoCapture vid(path); 34 | 35 | int im_width = static_cast(vid.get(CAP_PROP_FRAME_WIDTH)); 36 | int im_height = static_cast(vid.get(CAP_PROP_FRAME_HEIGHT)); 37 | //int im_width = sz.width, im_height = sz.height; 38 | float imasp = (float)im_width / nwidth; 39 | int nheight = round(im_height * aspect / imasp); 40 | 41 | //resize(image, image, { nwidth, nheight }, 0, 0, INTER_NEAREST); 42 | SetWindow(nwidth, nheight); 43 | 44 | wchar_t* screen = new wchar_t[nwidth * nheight+1]; 45 | HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL); 46 | SetConsoleActiveScreenBuffer(hConsole); 47 | DWORD dwBytesWritten = 0; 48 | 49 | const char* gradient = " .:!/r(lZ4H9W8$@"; 50 | 51 | Mat frame; 52 | 53 | while (1) 54 | { 55 | vid >> frame; 56 | if (frame.empty()) break; 57 | resize(frame, frame, { nwidth, nheight }, 0, 0, INTER_NEAREST); 58 | for (int x = 0; x < frame.rows; x++) { 59 | for (int y = 0; y < frame.cols; y++) { 60 | Vec3b pixel = frame.at(x, y); 61 | int color = round((pixel[0] + pixel[1] + pixel[2]) / 3.0); 62 | screen[x * nwidth + y] = gradient[color / 16]; 63 | } 64 | } 65 | WriteConsoleOutputCharacter(hConsole, screen, nwidth * nheight, { 0, 0 }, &dwBytesWritten); 66 | } 67 | 68 | return 0; 69 | } --------------------------------------------------------------------------------