User:Stonestone/pythonnotes: Difference between revisions
Stonestone (talk | contribs) (Created page with "===大明白区=== <pre>目前,懒得弄 </pre> ===等回复区=== <pre> from PIL import Image, ImageDraw , ImageFont from cStringIO import StringIO #其实不太懂StringIO...") |
Stonestone (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
===大明白区=== | ===大明白区=== | ||
<pre> | <pre>#这是一个mjpeg,然后用handbreak处理。最近上课就在讲这些东西. | ||
from PIL import Image, ImageDraw, ImageFont | |||
from PIL import Image, ImageDraw , ImageFont | # Docs: https://docs.python.org/2/library/stringio.html | ||
# If you measure for speed, you should use cStringIO | |||
from cStringIO import StringIO #其实不太懂StringIO这个模块。 | from cStringIO import StringIO #其实不太懂StringIO这个模块。 | ||
import random | import random | ||
Line 10: | Line 11: | ||
f = ImageFont.truetype("/Users/Stone/Desktop/Ptah-Regular.otf", size=300) | f = ImageFont.truetype("/Users/Stone/Desktop/Ptah-Regular.otf", size=300) | ||
of = open("frames.mjpeg", "wb") # | # `open` is a Python build-in function | ||
# Docs: https://docs.python.org/2/library/functions.html#open | |||
# "frames.mjpeg"(filename) is the first param of open function. | |||
# 'w' for writing (truncating the file if it already exists) | |||
# append 'b' to the mode value to open the file in binary mode, which will improve portability. | |||
of = open("frames.mjpeg", "wb") #不太懂这句啥意思,有啥用。 | |||
for i in range(100): | for i in range(100): | ||
# the value range of `i` is 0 to 99 | |||
p = i/99.0 #不明白为什么要除以99为什么不是除以100,老师说了一嘴我没听明白。。 | p = i/99.0 #不明白为什么要除以99为什么不是除以100,老师说了一嘴我没听明白。。 | ||
l = p*(1920-50) | l = p*(1920-50) | ||
Line 19: | Line 26: | ||
g = random.randint(220,255) | g = random.randint(220,255) | ||
b = random.randint(111,225) | b = random.randint(111,225) | ||
im = Image.new("RGB", (1920,1080) , color=(r,g,b)) #("mode",(size),(color)) | im = Image.new("RGB", (1920,1080) , color=(r,g,b)) #("mode",(size),(color)) | ||
draw = ImageDraw.Draw(im) | draw = ImageDraw.Draw(im) | ||
sc=p*255 | sc=p*255 | ||
Line 26: | Line 33: | ||
draw.text((tp-2,680-2), str(i), fill=(0,0,0),font=f) | draw.text((tp-2,680-2), str(i), fill=(0,0,0),font=f) | ||
draw.text((tp+2,680+2), str(i), fill=(0,0,0),font=f) | draw.text((tp+2,680+2), str(i), fill=(0,0,0),font=f) | ||
# See `StringIO` docs above. `class StringIO.StringIO([buffer])` | |||
# StringIO provides a convenient means of working with text in memory using the file API (read, write. etc.). | |||
buf = StringIO() #不太懂buf指的是什么。。 | buf = StringIO() #不太懂buf指的是什么。。 | ||
im.save(buf, format="jpeg") | im.save(buf, format="jpeg") | ||
of.write(buf.getvalue()) #of = bucket | of.write(buf.getvalue()) #of = bucket | ||
print i | print i | ||
print "done" | print "done" | ||
</pre> | </pre> | ||
===等回复区=== |
Revision as of 22:48, 30 November 2015
大明白区
#这是一个mjpeg,然后用handbreak处理。最近上课就在讲这些东西. from PIL import Image, ImageDraw, ImageFont # Docs: https://docs.python.org/2/library/stringio.html # If you measure for speed, you should use cStringIO from cStringIO import StringIO #其实不太懂StringIO这个模块。 import random f = ImageFont.truetype("/Users/Stone/Desktop/Ptah-Regular.otf", size=300) # `open` is a Python build-in function # Docs: https://docs.python.org/2/library/functions.html#open # "frames.mjpeg"(filename) is the first param of open function. # 'w' for writing (truncating the file if it already exists) # append 'b' to the mode value to open the file in binary mode, which will improve portability. of = open("frames.mjpeg", "wb") #不太懂这句啥意思,有啥用。 for i in range(100): # the value range of `i` is 0 to 99 p = i/99.0 #不明白为什么要除以99为什么不是除以100,老师说了一嘴我没听明白。。 l = p*(1920-50) print p , l r = random.randint(1,225) g = random.randint(220,255) b = random.randint(111,225) im = Image.new("RGB", (1920,1080) , color=(r,g,b)) #("mode",(size),(color)) draw = ImageDraw.Draw(im) sc=p*255 tp = 80+(p*1870) draw.text((tp,680), str(i), fill=(255,0,0),font=f) draw.text((tp-2,680-2), str(i), fill=(0,0,0),font=f) draw.text((tp+2,680+2), str(i), fill=(0,0,0),font=f) # See `StringIO` docs above. `class StringIO.StringIO([buffer])` # StringIO provides a convenient means of working with text in memory using the file API (read, write. etc.). buf = StringIO() #不太懂buf指的是什么。。 im.save(buf, format="jpeg") of.write(buf.getvalue()) #of = bucket print i print "done"