生成内存中图像以进行 Django 测试

发布于 2024-12-22 23:12:04 字数 362 浏览 2 评论 0原文

是否可以生成内存中图像用于测试目的?

这是我当前的代码:

  def test_issue_add_post(self):
        url = reverse('issues_issue_add')
        image = 'cover.jpg'
        data = {
            'title': 'Flying Cars',
            'cover': image,
        }
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, 302)

Is it possible to generate an in-memory image for testing purposes?

Here is my current code:

  def test_issue_add_post(self):
        url = reverse('issues_issue_add')
        image = 'cover.jpg'
        data = {
            'title': 'Flying Cars',
            'cover': image,
        }
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, 302)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你与昨日 2024-12-29 23:12:04

要生成纯红色的 200x200 测试图像:

import Image
size = (200,200)
color = (255,0,0,0)
img = Image.new("RGBA",size,color)

要将其转换为类似文件的对象,则:

import StringIO
f = StringIO.StringIO(img.tostring())

http:// /effbot.org/imagingbook/image.htm

To generate a 200x200 test image of solid red:

import Image
size = (200,200)
color = (255,0,0,0)
img = Image.new("RGBA",size,color)

To convert it to a file-like object, then:

import StringIO
f = StringIO.StringIO(img.tostring())

http://effbot.org/imagingbook/image.htm

︶ ̄淡然 2024-12-29 23:12:04

Jason 接受的答案在 Django 1.5 中对我不起作用

假设生成的文件要从单元测试中保存到模型的 ImageField 中,我需要通过创建一个 来更进一步ContentFile 使其正常工作:

from PIL import Image
from StringIO import StringIO

from django.core.files.base import ContentFile

image_file = StringIO()
image = Image.new('RGBA', size=(50,50), color=(256,0,0))
image.save(image_file, 'png')
image_file.seek(0)

django_friendly_file = ContentFile(image_file.read(), 'test.png')

Jason's accepted answer is not working for me in Django 1.5

Assuming the generated file is to be saved to a model's ImageField from within a unit test, I needed to take it a step further by creating a ContentFile to get it to work:

from PIL import Image
from StringIO import StringIO

from django.core.files.base import ContentFile

image_file = StringIO()
image = Image.new('RGBA', size=(50,50), color=(256,0,0))
image.save(image_file, 'png')
image_file.seek(0)

django_friendly_file = ContentFile(image_file.read(), 'test.png')
大海や 2024-12-29 23:12:04

因此,如果 client.post 需要一个类似对象的文件,您可以创建一个示例图像(如果您想在测试后直观地检查结果),或者只是制作一个 1px png 并从控制台中读出它,

open('1px.png', 'rb').read()

在我的情况下,控制台将其转储出来,

image_data = '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\tpHYs\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\x07tIME\x07\xdb\x0c\x17\x020;\xd1\xda\xcf\xd2\x00\x00\x00\x0cIDAT\x08\xd7c\xf8\xff\xff?\x00\x05\xfe\x02\xfe\xdc\xccY\xe7\x00\x00\x00\x00IEND\xaeB`\x82'

然后您可以使用 StringIO 充当类似文件的对象,因此上面的图像将是

from StringIO import StringIO

def test_issue_add_post(self):
    ...
    image = StringIO(image_data)
    ...

,并且您将拥有一个带有图像数据的类似文件的对象

So if client.post is expecting a file like object, you could create an example image (if you want to visually check result after tests) or just make a 1px png and read it out from console

open('1px.png', 'rb').read()

which in my case dumped out

image_data = '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\tpHYs\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\x07tIME\x07\xdb\x0c\x17\x020;\xd1\xda\xcf\xd2\x00\x00\x00\x0cIDAT\x08\xd7c\xf8\xff\xff?\x00\x05\xfe\x02\xfe\xdc\xccY\xe7\x00\x00\x00\x00IEND\xaeB`\x82'

then you can use StringIO which acts as a file like object, so above, image would be

from StringIO import StringIO

def test_issue_add_post(self):
    ...
    image = StringIO(image_data)
    ...

and you'll have a file like object with the image data

星軌x 2024-12-29 23:12:04

在Python 3中

from io import BytesIO
from PIL import Image

image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))
file = BytesIO(image.tobytes())
file.name = 'test.png'
file.seek(0)
#  + + + django_friendly_file = ContentFile(file.read(), 'test.png') # year 2019, django 2.2.1  -works 

In Python 3

from io import BytesIO
from PIL import Image

image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))
file = BytesIO(image.tobytes())
file.name = 'test.png'
file.seek(0)
#  + + + django_friendly_file = ContentFile(file.read(), 'test.png') # year 2019, django 2.2.1  -works 
╰ゝ天使的微笑 2024-12-29 23:12:04

感谢爱德华多的帮助,我找到了一个可行的解决方案。

from StringIO import StringIO
import Image

file = StringIO()
image = Image.new("RGBA", size=(50,50), color=(256,0,0))
image.save(file, 'png')
file.name = 'test.png'
file.seek(0)

Thanks to help from Eduardo, I was able to get a working solution.

from StringIO import StringIO
import Image

file = StringIO()
image = Image.new("RGBA", size=(50,50), color=(256,0,0))
image.save(file, 'png')
file.name = 'test.png'
file.seek(0)
物价感观 2024-12-29 23:12:04

你用过PIL模块吗?它可以让你操作图像 - 并且也应该允许创建。

事实上,这是一个博客条目,其中包含一些代码
http://bradmontgomery.blogspot.com/2008/07 /django-generate-image-with-pil.html

不知道你的测试机器是否有互联网连接,但你也可以从谷歌拉下随机图像来改变测试 数据?

Have you used the PIL module? It lets you manipulate images - and should allow creation as well.

In fact, here's a blog entry with some code that does it
http://bradmontgomery.blogspot.com/2008/07/django-generating-image-with-pil.html

Dont know whether you test machine has an internet connection, but you could also pull down random images from google to vary the test data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文