第21章 项目2:画幅好画

Python Imaging Library (PIL)

下载地址:http://www.pythonware.com/products/pil/

点击运行,出现Python version 2.7 required, which was not found in the registry

这是由于系统是64位的原因,在安装python(32位)时,如果选择只为当前用户,以上问题是不会出现的,如果选择所有用户,那就用如下方法解决。

解决方法:

参考:http://www.cnblogs.com/min0208/archive/2012/05/24/2515584.html

新建一个register.py 文件,把如下代码贴进去,保存在F:\program\pybook

register.py

# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html

import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)


def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python"
, version, "is now registered!"
return
if
(QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"

if
__name__ == "__main__":
RegisterPy()

 

在该目录下运行命令:python register.py

显示"python 2.7 is already registered"

再安装setuptools的时候,就能自动识别出来python2.7了。

再次运行PIL-1.1.7.win32-py2.7.exe

安装成功。

 

数据网址:

http://www.swpc.noaa.gov/products/predicted-sunspot-number-and-radio-flux

http://services.swpc.noaa.gov/text/predicted-sunspot-radio-flux.txt

ReportLab

下载地址:https://pypi.python.org/pypi/reportlab

D:\Program Files\python27\Scripts目录下使用指令:

pip install D:\python64\reportlab-3.3.0-cp27-none-win_amd64.whl

安装成功

但是,使用from reportlab.graphics.shapes import Drawing, String

from reportlab.graphics import renderPDF时均报错:

显示缺少platypus,原因应为reportlab包不完整

D:\Program Files\python27\Lib\site-packages\reportlab\platypus的文件夹中没有py文件

reportlab包下载:https://pypi.python.org/simple/reportlab/

将解压包中D:\python64\reportlab-3.3.0\build\lib.win-amd64-2.7\reportlab\platypus目录下的对应全部.py文件复制过来

D:\Program Files\python27\Lib\site-packages\reportlab\pdfbase中也没有.py文件

D:\python64\reportlab-3.3.0\build\lib.win-amd64-2.7\reportlab\pdfbase中的复制过去

这次是pdfgen,仍是同一问题,解决方法相同。

终于正常工作了。

总结:将platypuspdfbasepdfgenpy文件补充完整即可修复该问题

 

 

 

 

 

21-1 Hello_report.py

from reportlab.graphics.shapes import Drawing, String
from reportlab.graphics import renderPDF

d = Drawing(100,100)
s = String(50, 50, 'Hello, World!', textAnchor = 'middle')

d.add(s)

renderPDF.drawToFile(d, 'hello.pdf','A simple PDF file')

但是在pycharm中运行报错,无法导入reportlab包,而IDLE中却可以运行。

解决方法:打开pycharmsettings,将ProjectInterpreter改为python.exe,之前默认的好像是ipy.exe

再次运行程序,不再报错,生成一个hello.pdf文件。

21-2 sunspots_roto.py

from reportlab.lib import colors
from reportlab.graphics.shapes import *
from reportlab.graphics import renderPDF

data = [
# Year Month Predicted High Low
(2016, 8, 89.8, 93.8, 85.8),
(2016, 9, 88.9, 92.9, 84.9),
(2016, 10, 88.3, 93.3, 83.3),
(2016, 11, 87.6, 93.6, 81.6),
(2016, 12, 87.2, 94.2, 80.2),
(2017, 1, 87.1, 95.1, 79.1),
(2017, 2, 86.7, 94.7, 78.7),
(2017, 3, 86.2, 95.2, 77.2),
(2017, 4, 85.2, 94.2, 76.2),
(2017, 5, 83.8, 92.8, 74.8),
]

drawing = Drawing(200, 150)

pred = [row[2]-40 for row in data]
high = [row[3]-40 for row in data]
low = [row[4]-40 for row in data]
times = [200*((row[0] + row[1]/12.0) - 2016)-110 for row in data]

drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue))
drawing.add(PolyLine(zip(times, high), strokeColor=colors.red))
drawing.add(PolyLine(zip(times, low), strokeColor=colors.green))
drawing.add(String(65,115, 'Sunspots', fontSize=18, fillColor=colors.red))

renderPDF.drawToFile(drawing, 'report1.pdf', 'Sunspots')

 

运行生成一个reportlab.pdf文件

 

21-3 sunspots.py

# coding=utf-8
from urllib import urlopen # 用于下载原数据
from reportlab.graphics.shapes import * # 使用shapes可以画出复杂的形状
from reportlab.graphics.charts.lineplots import LinePlot # chars包里包含许多常用的图形
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics import renderPDF # 用于渲染PDF文件

URL = 'http://services.swpc.noaa.gov/text/predicted-sunspot-radio-flux.txt'
COMMENT_CHARS = '#:'

drawing = Drawing(400, 200) # 初始化坐标原点,创建一个400*200像素大小 的PDF格式图形
data = [] # 读取Product中数据进行筛选,将正确数据添加到data元组中,data这个列表的每个元素实际上也是列表

# 下载数据并提取有效数据
for line in urlopen(URL).readlines():
if not line.isspace() and not line[0] in COMMENT_CHARS: # line.isspace()判断改行是不是为空行注释
data.append([float(n) for n in line.split()])

# 获取data元组中用于画图的对应数据
pred = [row[2] for row in data]
high = [row[3] for row in data]
low = [row[4] for row in data]
times = [row[0] + row[1]/12.0 for row in data]

# LinePlot类实例化不需要任务参数,设置主要特性是:x。y、height、width和data,data点的坐标列表。
lp = LinePlot()
lp.x = 50
lp.y = 50
lp.height = 125
lp.width = 300

"""
zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),
然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。
因为画图需要用的数据,必须是一定格式的,所以用zip组装了数据:[[ (,), (,) ..... ]]
"""
lp.data = [zip(times, pred), zip(times, high), zip(times, low)]
# 为每条线调置笔画颜色
lp.lines[0].strokeColor = colors.blue
lp.lines[1].strokeColor = colors.red
lp.lines[2].strokeColor = colors.green

drawing.add(lp) #将设定的LinePlost类添加到drawing
drawing.add(String(250,150, 'Sunspots', fontSize=14, fillColor=colors.red)) #添加一个String对象到drawing中

renderPDF.drawToFile(drawing, 'report2.pdf', 'Sunspots') #renderPDF.drawToFile是把内容保存到report2.pdf

运行生成一个report2.pdf文件

补充:

  1. 用pip或easy_install安装PIL时,会出现 Unable to find vcvarsall.bat

    解决方法:

    在执行setup命令前设置一下VS90COMNTOOLS

    Visual Studio 2010 (VS10): SET VS90COMNTOOLS=%VS100COMNTOOLS%

    Visual Studio 2012 (VS11): SET VS90COMNTOOLS=%VS110COMNTOOLS%

    Visual Studio 2013 (VS12): SET VS90COMNTOOLS=%VS120COMNTOOLS%

    Visual Studio 2015 (VS14): SET VS90COMNTOOLS=%VS140COMNTOOLS%

    或者设置系统变量VS90COMNTOOLS=%VS140COMNTOOLS%

    但依然报错:

    command 'D:\\Program Files\\Microsoft Visual Studio 14.0\\VC\\BIN\\amd64\\cl

    不知道如何解决。

    2. Reportlab.exe文件安装方法

    下载地址:http://www.reportlab.com/ftp/

    由于之前配置过register.py,因此运行该文件不会报错找不到python2.7

    3. platypus

    pip install platypus

    这个包好像和reportlab无关

转载于:https://www.cnblogs.com/Sumomo0516/p/6131646.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值