我有一个使用Cairo 图像表面的小型 PyGI 项目,然后我使用表面图案进行缩放并在 Gtk.DrawingArea 上渲染。
我想将缩放版本写入PNG 文件。我试图用Surface.write_to_png()从原始表面写入,但它只写入原始(即非缩放)大小,所以我被困在那里。
然后我想我也许可以从 Gtk.DrawingArea 获取渲染图像并将其写入磁盘,但我还没有找到如何在 PyGI 中做到这一点(这似乎只能在 GTK+ 2 中实现 -将 gtk.DrawingArea 保存到文件)。所以我想弄清楚如何将缩放后的图像写入磁盘。
这是创建表面、放大并渲染它的代码:
def on_drawingarea1_draw (self, widget, ctx, data=None):
# 'widget' is a Gtk.DrawingArea
# 'ctx' is the Cairo context
text = self.ui.entry1.get_text()
if text == '':
return
# Get the data and encode it into the image
version, size, im = qrencode.encode(text)
im = im.convert('RGBA') # Cairo expects RGB
# Create a …Run Code Online (Sandbox Code Playgroud) 我想在Mac OSX Lion上为Cairo安装Python绑定,以便我可以将它们与Python 2.7的Apple版本一起使用.我倾向于使用Python模块等安装easy_install,但在这种情况下似乎不起作用:
$ sudo easy_install pycairo
Searching for pycairo
Reading http://pypi.python.org/simple/pycairo/
Reading http://cairographics.org/pycairo
Best match: pycairo 1.10.0
Downloading http://cairographics.org/releases/pycairo-1.10.0.tar.bz2
Processing pycairo-1.10.0.tar.bz2
error: Couldn't find a setup script in /tmp/easy_install-BYpm4Y/pycairo-1.10.0.tar.bz2
Run Code Online (Sandbox Code Playgroud)
Stack Overflow上的其他地方也有类似的问题,但答案并没有解释我在这里做错了什么.
这个问题与Python 2.6有关,接受的答案使用Macport而不是easy_install,而另一个答案似乎是建议使用我上面尝试的方法(失败):
这个问题涉及从源代码构建pycairo,我不想这样做:
我想用Pycairo生成一个动态创建的png图像,然后用它来设置Django.我读到这个:使用Django提供动态生成的图像.
有没有办法将Pycairo表面的数据直接传输到HTTP响应中?我现在这样做:
data = surface.to_rgba()
im = Image.frombuffer ("RGBA", (width, height), data, "raw", "RGBA", 0,1)
response = HttpResponse(mimetype="image/png")
im.save(response, "PNG")
return response
Run Code Online (Sandbox Code Playgroud)
但它实际上不起作用,因为没有to_rgba调用(这个调用我发现使用谷歌代码,但不起作用).
编辑:to_rgba可以用正确的调用get_data()替换,但我仍然想知道我是否可以完全绕过PIL.
又是我.这是一个与我正在做的项目相关的代码,称为Twitter数据上的情感分析.以下代码主要用于显示正负推文的数量,我在下面给出了错误.
from pyspark import SparkConf, SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils
import operator
import numpy as np
import matplotlib.pyplot as plt
def main():
conf = SparkConf().setMaster("local[2]").setAppName("Streamer")
sc = SparkContext(conf=conf)
# Creating a streaming context with batch interval of 10 sec
ssc = StreamingContext(sc, 10)
ssc.checkpoint("checkpoint")
pwords = load_wordlist("positive.txt")
nwords = load_wordlist("negative.txt")
counts = stream(ssc, pwords, nwords, 100)
make_plot(counts)
def make_plot(counts):
"""
This function plots the counts of positive and negative words for each timestep.
"""
positiveCounts = …Run Code Online (Sandbox Code Playgroud)