使用python将文件夹中的图片批量旋转(扩充数据集)

from PIL import Image
import os

# 定义函数,旋转图片并保存
def rotate_images(input_dir, output_dir, angle):
    for filename in os.listdir(input_dir):
        # 判断文件是否为图片
        if filename.endswith('.jpg') or filename.endswith('.png'):
            # 打开图片
            img = Image.open(os.path.join(input_dir, filename))
            # 旋转图片,使用expand=True参数来确保旋转后的图像不会被裁剪,若要裁剪图像,可以将此参数设置为False。
            rotated_img = img.rotate(angle, expand=True)
            # 保存旋转后的图片
            rotated_img.save(os.path.join(output_dir, filename))

# 输入文件夹路径、输出文件夹路径、旋转角度
input_dir = 'D:\\images'
output_dir = 'D:\\images_out'
angle = 345  # 逆时针旋转度数

rotate_images(input_dir, output_dir, angle)
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个简单的 Python 俄罗斯方块游戏的示例代码: ```python import pygame import random # 颜色定义 BLACK = (0, 0, 0) WHITE = (255, 255, 255) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) PURPLE = (255, 0, 255) YELLOW = (255, 255, 0) # 方块定义 shapes = [ [[1, 1, 1], [0, 1, 0]], [[0, 2, 2], [2, 2, 0]], [[3, 3, 0], [0, 3, 3]], [[4, 0, 0], [4, 4, 4]], [[0, 0, 5], [5, 5, 5]], [[6, 6], [6, 6]], [[7, 7, 7, 7]] ] # 初始化 Pygame pygame.init() # 设置窗口大小 screen_width = 640 screen_height = 480 screen = pygame.display.set_mode([screen_width, screen_height]) # 设置窗口标题 pygame.display.set_caption("俄罗斯方块") # 时钟对象 clock = pygame.time.Clock() # 字体对象 font = pygame.font.Font(None, 36) # 方块大小 block_size = 20 # 方块颜色 colors = [BLACK, BLUE, GREEN, PURPLE, RED, YELLOW] # 游戏区域大小 play_width = 10 * block_size play_height = 20 * block_size # 游戏区域位置 play_x = (screen_width - play_width) // 2 play_y = screen_height - play_height - 10 # 方块初始位置 start_x = play_x + play_width // 2 start_y = play_y + block_size # 游戏区域数组 play_area = [[0 for _ in range(10)] for _ in range(20)] # 方块对象 class Block: def __init__(self): self.shape = random.choice(shapes) self.color = random.choice(colors) self.rotation = 0 self.x = start_x - len(self.shape[0]) // 2 * block_size self.y = start_y - len(self.shape) * block_size # 向左移动 def move_left(self): self.x -= block_size if self.check_collision(): self.x += block_size # 向右移动 def move_right(self): self.x += block_size if self.check_collision(): self.x -= block_size # 向下移动 def move_down(self): self.y += block_size if self.check_collision(): self.y -= block_size self.lock() # 旋转 def rotate(self): self.rotation = (self.rotation + 1) % len(self.shape) if self.check_collision(): self.rotation = (self.rotation - 1) % len(self.shape) # 检查碰撞 def check_collision(self): for i in range(len(self.shape)): for j in range(len(self.shape[0])): if self.shape[i][j] != 0: x = self.x + j * block_size y = self.y + i * block_size if x < play_x or x >= play_x + play_width or y >= play_y + play_height: return True if y >= play_y: if play_area[(y - play_y) // block_size][x // block_size] != 0: return True return False # 固定方块 def lock(self): for i in range(len(self.shape)): for j in range(len(self.shape[0])): if self.shape[i][j] != 0: x = self.x + j * block_size y = self.y + i * block_size if y >= play_y: play_area[(y - play_y) // block_size][x // block_size] = self.color # 绘制方块 def draw(self, surface): for i in range(len(self.shape)): for j in range(len(self.shape[0])): if self.shape[i][j] != 0: x = self.x + j * block_size y = self.y + i * block_size pygame.draw.rect(surface, self.color, [x, y, block_size, block_size]) # 消除一行 def clear_row(row): for i in range(row, 0, -1): for j in range(len(play_area[0])): play_area[i][j] = play_area[i-1][j] # 消除满行 def clear_rows(): rows_cleared = 0 for i in range(len(play_area)): if all(play_area[i]): clear_row(i) rows_cleared += 1 return rows_cleared # 绘制游戏区域 def draw_play_area(surface): for i in range(len(play_area)): for j in range(len(play_area[0])): if play_area[i][j] != 0: x = play_x + j * block_size y = play_y + i * block_size pygame.draw.rect(surface, colors[play_area[i][j]], [x, y, block_size, block_size], 0) pygame.draw.rect(surface, WHITE, [play_x + j * block_size, play_y + i * block_size, block_size, block_size], 1) # 游戏主循环 def main(): game_over = False block = Block() next_block = Block() score = 0 while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: block.move_left() elif event.key == pygame.K_RIGHT: block.move_right() elif event.key == pygame.K_DOWN: block.move_down() elif event.key == pygame.K_UP: block.rotate() # 更新方块位置 block.move_down() # 如果方块已经固定,生成新的方块 if block.y <= start_y - len(block.shape) * block_size: block = next_block next_block = Block() # 消除满行 rows_cleared = clear_rows() score += rows_cleared * 100 # 绘制游戏界面 screen.fill(BLACK) draw_play_area(screen) block.draw(screen) next_block.draw(screen) score_text = font.render("Score: {}".format(score), True, WHITE) screen.blit(score_text, [10, 10]) pygame.display.flip() # 控制游戏帧率 clock.tick(5) pygame.quit() if __name__ == '__main__': main() ``` 这是一个基本的俄罗斯方块游戏,仅供参考。您可以根据自己的需要进行修改和扩展。 ### 回答2: Python俄罗斯方块是一种模拟经典游戏俄罗斯方块的Python编程版本。俄罗斯方块是一款由苏联工程师亚历克斯·帕金诺在1984年设计的游戏,现在已经成为了全球流行的游戏之一。 Python俄罗斯方块的实现过程是通过使用Python编程语言,结合一些基础的图形库和游戏框架,编写代码来模拟游戏的整个过程。 在Python俄罗斯方块,我们可以使用键盘的方向键来控制方块的移动和旋转。游戏的主要目标是通过旋转和移动不同形状的方块,让它们在垂直方向上堆积起来,形成完整的水平线,当水平线被完整填满时,该线会消失并获得分数,游戏的难度逐渐增加。 Python俄罗斯方块的编程过程,需要考虑到方块的形状、位置、移动逻辑、碰撞检测以及分数计算等方面。通过适当的设计和编码,可以实现一个流畅的游戏体验。 Python俄罗斯方块是一个很好的编程实践项目,可以让我们学习和理解Python编程语言的基本语法和数据结构,同时也可以体验到游戏开发的乐趣。通过对源代码的修改和扩展,还可以实现一些自定义的功能和特性,使游戏更加个性化。 最后,Python俄罗斯方块不仅仅是一个游戏,更是一个可以培养逻辑思维能力、反应能力和空间感知能力的练习项目。无论是初学者还是有经验的开发者,都可以通过实践和探索,提升自己的编程技能和游戏设计能力。 ### 回答3: Python俄罗斯方块是一款基于Python语言开发的俄罗斯方块游戏。俄罗斯方块是一款经典的益智游戏,通过不断下落的不同形状的方块,玩家需要将它们堆积在一起,填满一行或多行后消除,从而获取得分。 Python俄罗斯方块的开发过程主要包含以下几个关键步骤。首先,需要设计游戏的界面。通过使用Python的图形库,如Pygame,可以创建一个游戏窗口,并在其绘制方块和游戏得分等元素。 接下来,需要编写游戏的逻辑部分。这包括方块的下落、旋转和移动等操作,以及判断并处理消除行的情况。这些逻辑可以通过使用Python的面向对象编程来组织和实现,可以将方块、游戏区域等各个元素分别定义为类,并在类实现相应的方法和属性。 此外,还需要实现一些游戏的功能和特性,如计时器、难度递增、游戏结束判断等。这些可以通过编写相应的函数来实现,并与界面和逻辑进行交互。 最后,完成游戏的测试和调试工作。在游戏开发的过程,需要对代码进行反复测试和调试,确保游戏的各个功能和特性能够正常运行,并排除可能的bug和问题。 总的来说,Python俄罗斯方块是一款具有挑战性和趣味性的游戏,通过使用Python语言,我们可以方便地开发出这样一款小游戏,提供给玩家进行娱乐和放松。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值