In short, how do I get this:
From this:
JavaScript
x
8
1
def fiblike(ls, n):
2
store = []
3
for i in range(n):
4
a = ls.pop(0)
5
ls.append(sum(ls)+a)
6
store.append(a)
7
return store
8
With all the indentation guide and code highlighting.
I have written hundreds of Python scripts and I need to convert all of them to images…
I have seen this:
JavaScript
1
29
29
1
import Image
2
import ImageDraw
3
import ImageFont
4
5
def getSize(txt, font):
6
testImg = Image.new('RGB', (1, 1))
7
testDraw = ImageDraw.Draw(testImg)
8
return testDraw.textsize(txt, font)
9
10
if __name__ == '__main__':
11
12
fontname = "Arial.ttf"
13
fontsize = 11
14
text = "example@gmail.com"
15
16
colorText = "black"
17
colorOutline = "red"
18
colorBackground = "white"
19
20
21
font = ImageFont.truetype(fontname, fontsize)
22
width, height = getSize(text, font)
23
img = Image.new('RGB', (width+4, height+4), colorBackground)
24
d = ImageDraw.Draw(img)
25
d.text((2, height/2), text, fill=colorText, font=font)
26
d.rectangle((0, 0, width+3, height+3), outline=colorOutline)
27
28
img.save("D:/image.png")
29
from here
But it does not do code highlighting and I want either a numpy
or cv2
based solution.
How can I do it?
Advertisement
Answer
You can use pygments library to convert to HTML with highlighting and then convert the HTML to an image.