鱼C论坛

 找回密码
 立即注册
查看: 2115|回复: 2

[已解决]pygame裁剪图像的问题

[复制链接]
发表于 2023-4-24 21:35:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

  5. size=width,height=600,300
  6. bg=(255,255,255)

  7. clock=pygame.time.Clock()


  8. screen=pygame.display.set_mode(size)
  9. pygame.display.set_caption("初次见面,请大家多多关照!")
  10. turtle=pygame.image.load('悟空.jpg')
  11. position=turtle.get_rect()
  12. position.center = width//2,height//2


  13. # 0-->未选择,1-->选择中,2-->选择完成
  14. select = 0
  15. select_rect = pygame.Rect(0,0,0,0)
  16. # 0-->未拖拽,1-->拖拽中,2-->拖拽完成
  17. drag = 0

  18. while True:
  19.     for event in pygame.event.get():
  20.         if event.type == pygame.QUIT:
  21.             sys.exit()
  22.             

  23.         elif event.type == MOUSEBUTTONDOWN:
  24.             if event.button == 1:
  25.                 #第一次点击,选择范围
  26.                 if select ==0 and drag ==0:
  27.                     pos_start = event.pos
  28.                     select = 1
  29.                 #第二次点击,拖拽图像
  30.                 elif select ==2 and drag ==0:
  31.                     capture = screen.subsurface(select_rect).copy()
  32.                     cap_rect = capture.get_rect()
  33.                     drag = 1
  34.                 #第三次点击,初始化
  35.                 elif select ==2 and drag ==2:
  36.                     select = 0
  37.                     drag = 0
  38.                     
  39.         elif event.type == MOUSEBUTTONUP:
  40.             if event.button == 1:
  41.                 #第一次释放,结束选择
  42.                 if select ==1 and drag ==0:
  43.                     pos_stop = event.pos
  44.                     select = 2
  45.                 #第二次释放,结束拖拽
  46.                 elif select ==2 and drag == 1:
  47.                     drag = 2

  48.             
  49.    

  50.     screen.fill(bg)
  51.     screen.blit(turtle,position)
  52.    
  53.     #实时绘制选择框
  54.     if select:
  55.         mouse_pos = pygame.mouse.get_pos()
  56.         if select == 1:
  57.             pos_stop = mouse_pos
  58.         select_rect.left,select_rect.top = pos_start
  59.         select_rect.width,select_rect_height = pos_stop[0]-pos_start[0],pos_stop[1]-pos_start[1]
  60.         pygame.draw.rect(screen,(0,0,0),select_rect,1)

  61.     #拖拽裁剪的图像
  62.     if drag:
  63.         if drag ==1:
  64.             cap_rect.center = mouse_pos
  65.         screen.blit(capture,cap_rect)
  66.    
  67.     pygame.display.flip()
  68.     clock.tick(30)
复制代码


实时绘制选择框这里为啥框不出选择框咨询一下
  1. pygame.draw.rect(screen,(0,0,0),select_rect,1)
复制代码
最佳答案
2023-4-24 21:47:31
本帖最后由 isdkz 于 2023-4-24 21:49 编辑

您好!我注意到您的代码中有一个小错误,导致选择框不能正确显示。在定义select_rect的长和宽时(第70行),您应该使用select_rect.width和select_rect.height,而不是select_rect_height。请参阅以下修复后的代码段:

  1. if select:
  2.     mouse_pos = pygame.mouse.get_pos()
  3.     if select == 1:
  4.         pos_stop = mouse_pos
  5.     select_rect.left, select_rect.top = pos_start
  6.     select_rect.width, select_rect.height = pos_stop[0] - pos_start[0], pos_stop[1] - pos_start[1]
  7.     pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)
复制代码


将此修复后的代码段替换到您的代码中,选择框应该能正确显示了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-4-24 21:47:31 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-4-24 21:49 编辑

您好!我注意到您的代码中有一个小错误,导致选择框不能正确显示。在定义select_rect的长和宽时(第70行),您应该使用select_rect.width和select_rect.height,而不是select_rect_height。请参阅以下修复后的代码段:

  1. if select:
  2.     mouse_pos = pygame.mouse.get_pos()
  3.     if select == 1:
  4.         pos_stop = mouse_pos
  5.     select_rect.left, select_rect.top = pos_start
  6.     select_rect.width, select_rect.height = pos_stop[0] - pos_start[0], pos_stop[1] - pos_start[1]
  7.     pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)
复制代码


将此修复后的代码段替换到您的代码中,选择框应该能正确显示了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-24 21:50:29 | 显示全部楼层
isdkz 发表于 2023-4-24 21:47
您好!我注意到您的代码中有一个小错误,导致选择框不能正确显示。在定义select_rect的长和宽时(第70行), ...

感谢感谢,找了半天没看到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 01:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表