Initial commit: Django gallery project
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
144
gallery/management/commands/import_example_images.py
Normal file
144
gallery/management/commands/import_example_images.py
Normal file
@@ -0,0 +1,144 @@
|
||||
import os
|
||||
import random
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.core.files import File
|
||||
from gallery.models import Artwork, Category, About
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '导入示例图片到画廊数据库'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write(self.style.SUCCESS('开始导入示例图片...'))
|
||||
|
||||
# 1. 创建分类
|
||||
categories = self.create_categories()
|
||||
self.stdout.write(self.style.SUCCESS(f'创建了 {len(categories)} 个分类'))
|
||||
|
||||
# 2. 创建关于页面
|
||||
self.create_about_page()
|
||||
self.stdout.write(self.style.SUCCESS('创建了关于页面'))
|
||||
|
||||
# 3. 导入示例图片
|
||||
example_dir = os.path.join(settings.BASE_DIR, 'example_img')
|
||||
if os.path.exists(example_dir):
|
||||
image_files = [f for f in os.listdir(example_dir) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
||||
self.stdout.write(self.style.SUCCESS(f'找到 {len(image_files)} 张示例图片'))
|
||||
|
||||
artworks_created = 0
|
||||
for i, filename in enumerate(image_files):
|
||||
try:
|
||||
# 检查是否已经存在相同文件名的作品
|
||||
if Artwork.objects.filter(image__endswith=filename).exists():
|
||||
self.stdout.write(self.style.WARNING(f' 已存在: {filename},跳过'))
|
||||
continue
|
||||
|
||||
artwork = self.create_artwork_from_image(example_dir, filename, i, categories)
|
||||
if artwork:
|
||||
artworks_created += 1
|
||||
self.stdout.write(self.style.SUCCESS(f' 已导入: {artwork.title}'))
|
||||
except Exception as e:
|
||||
self.stdout.write(self.style.ERROR(f' 导入失败 {filename}: {str(e)}'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'成功导入 {artworks_created} 个作品'))
|
||||
else:
|
||||
self.stdout.write(self.style.WARNING('未找到 example_img 目录,跳过图片导入'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('示例数据导入完成!'))
|
||||
|
||||
def create_categories(self):
|
||||
"""创建作品分类"""
|
||||
categories_data = [
|
||||
{'name': '风景摄影', 'slug': 'landscape'},
|
||||
{'name': '人像摄影', 'slug': 'portrait'},
|
||||
{'name': '城市建筑', 'slug': 'architecture'},
|
||||
{'name': '自然生态', 'slug': 'nature'},
|
||||
{'name': '抽象艺术', 'slug': 'abstract'},
|
||||
]
|
||||
|
||||
categories = []
|
||||
for data in categories_data:
|
||||
category, created = Category.objects.get_or_create(
|
||||
slug=data['slug'],
|
||||
defaults={'name': data['name']}
|
||||
)
|
||||
if created:
|
||||
categories.append(category)
|
||||
|
||||
return categories
|
||||
|
||||
def create_about_page(self):
|
||||
"""创建关于页面"""
|
||||
if not About.objects.exists():
|
||||
About.objects.create(
|
||||
title='关于 YITAO-REN GALLERY',
|
||||
content='''YITAO-REN GALLERY 是一个专注于现代摄影艺术展示的在线平台。
|
||||
|
||||
我们的使命是发现和推广具有独特视角和艺术价值的摄影作品,为艺术家和艺术爱好者搭建一个交流与展示的空间。
|
||||
|
||||
画廊成立于2026年,名字来源于创始人对于摄影艺术的热爱与追求。"Yitao"代表着艺术的独特道路,"Ren"象征着人文关怀。我们相信,每一幅摄影作品都是摄影师与世界对话的方式,是瞬间与永恒的完美结合。
|
||||
|
||||
在 YITAO-REN GALLERY,您可以:
|
||||
- 欣赏高质量的现代摄影艺术作品
|
||||
- 了解新兴摄影艺术家的创作理念
|
||||
- 参与线上艺术交流活动
|
||||
- 收藏您喜爱的摄影作品
|
||||
|
||||
我们致力于为每一位访问者提供优质的视觉体验,让艺术触手可及。'''
|
||||
)
|
||||
|
||||
def create_artwork_from_image(self, example_dir, filename, index, categories):
|
||||
"""从图片文件创建作品"""
|
||||
# 作品标题和描述
|
||||
titles = [
|
||||
'晨曦微光', '城市脉络', '静谧时光', '光影交错', '自然韵律',
|
||||
'建筑美学', '人文纪实', '色彩交响', '视觉诗篇', '时空印记',
|
||||
'抽象表达', '情感共鸣', '创意无限'
|
||||
]
|
||||
|
||||
descriptions = [
|
||||
'捕捉清晨第一缕阳光洒在大地上的温暖瞬间,展现自然与光的和谐对话。',
|
||||
'现代都市的脉络与节奏,钢筋水泥中的生命律动,城市发展的视觉记录。',
|
||||
'时光在静谧中流淌,记录那些被遗忘的角落和沉淀的记忆。',
|
||||
'光与影的舞蹈,明暗对比中展现物体的立体感和空间层次。',
|
||||
'大自然的韵律与节奏,山川河流的壮美与微观世界的精妙。',
|
||||
'建筑的结构美学与空间哲学,几何线条中的力量与平衡。',
|
||||
'人文关怀的视觉表达,记录时代变迁中的人物与故事。',
|
||||
'色彩的碰撞与融合,视觉上的交响乐,情感的温度计。',
|
||||
'用镜头书写的视觉诗篇,每一帧都是情感的抒发和思想的表达。',
|
||||
'时间与空间的交汇点,记录那些转瞬即逝的珍贵时刻。',
|
||||
'超越具象的视觉语言,用形状、色彩和纹理表达内在情感。',
|
||||
'触动心灵的情感连接,让观者与作品产生深层次的共鸣。',
|
||||
'突破传统的创意表达,探索摄影艺术的无限可能性。'
|
||||
]
|
||||
|
||||
# 选择标题和描述
|
||||
title_index = index % len(titles)
|
||||
description_index = index % len(descriptions)
|
||||
|
||||
# 随机选择分类
|
||||
category = random.choice(categories) if categories else None
|
||||
|
||||
# 根据图片比例智能选择网格尺寸
|
||||
# 这里先使用默认值,图片保存时会根据实际尺寸调整
|
||||
grid_size = 'medium'
|
||||
|
||||
# 创建作品
|
||||
artwork = Artwork(
|
||||
title=titles[title_index],
|
||||
description=descriptions[description_index],
|
||||
order=index * 10, # 按导入顺序排序
|
||||
grid_size=grid_size,
|
||||
category=category
|
||||
)
|
||||
|
||||
# 保存图片
|
||||
image_path = os.path.join(example_dir, filename)
|
||||
with open(image_path, 'rb') as f:
|
||||
artwork.image.save(filename, File(f), save=False)
|
||||
|
||||
# 保存作品
|
||||
artwork.save()
|
||||
|
||||
return artwork
|
||||
Reference in New Issue
Block a user