2 Replies - 11329 Views - Last Post: 12 January 2013 - 09:34 PM Rate Topic: -----

#1 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 208
  • Joined: 21-June 12

Pygame problem with blit and strange variable issues

Posted 11 January 2013 - 05:11 AM

I am having a really strange problem with Pygame, and it's got me stumped for the last few (OK, more like 5) hours. There are free programs out there for making photo mosaics, but ever since my early days tinkering with VB5, I've wanted to write my own version. You know how that is. I have all kinds of cool parts written for loading source images, finding color averages and everything. But here I'm stuck and confused. So very stuck and confused.

This part of the program converts a 'target image' (the one that will be made up of small source images) to smaller blocks of color that other source images will try to match and then replace eventually. But for some reason, the size of the blocks keeps increasing with every iteration. I've tried so many different things that I've had to go through the script and delete out a bunch of things and add a couple more comments before posting.

The target img is attached (a 1280x800 random google image), but any other picture should work just the same. Watch as the Y size of the blit increases with every block going down, and the X size increases as new rows are made. I hard coded in a set size for the solid color rectangle (2 pixels across, much smaller than I'll use), but for whatever reason this keeps increasing. The first row of blits is so small right now that it's hard to see. That quickly changes.
**OK, image is not attaching. Here's the link to what I am using (www.travelimg.org/wp-content/uploads/2012/01/iceland-golden-falls-druffix-europe-golden-falls-golden-falls-iceland-natur-waterfall-waterfalls-1024x768.jpg), but any other pic/size renamed to target.jpg should do the same.

If anyone can point me in the right direction it would be much appreciated. I want to cover this whole source pic in nice 12x12 blocks of solid color to start with. I can't figure out what is changing these block sizes as it goes.

import pygame
import os
from time import sleep

okformats = ['png','jpg','bmp','pcx','tif','lbm','pbm','pgm','ppm','xpm']

targetimg = 'C:\\Python27\\mosaic\\target.jpg'  #sorry linux users, I got lazy here

if targetimg[-3:] not in okformats:
    print 'That format is unsupported, get ready for some errors...'
    print 'Loading...'

screen = pygame.display.set_mode((100,100)) #picked a size just to start it out
clock = pygame.time.Clock() #possibly not needed in this script

targetpic = pygame.image.load(targetimg).convert()

targetrect = targetpic.get_rect()  #returns something like [0,0,128,128]
targetsize = targetrect[2:]
targetw = targetrect[2]
targeth = targetrect[3]

numpicsx = 100 #number of pictures that make up the width
sourceratio = 1  #testing with square pics for now
picxsize = targetw/numpicsx
numpicsy = targeth/(picxsize*sourceratio)
picysize = targeth/numpicsy

print 'Blitting target image'
screen = pygame.display.set_mode(targetsize)
screen.fill((255,255,255)) #set to white in case of transparency

#update screen

SLOWDOWN = .1  #temp slow down to watch it

print numpicsx #here are some print statements just to show all the starting values are correct
print numpicsy
print '---'
print picxsize
print picysize


for x in xrange(numpicsx):

    for y in xrange(numpicsy):
        currentrect = [x*picxsize,y*picysize,x*picxsize+picxsize,y*picysize+picysize]

        avgc = pygame.transform.average_color((targetpic), currentrect) #average color
        avgc = avgc[:3]  #drops out the alpha if there is one

        #pygame.draw.rect(screen, avgc, currentrect)
        pygame.draw.rect(screen, avgc, (currentrect[0],currentrect[1],currentrect[0]+2,currentrect[1]+2))  #hard coded 2s (rather than 12s in this case) to help pin point the problem

        clock.tick(30) #probably not needed


print 'Done./nSleeping then quitting...'


Is This A Good Question/Topic? 1
  • +

Replies To: Pygame problem with blit and strange variable issues

#2 alexr1090   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 126
  • Joined: 08-May 11

Re: Pygame problem with blit and strange variable issues

Posted 12 January 2013 - 02:23 AM

#pygame.draw.rect(screen, avgc, currentrect)

uncomment this line. Change currentrect's third and fourth variables to the width and height at which you want to print the lines (12*12 in your case).

I'll explain a bit for anyone who has problem in future. Basically draw.rect draws a rectangle. The width and height at which it draws the rectangle are based on the rectangle argument's width and height that was sent to it. So if the rectangle sent to it has a width and height that's always increasing the rectangles drawn will always increase.
Was This Post Helpful? 2
  • +
  • -

#3 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 208
  • Joined: 21-June 12

Re: Pygame problem with blit and strange variable issues

Posted 12 January 2013 - 09:34 PM

Awesome! Thanks a lot man. I was thinking that the correct format for rect was (x1,y1,x2,y2) rather than (x,y,width,height). This fixed my problem. Below is what I was going off of when writing. A section from pygame.org that is worded in a way that confused me.

"draw a rectangle shape
pygame.draw.rect(Surface, color, Rect, width=0): return Rect

Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled."
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1