复制一个像素区域 [英] Copy an area of pixels

查看:44
本文介绍了复制一个像素区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 和 pygame 来制作一个简单的绘图应用程序.

我需要一种方法来从一个区域复制像素(例如,从 X 100、Y 100 到 X 200、Y 200),保存它,然后再次将其放下.很像截图.

也许是表面……?

我不知道表面是如何工作的,甚至不清楚它们的作用.

解决方案

您可以使用 ,但这相当麻烦.

I am using Python with pygame to make a simple drawing application.

I need a way to copy pixels from an area (for example, from X 100, Y 100 to X 200, Y 200), save that, and then place it down again. Much like a screenshot.

Maybe a surface...?

I have no clue how surfaces work or even a clear idea of what they do.

解决方案

You could use Surface.subsurface to create, well, a subsurface.

Here's a simple example. Select an area with the mouse, then blit the selected subsurface by pressing a mouse button again:

import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))

clock = pygame.time.Clock()
for x in xrange(0, 800):
    pygame.draw.rect(screen, ((x+100)%255, (x*2)%255, x%255), pygame.Rect(x, 0, 1, 800))

org_screen = screen.copy()

class Cursor(object):
    def __init__(self):
        self._inner = CopyCursor()

    def mousedown(self, mpos):
        self._inner = self._inner.mousedown(mpos)

    def mouseup(self, mpos):
        self._inner = self._inner.mouseup(mpos)

    def draw(self, screen, mpos):
        self._inner.draw(screen, mpos)

class CopyCursor(object):
    def __init__(self):
        self.start, self.end = None, None

    def mousedown(self, mpos):
        self.start = mpos
        return self

    def mouseup(self, mpos):
        if self.start and self.end:
            r = pygame.Rect(self.start, self.end)
            r.normalize()
            # important to call .copy() so 'sub_surf' 
            # loses its relation to 'screen'
            return BlitCursor(screen.subsurface(r).copy())
        return self

    def draw(self, screen, mpos):
        if self.start:
            self.end = mpos[0]-self.start[0], mpos[1]-self.start[1]
            rect = pygame.Rect(self.start, self.end)
            pygame.draw.rect(screen, pygame.Color('grey'), rect, 3)

class BlitCursor(object):
    def __init__(self, sub_surf):
        self.sub_surf = sub_surf

    def mousedown(self, mpos):
        pygame.event.post(pygame.event.Event(pygame.USEREVENT, surface=self.sub_surf, pos=mpos))
        return CopyCursor()

    def mouseup(self, mpos):
        pass

    def draw(self, screen, mpos):
        screen.blit(self.sub_surf, mpos)

cursor = Cursor()
while True:
    mpos = pygame.mouse.get_pos()
    for e in pygame.event.get():
        if e.type == pygame.QUIT: break
        if e.type == pygame.MOUSEBUTTONUP: cursor.mouseup(mpos)
        if e.type == pygame.MOUSEBUTTONDOWN: cursor.mousedown(mpos)
        if e.type == pygame.USEREVENT:
            org_screen.blit(e.surface, e.pos)
            screen.blit(org_screen, (0, 0))
    else:
        if not pygame.event.peek(pygame.USEREVENT):
            cursor.draw(screen, mpos)            
            pygame.display.flip()
            screen.blit(org_screen, (0, 0))
        clock.tick(50)
        continue
    break


Another way to do this would be to use the area keyword while blitting to only blit a specific area of the source Surface.

A third way is to use pygame.transform.chop, but that's rather cumbersome.

这篇关于复制一个像素区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