子曰:
學而不思則罔,思而不學則殆。
因此請思考底下互動程式意欲何為乎??!!
>>> import mnist_loader >>> training_data, validation_data, test_data = \ ... mnist_loader.load_data_wrapper() >>> import network >>> net = network.Network([784, 30, 10]) >>> npzfile = network.np.load("swb.npz") >>> npzfile.files ['s', 'b2', 'w2', 'w1', 'b1'] >>> net.weights[0] = npzfile["w1"] >>> net.weights[1] = npzfile["w2"] >>> net.biases[0] = npzfile["b1"] >>> net.biases[1] = npzfile["b2"] >>> net.evaluate(test_data=test_data) 9499 >>> from PIL import Image, ImageDraw, ImageFont >>> import matplotlib.pyplot as plt >>> font = ImageFont.truetype("/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc",28) >>> im_0 = Image.new("RGB",(28,28)) >>> draw_0 = ImageDraw.Draw(im_0) >>> draw_0.text((0,0), "0", font=font) >>> np_im_0 = network.np.asarray(im_0) >>> plt.imshow(np_im_0,cmap='Greys', interpolation='nearest') <matplotlib.image.AxesImage object at 0x5466d50> >>> plt.show() >>> np_im_0.size 2352
>>> im_0g = im_0.convert('L') >>> np_im_0g = network.np.asarray(im_0g) >>> plt.imshow(np_im_0g,cmap='Greys', interpolation='nearest') <matplotlib.image.AxesImage object at 0x40ba290> >>> plt.show() >>> np_im_0g.size 784 >>> np_im_0gc = np_im_0g.reshape(784,1) >>> net.feedforward(np_im_0gc) array([[ 4.97894890e-13], [ 3.26055003e-07], [ 2.68689931e-07], [ 9.99951724e-01], [ 6.39075045e-09], [ 3.00937517e-09], [ 5.28623209e-13], [ 9.97879811e-01], [ 5.07832499e-11], [ 5.27639416e-17]]) >>> network.np.argmax(net.feedforward(np_im_0gc)) 3 >>>
【參考資料】
Image
Module
The Image
module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.
Examples
The following script loads an image, rotates it 45 degrees, and displays it using an external viewer (usually xv on Unix, and the paint program on Windows).
Open, rotate, and display an image (using the default viewer)
from PIL import Image im = Image.open("bride.jpg") im.rotate(45).show()
───
ImageFont
Module
The ImageFont
module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the PIL.ImageDraw.Draw.text()
method.
PIL uses its own font file format to store bitmap fonts. You can use the :command`pilfont` utility to convert BDF and PCF font descriptors (X window font formats) to this format.
Starting with version 1.1.4, PIL can be configured to support TrueType and OpenType fonts (as well as other font formats supported by the FreeType library). For earlier versions, TrueType support is only available as part of the imToolkit package
Example
from PIL import ImageFont, ImageDraw draw = ImageDraw.Draw(image) # use a bitmap font font = ImageFont.load("arial.pil") draw.text((10, 10), "hello", font=font) # use a truetype font font = ImageFont.truetype("arial.ttf", 15) draw.text((10, 25), "world", font=font)
───
ImageDraw
Module
The ImageDraw
module provide simple 2D graphics for Image
objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.
For a more advanced drawing library for PIL, see the aggdraw module.
Example: Draw a gray cross over an image
from PIL import Image, ImageDraw
im = Image.open("lena.pgm")
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw
# write to stdout
im.save(sys.stdout, "PNG")
───