- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚开始使用 Django 和 Python,正在尝试构建一个照片应用程序。该脚本正在生成缩略图,我想自己做。不幸的是我不明白 StringIO() 正在做什么。在这种情况下,Python 文档对我没有多大帮助。
有人可以向我解释一下 StringIO()
在这种特殊情况下的作用吗?
来自http://djangosnippets.org/snippets/1172/ :
def save(self):
from PIL import Image
#Original photo
imgFile = Image.open(self.image.path)
#Convert to RGB
if imgFile.mode not in ('L', 'RGB'):
imgFile = imgFile.convert('RGB')
#Save a thumbnail for each of the given dimensions
#The IMAGE_SIZES looks like:
#IMAGE_SIZES = { 'image_web' : (300, 348),
# 'image_large' : (600, 450),
# 'image_thumb' : (200, 200) }
#each of which corresponds to an ImageField of the same name
for field_name, size in self.IMAGE_SIZES.iteritems():
field = getattr(self, field_name)
working = imgFile.copy()
working.thumbnail(size, Image.ANTIALIAS)
fp = StringIO()
working.save(fp, "JPEG", quality=95)
cf = ContentFile(fp.getvalue())
field.save(name=self.image.name, content=cf, save=False);
#Save instance of Photo
super(Photo, self).save()
最佳答案
StringIO是一个可以用作类文件对象的类。您可以像使用常规文件一样使用它,只不过数据不是写入磁盘,而是写入内存中的缓冲区(字符串缓冲区)。
在此脚本中,图像首先保存到 StringIO 内存缓冲区,然后检索字符串的值并将其传递给 ContentFile 的构造函数以创建 ContentFile 的新实例,然后将其传递给字段保存功能。
我认为脚本使用 StringIO 的原因是 ContentFile 的构造函数接受一个字符串,写入然后读取 StringIO 文件是获取表示为字符串的图像内容的最简单方法。
作为旁注,我建议您查看 Django's ImageFile字段类型,已经足够满足我的图像相关需求了,而且比通过StringIO和ContentFiles更清晰。
关于python - 该脚本中 StringIO() 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8496711/
我正在使用 python 的 csv.DictReader 但我正在用这样的字符串初始化它: dict_reader = csv.DictReader(StringIO.StringIO(some_s
我已经用谷歌搜索并在 SO 上搜索这些缓冲区模块之间的区别。不过,我还是不是很明白,我觉得我看的一些帖子已经过时了。 在 Python 2.7.11 中,我使用 r = requests.get(ur
在我的模型中,我想通过覆盖保存方法来格式化图像字段 我在我的模型中做到了这一点 from PIL import Image as Img from io import StringIO from dj
我正在使用 Boost 的序列化库序列化大量数据以通过网络发送。 Boost 的序列化库需要一个中间文件来进行序列化和反序列化。传输的数据量使得执行所有这些操作变得很尴尬。将事情保存在内存中会好得多。
StringIO 是我们在从文本中读取 Pandas 数据帧时使用的类似文件的字符串缓冲区对象,例如"How to create a Pandas DataFrame from a string?"
我在使用这两个类时得到了不同的结果。 In [15]: StringIO.StringIO().write(u'\u2222') In [16]: cStringIO.StringIO().write
所以,我关注了this question为了得到一些声音播放 Music21 ,这是代码: from music21 import * import random def main(): # Set
显然,我是 Python 的新手。 我想在下面的代码中使用 StringIO :提取 example.xml import os os.chdir('d:/py/xml/') from lxml i
我只是无法让 StreamHandler 与 StringIO 一起工作以接受来自进程的日志记录。奇怪的是,到 stdout 的流工作得很好。 这是我的代码: from time import sle
我们正在使用一些我们没有源代码的编译Python代码。该代码提示用户输入,我们正在尝试自动化该部分。 基本上要求输入用户名、密码,然后根据特定情况询问一些不同的问题。我不知道编译后的函数是使用raw_
我正在使用以下代码在线检索图像: import Image import urllib2 import cStringIO url = 'http://storage.googleapis.com/b
尝试[ see it running here ]: from sys import stdout, stderr from cStringIO import StringIO from loggin
我在使用 Ruby 的 StringIO 类时观察到一些非常奇怪的行为。 在 irb 控制台中输入以下内容: 2.3.0 :002 > original_string = 'test' => "te
如何在 StringIO 中用另一个字符串替换字符串? - 我听说如果它们的长度相同是可能的。 尝试: from cStringIO import StringIO c = 'can\nhaz\nfo
我似乎得到了不同的输出: from StringIO import * file = open('1.bmp', 'r') print file.read(), '\n' print StringIO
在 Python 中,我有一个文件流,我想将它的一部分复制到 StringIO 中。我希望它尽可能快,副本最少。 但如果我这样做: data = file.read(SIZE) stream = St
我正在寻找一些 StringIO -类似的类,它允许我从我的程序的不同部分同时写入和读取。 从程序的一部分我想将字符写入(追加)到缓冲区,从另一部分我想读取它们。 StringIO 的问题如下: bu
我有 python 3.6。我想从另一个名为“run.py”的 python 文件执行名为“operation.py”的 python 文件。 在 operation.py 中,我执行 from cS
我刚刚开始使用 Django 和 Python,正在尝试构建一个照片应用程序。该脚本正在生成缩略图,我想自己做。不幸的是我不明白 StringIO() 正在做什么。在这种情况下,Python 文档对我
我正在尝试使用将 rml 转换为 pdf, import cStringIO buf = cStringIO.StringIO() rml2pdf.go(rml, outputFileName=buf
我是一名优秀的程序员,十分优秀!