Изрязване на изображения с помощта на Python

Аз съм нов в кодирането на Python и пиша програма, в която ще изрязвам въведено изображение и след това ще го запазвам на място. Сега мога да направя това с помощта на комбинация от PIL и pygame. Но проблемът е, че когато избирам изображението от отворения прозорец на pygame, областта за избор е напълно непрозрачна и не мога да видя през региона, който избирам. Това създава проблеми на шефа ми, който иска да може да вижда през него, докато избира. За да разберете по-добре проблема, пиша моя код тук:

import pygame, sys 
from PIL import Image 

pygame.init()

def displayImage( screen, px, topleft):
     screen.blit(px, px.get_rect())
     if topleft:
         pygame.draw.rect( screen, (128,128,128), pygame.Rect(topleft[0], topleft[1], pygame.mouse.get_pos()[0] - topleft[0], pygame.mouse.get_pos()[1] - topleft[1]))
     pygame.display.flip()  

def setup(path):
     px = pygame.image.load(path)
     screen = pygame.display.set_mode( px.get_rect()[2:] )
     screen.blit(px, px.get_rect())
     pygame.display.flip()
     return screen, px

def mainLoop(screen, px):
     topleft = None
     bottomright = None
     n=0
     while n!=1:
         for event in pygame.event.get():
             if event.type == pygame.MOUSEBUTTONUP:
                 if not topleft:
                     topleft = event.pos
                 else:
                     bottomright = event.pos
                     n=1
         displayImage(screen, px, topleft)
     return ( topleft + bottomright )

if __name__ == "__main__":
     input_loc="C:\pic1.PNG"
     output_loc="C:\pic2.PNG"
     screen, px = setup(input_loc)
     left, upper, right, lower = mainLoop(screen, px)
     im = Image.open(input_loc)
     im = im.crop(( left, upper, right, lower))
     pygame.display.quit()
     im.save(output_loc)

Всяка помощ се оценява. За разбирането.


person Arnab Ghosal    schedule 26.05.2011    source източник


Отговори (2)


Хвърлих бърз поглед и поправих няколко други проблема по пътя. По същество моите промени правят това:

  • Начертайте ограничителната кутия върху временно изображение, задайте неговата алфа прозрачност и след това го замажете върху основното изображение.
  • Избягвайте странични цикли на рисуване (когато мишката не се движи, няма смисъл да рисувате същото изображение отново).
  • Уверете се, че ширината и височината винаги са положителни. Ако правоъгълникът е начертан чрез плъзгане на мишката наляво или нагоре, вашият код ще завърши с отрицателна ширина и/или височина, предизвиквайки изключение, когато се опитвате да напишете крайното изображение.

Ето екранна снимка на изпълнение на фиксирания код:

въведете описание на изображението тук

Разделих кода на две части, за да избегна лентите за превъртане:

import pygame, sys
from PIL import Image
pygame.init()

def displayImage(screen, px, topleft, prior):
    # ensure that the rect always has positive width, height
    x, y = topleft
    width =  pygame.mouse.get_pos()[0] - topleft[0]
    height = pygame.mouse.get_pos()[1] - topleft[1]
    if width < 0:
        x += width
        width = abs(width)
    if height < 0:
        y += height
        height = abs(height)

    # eliminate redundant drawing cycles (when mouse isn't moving)
    current = x, y, width, height
    if not (width and height):
        return current
    if current == prior:
        return current

    # draw transparent box and blit it onto canvas
    screen.blit(px, px.get_rect())
    im = pygame.Surface((width, height))
    im.fill((128, 128, 128))
    pygame.draw.rect(im, (32, 32, 32), im.get_rect(), 1)
    im.set_alpha(128)
    screen.blit(im, (x, y))
    pygame.display.flip()

    # return current box extents
    return (x, y, width, height)

И част 2 (свързване към горното):

def setup(path):
    px = pygame.image.load(path)
    screen = pygame.display.set_mode( px.get_rect()[2:] )
    screen.blit(px, px.get_rect())
    pygame.display.flip()
    return screen, px

def mainLoop(screen, px):
    topleft = bottomright = prior = None
    n=0
    while n!=1:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONUP:
                if not topleft:
                    topleft = event.pos
                else:
                    bottomright = event.pos
                    n=1
        if topleft:
            prior = displayImage(screen, px, topleft, prior)
    return ( topleft + bottomright )

if __name__ == "__main__":
    input_loc = 'stack.png'
    output_loc = 'out.png'
    screen, px = setup(input_loc)
    left, upper, right, lower = mainLoop(screen, px)

    # ensure output rect always has positive width, height
    if right < left:
        left, right = right, left
    if lower < upper:
        lower, upper = upper, lower
    im = Image.open(input_loc)
    im = im.crop(( left, upper, right, lower))
    pygame.display.quit()
    im.save(output_loc)
