Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Image-to-ASCII-Art/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Image-to-ASCII-Art
The title is self-explanatory.

# Requirements
- Pillow Module for Python3
- PyQt5 and PyQt-tools for Python3

```
pip install pillow
pip install pyqt5
pip install pyqt5-tools
```

# File to Run
- program.pyw

# Test
See test.txt file

# Program
![](program.png)

# A Little Explanation
- Size: Bigger is better. The larger it is, the larger the image in the text file. Resolution also increases.
- Ratio: Smaller is better. This value is for the vertical length of the picture to be created not to be longer than normal.

# Best Settings
`Size: 1000 Ratio: 0.2`

`Size: 150 Ratio: 0.3`

# Important Note
The larger the size part, the larger the picture, and probably only a small part of the picture will be visible in the text file when it exceeds a certain value. ***Zoom out in your text editor to see the whole picture.***
73 changes: 73 additions & 0 deletions Image-to-ASCII-Art/drawASCIIart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import PIL.Image

SIZE = 100
WIDTHRATIO = 0.6
path = None
SAVE_NAME = "ascii_image.txt"

# ASCII chars used to build the output text
ASCII_CHARS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]

# resize image
def resize_image(image, new_width=SIZE):
width, height = image.size
ratio = height / width
new_height = int(new_width * ratio * WIDTHRATIO)
resized_image = image.resize((new_width, new_height))
return(resized_image)

# convert grayscale
def grayify(image):
grayscale_image = image.convert("L")
return(grayscale_image)

# pixels to ASCII chars
def pixels_to_ascii(image):
pixels = image.getdata()
characters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels])
return(characters)

def setSize(size_multi : int):
if size_multi < 50:
size_multi = 50

elif size_multi > 1000:
size_multi = 1000

return(size_multi)

def setWidth(ratio_multi : float):
if ratio_multi < 0.2:
ratio_multi = 0.2

elif ratio_multi > 1:
ratio_multi = 1

return(ratio_multi)

def main(new_width = None, WIDTHRAT = None):
if WIDTHRAT != None:
WIDTHRATIO = WIDTHRAT
if new_width != None:
SIZE = new_width
else:
new_width = SIZE

try:
image = PIL.Image.open(path)
except:
print(path, "is not a valid pathname to an image.")

# image to ascii
new_image_data = pixels_to_ascii(grayify(resize_image(image, new_width)))

# format
pixel_count = len(new_image_data)
ascii_image = "\n".join(new_image_data[i:(i+new_width)] for i in range(0, pixel_count, new_width))

# print
#print(ascii_image)

# save results
with open(SAVE_NAME, "w") as f:
f.write(ascii_image)
Binary file added Image-to-ASCII-Art/program.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions Image-to-ASCII-Art/program.pyw
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import sys
import os
import drawASCIIart
from PyQt5.QtWidgets import QWidget, QApplication, QLineEdit, QLabel, QPushButton,QVBoxLayout, QFileDialog, QHBoxLayout

class ASCIIArt(QWidget):

def __init__(self):

super().__init__()
self.init_ui()

def init_ui(self):

self.ratioString = QLabel("Ratio (0.2 - 1.0)")
self.ratio = QLineEdit()
self.nameString = QLabel("Name")
self.name = QLineEdit()
self.ratio.setText(str(drawASCIIart.WIDTHRATIO))
self.sizeString = QLabel("Size (50 - 1000)")
self.size = QLineEdit()
self.size.setText(str(drawASCIIart.SIZE))
self.filePath = QLabel("")
self.button = QPushButton("Add Photo")
self.startButton = QPushButton("Start")
self.startButton.setEnabled(False)

v_box = QVBoxLayout()
v_box.addStretch()
v_box.addWidget(self.nameString)
v_box.addWidget(self.name)
v_box.addWidget(self.sizeString)
v_box.addWidget(self.size)
v_box.addWidget(self.ratioString)
v_box.addWidget(self.ratio)
v_box.addWidget(self.filePath)
v_box.addWidget(self.button)
v_box.addWidget(self.startButton)
v_box.addStretch()

self.setLayout(v_box)
self.setWindowTitle("ASCII Art")

self.button.clicked.connect(self.open_dir)
self.startButton.clicked.connect(self.Start)
self.setMinimumHeight(250)
self.setMaximumHeight(250)
self.setMinimumWidth(300)
self.setMaximumWidth(300)

self.show()

def open_dir(self):

file_path = QFileDialog.getOpenFileName(self, "Select an Image", os.getenv("DESKTOP"),"Images (*.png *.jpg)")
drawASCIIart.path = file_path[0]
self.filePath.setText(file_path[0])
self.startButton.setEnabled(True)

def Start(self):
if self.name.text() != "":
drawASCIIart.SAVE_NAME = self.name.text() + ".txt"
else:
self.filePath.setText("Name field cannot be left blank!")
return False
self.filePath.setText("Wait, please.")
drawASCIIart.main(drawASCIIart.setSize(int(self.size.text())), drawASCIIart.setWidth(float(self.ratio.text())))
self.filePath.setText("Done!")

app = QApplication(sys.argv)
menu = ASCIIArt()
sys.exit(app.exec_())
Binary file added Image-to-ASCII-Art/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions Image-to-ASCII-Art/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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