@ArnavPoddar/

Image to Ascii - SUCCESS

Python

No description

fork
loading
Files
  • main.py
  • asciiart1.gif
  • benchy.jpg
  • bones.jpg
  • borealis.jpg
  • circle_minecraft.jpg
  • Circle.gif
  • dichotomy.jpg
  • hackerearth.png
  • hepburn-audrey-image.jpg
  • ice cream.jpeg
  • maryland.png
  • out.txt
  • papyrus.jpg
  • person.jpg
  • requirements.txt
  • rose.jpg
  • test card.png
  • test.png
  • trump.jpeg
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from PIL import Image

import os

def imagetoascii(image, sizefactor=10, width=None, height=None, invert=False, color=False, allats=False):
	"""
	image: The only required field. What image do you want to be changed into ascii? Must be a path/filename (string)
	sizefactor: How many times smaller do you want your ascii image to be? Is overridden by width or height settings. (int)
	width: How many chars long do you want the asciiart to be? (int)
	height: How tall do you want your asciiart to be? (int)
	invert: Do you want the dark parts to be bright and vice versa? (bool)
	color: Do you want your image to be in color? (Uses Truecolor) (bool)
	allats: Reccomended for color. Do you want the entire image to be @s so it looks more like a photo? (bool)
	"""
	b = []
	sizefactor = round(sizefactor)
	try:
		import replit # Testing if it is on repl.it
		if invert:
			invert = False
		else:
			invert = True
	except:
		pass
	GRAYSCALE = " .:-=+*#%@"[::-1]
	if allats:
		GRAYSCALE = "@@@@@@@@@@@@@"
	if invert:
		GRAYSCALE = GRAYSCALE[::-1]
	img = Image.open(image)
	width_, height_ = img.size
	if width == None and height == None:
		img.thumbnail((width_ / sizefactor, height_ / sizefactor)) # Making the image smaller
	elif width != None:
		width /= 2
		img.thumbnail((width, height_))
	elif height != None:
		img.thumbnail((width_, height))
	width_ = img.size[0]
	pixvals = list(img.getdata()) # RGB values for all the images

	ab = 0
	for x in pixvals:
		if type(x) == int:
			pixvals[ab] = (x, x, x) # Turns into tuple if needed
		ab += 1
	ab = 0
	for x in pixvals:
		a = x[0] + x[1] + x[2] # Averaging the tuples
		a /= 3
		a /= (255 / 9)
		a = round(a)
		b.append(GRAYSCALE[a] * 2) # Assigning them to their greyscales
		if ab % width_ == width_ - 1:
			b.append("\n")
		ab += 1
	if color:
		test = 0
		ab = 0
		for x in b:
			if x != "\n":
				temp = pixvals[ab]
				b[test] = "\x1b[38;2;" + str(temp[0]) + ";" + str(temp[1]) + ";" + str(temp[2]) + "m" + x
				ab += 1
			test += 1
	b = "".join(b)
	return b

# Image to color
print(imagetoascii("bones.jpg", width = 50, height = 50, invert = False, color = True, allats = True))