person samplebias    schedule 26.05.2011
comment
Благодаря брато за навременната помощ. За разбирането. - person Arnab Ghosal; 26.05.2011
comment
Имам нужда и от тънка рамка около правоъгълната селекция. Можете ли да ми помогнете с това? - person Arnab Ghosal; 27.05.2011
comment
Да, бихте използвали pygame.draw.rect() след fill() - актуализирах отговора съответно. - person samplebias; 27.05.2011
comment
Сега се опитвам да увелича размера на дисплея на изображението. В момента дисплеят на pygame е със същия размер като картината, която използвам. Това, което се опитвам да направя, е да се намеся в px.get_rect(), за да мога да покажа изображението в увеличено състояние, така че когато потребителят избере интересния регион, да може да го направи по-точно. Правилен ли е подходът? - person Arnab Ghosal; 28.05.2011
comment
за ефекта на мащабиране промених тези два реда: screen = pygame.display.set_mode((int(px.get_rect()[2]*1.2),int(px.get_rect()[3]*1.2))) screen.blit(px,(px.get_rect()[0],px.get_rect()[1],int(px.get_rect()[2]*1.2),int(px.get_rect()[3]*1.2))), но все още не ми дава увеличено изображение. - person Arnab Ghosal; 28.05.2011

Отговорът от samplebias беше страхотен за мен. Разширих го за големите изображения, с които работя, като добавих възможности за мащабиране и панорамиране.

import pygame, sys
from pygame.locals import K_a, K_s,K_w,K_d,K_LEFTBRACKET,K_RIGHTBRACKET

from PIL import Image
pygame.init()

BG_COLOR = (0,0,0)

def displayRect(screen, px, topleft, prior,pos,scale):
    # ensure that the rect always has positive width, height
    #topleft = [(val-pos[i])/scale for i,val in enumerate(topleft)]
    topleft = [(val/scale-pos[i]) for i,val in enumerate(topleft)]
    x, y = topleft
    bottomright = pygame.mouse.get_pos()
    width =  bottomright[0] - topleft[0]
    height = bottomright[1] - topleft[1]
    if width < 0:
        x += width
        width = abs(width)
    if height < 0:
        y += height
        height = abs(height)

    # eliminate redundant drawing cycles (when mouse isn't moving)

    current = x, y, width, height
    if not (width and height):
        return current
    if current == prior:
        return current

    # draw transparent box and blit it onto canvas
    rect = px.get_rect()
    px = pygame.transform.scale(px,[rect.width/scale, rect.height/scale])
    screen.blit(px, (rect[0]-pos[0],rect[1]-pos[1]))
    im = pygame.Surface((width, height))
    im.fill((128, 128, 128))
    pygame.draw.rect(im, (32, 32, 32), im.get_rect(), 1)
    im.set_alpha(128)
    screen.blit(im, (x, y))
    pygame.display.flip()

    # return current box extents
    return (x, y, width, height)

def setup(px):
    screen = pygame.display.set_mode( px.get_rect()[2:] )
    screen.blit(px, px.get_rect())
    pygame.display.flip()
    return screen, px

def move(pos,scale,px,screen):
    x,y = pos
    #print pos,x
    rect = px.get_rect()
    screen.fill(BG_COLOR)
    px = pygame.transform.scale(px,[rect.width/scale, rect.height/scale])
    screen.blit(px, (rect[0]-x,rect[1]-y))
    pygame.display.flip()
    #px.rect.topleft = pr.rect.topleft[0] - x, 

def mainLoop(screen, px, filelist):
    topleft = bottomright = prior = None
    n=0
    scale = 1
    pos = [0,0]
    while n!=1:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONUP:
                if not topleft:
                    topleft = [(val+pos[i])*scale for i,val in enumerate(event.pos)]
                    print "tr: ",topleft
                else:
                    bottomright = [(val+pos[i])*scale for i,val in enumerate(event.pos)]
                    print "br: ",bottomright
                    n=1

            if event.type == pygame.KEYDOWN and event.key == K_a:
                pos = [pos[0]-200,pos[1]]
                move(pos,scale,px,screen)
            if event.type == pygame.KEYDOWN and event.key == K_d:
                pos = [pos[0]+200,pos[1]]
                move(pos,scale,px,screen)
            if event.type == pygame.KEYDOWN and event.key == K_w:
                pos = [pos[0],pos[1]-200]
                move(pos,scale,px,screen)
            if event.type == pygame.KEYDOWN and event.key == K_s:
                pos = [pos[0],pos[1]+200]
                move(pos,scale,px,screen)


            if event.type == pygame.KEYDOWN and event.key == K_RIGHTBRACKET:
                scale = scale/1.25
                move(pos,scale,px,screen)
            if event.type == pygame.KEYDOWN and event.key == K_LEFTBRACKET:
                scale = scale*1.25
                move(pos,scale,px,screen)


        if topleft:
            prior = displayRect(screen, px, topleft, prior,pos,scale)

    return ( topleft + bottomright )

Използвайте предоставената главна функция samplebias.

Благодаря, Stack Overflow!

person Permafacture    schedule 02.01.2012