小编fia*_*cre的帖子

Django:改变图像大小并上传到S3

我继承了一个Django项目,我们已将图像移动到S3

其中一个模型是典型的用户配置文件

class Profile(UUIDBase):

    first_name = models.CharField(_("First Name"), max_length=20)
    last_name = models.CharField(_("Last Name"), max_length=20, null=True)
    profile_image = models.ImageField(
        _("Profile Image"),
        upload_to=profile_image_name,
         max_length=254,
        blank=True,
        null=True
    )
    profile_image_thumb = models.ImageField(
        _("Profile Image Thumbnail"),
        upload_to=profile_image_name,
        max_length=254,
        blank=True,
       null=True
    )
    ... other fields
Run Code Online (Sandbox Code Playgroud)

profile_image_name功能在哪里:

def profile_image_name(instance, filename):
    if filename:
        target_dir = 'uploads/profile_img/'
        _, ext = filename.rsplit('.', 1)
        filename = str(instance.uid) + '.' + ext
        return '/'.join([target_dir, filename])
Run Code Online (Sandbox Code Playgroud)

我有一些有用的代码:

@shared_task
def resize_image(image_path, dim_x, append_str='_resized', **kwargs):
    '''
    resize any image_obj while maintaining aspect ratio
    ''' …
Run Code Online (Sandbox Code Playgroud)

python django amazon-s3 image-resizing python-imaging-library

7
推荐指数
1
解决办法
583
查看次数

Django测试 - 无法删除并重新创建测试数据库

我在Mac OS X上运行Django 1.9,Postgres 9.5.1

我跑的时候 /manage.py test --settings=myproj.settings.local

我明白了:

Creating test database for alias 'default'...
Creating test database for alias 'userlocation'...
Got an error creating the test database: database "test_myproj" already exists

Type 'yes' if you would like to try deleting the test database 'test_myproj', or 'no' to cancel: yes
Destroying old test database for alias 'userlocation'...
Got an error recreating the test database: database "test_myproj" is being accessed by other users
DETAIL:  There is 1 other session using …
Run Code Online (Sandbox Code Playgroud)

python django postgresql postgis

7
推荐指数
2
解决办法
3874
查看次数

Python图像库和KeyError:'JPG'

这有效:

from PIL import Image, ImageFont, ImageDraw

def create_image_file(name='test.jpeg', ext='jpeg', size=(500, 500), color=(5, 179, 200)):
    file_obj = open(name, 'w')
    image = Image.new("RGBA", size=size, color=color)
    usr_font = ImageFont.truetype(
        "/Users/myuser/ENV/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf", 59)
    d_usr = ImageDraw.Draw(image)
    d_usr = d_usr.text((105, 280), "Test Image", (0, 0, 0), font=usr_font)
    image.save(file_obj, ext)
    file_obj.close()

if __name__ == '__main__':
    f = create_image_file()
Run Code Online (Sandbox Code Playgroud)

但是,如果我将参数更改为:

def create_image_file(name='test.jpg', ext='jpg', ...)
Run Code Online (Sandbox Code Playgroud)

提出了一个例外:

File "/Users/myuser/project/venv/lib/python2.7/site-packages/PIL/Image.py", line 1681, in save
    save_handler = SAVE[format.upper()]
KeyError: 'JPG'
Run Code Online (Sandbox Code Playgroud)

我需要使用具有.jpg扩展名的用户上传图像.这是Mac特有的问题吗?我可以做些什么来将格式数据添加到Image lib吗?

python macos jpeg python-imaging-library

6
推荐指数
1
解决办法
8120
查看次数