python Loading简单效果

这篇博客展示了如何使用Python创建简单的加载效果。通过代码实现,详细解释了实现过程,并附带了运行效果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码

import time
print("---runoob example Loading---")
print("Loading",end="")
for i in range(20):
	print(".",end='',Flush=True)
	#sep(".") 以.分隔,若为print(".",end='',Flush=True,sep=".")
	#.的数量会加倍
	time.sleep(0.5)

效果

在这里插入图片描述

### 使用 Python 创建具有 3D 旋转效果的照片墙 为了实现这一目标,可以利用 `opencv-python` 和 `PIL` 来处理图像,并借助 Pygame 或者其他图形界面如 PyQt 来构建带有动画效果的应用程序。下面提供了一个基于 Pygame 的简单例子。 #### 安装所需依赖包 首先需要安装必要的软件包: ```bash pip install opencv-python pillow pygame ``` #### 导入模块并初始化环境 准备阶段包括导入所需的 Python 模块以及设置初始参数。 ```python import sys, os import pygame from PIL import Image import cv2 import numpy as np ``` #### 加载图片资源 定义函数读取本地磁盘上的多张照片到内存中以便后续操作。 ```python def load_images_from_folder(folder): images = [] for filename in os.listdir(folder): img_path = os.path.join(folder,filename) try: pil_image = Image.open(img_path).convert('RGB') open_cv_image = np.array(pil_image) # Convert RGB to BGR open_cv_image = open_cv_image[:, :, ::-1].copy() images.append(open_cv_image) except Exception as e: print(f"Error loading image {img_path}: ", str(e)) return images ``` #### 构建三维变换矩阵 此部分实现了围绕 Y 轴的旋转变换矩阵计算方法。 ```python def get_rotation_matrix(angle_degrees): angle_radians = np.radians(angle_degrees) cos_angle = np.cos(angle_radians) sin_angle = np.sin(angle_radians) rotation_matrix = np.float32([ [cos_angle,-sin_angle], [sin_angle, cos_angle]]) return rotation_matrix ``` #### 渲染场景逻辑 编主循环负责更新每一帧的画面内容,在这里会应用之前提到过的旋转变化给每一张图片。 ```python pygame.init() screen_width = 800 screen_height = 600 display_surface = pygame.display.set_mode((screen_width, screen_height)) images = load_images_from_folder('./photos')[:9] # 只选取前九张作为样本测试 image_size = (200, 200) center_x = int(screen_width / 2) center_y = int(screen_height / 2) running = True angle_offset = 0 while running: display_surface.fill([255, 255, 255]) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False for i,img in enumerate(images): rotated_img = cv2.warpAffine( src=img, M=get_rotation_matrix(-i*20 + angle_offset), dsize=image_size[::-1],borderValue=(255,255,255)) surf = pygame.image.frombuffer(rotated_img.tobytes(),rotated_img.shape[1::-1],"BGR") rect = surf.get_rect(center=( center_x+(i%3)*(int(image_size[0]*1.5)-image_size[0]), center_y+(i//3)*(int(image_size[1]*1.5)-image_size[1]))) display_surface.blit(surf,rect) pygame.display.flip() angle_offset += .5 pygame.time.Clock().tick(30) pygame.quit() sys.exit() ``` 这段代码展示了如何通过调整角度偏移量使各个位置上的图片产生连续不断的自转运动[^1][^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值