1
import sys
import pygame
import random

#command line
image = sys.argv[1]

#loading the image and importing
inputImage = pygame.image.load(image)

#getting the image's height and width
(width,height) = inputImage.get_size()

#creating the bigger windom of the image by multiplying original size by 6
window = pygame.display.set_mode((width*6,height*6))


for x in range(height):
    for y in range(width):

        #rgb coordinates in the x and y image
        (r,g,b,_) = inputImage.get_at((x,y))

        #Calculate required number of circles
        n1 = int(r/5)
        n2 = int(g/5)
        n3 = int(b/5)

        #drawing random red circles on the image
        while(n1 > 0):
            pygame.draw.circle(window,(255,0,0),(random.randint((x*5)-15,(x*5)),random.randint((y*5)-15,(y*5))),1)
            n1 = n1-1

        #drawing random green circles on the image
        while(n2 > 0):
            pygame.draw.circle(window,(0,255,0),(random.randint((x*5)-15,(x*5)),random.randint((y*5)-15,(y*5))),1)
            n2= n2-1

        #drawing random blue circles on the image
        while(n3 > 0):
            pygame.draw.circle(window,(0,0,255),(random.randint((x*5)-15,(x*5)),random.randint((y*5)-15,(y*5))),1)
            n3 = n3-1


#updating the image
pygame.display.update()

pygame.time.delay(5000)

My code sends me an error saying what the title is, so when I try to change the sys.argv line into image = sys.argv[0] It gives me a different error saying: pygame.error: Unsupported image format and I am not sure what that means? Am I missing something important? Also, if you guys can help me as to how to show the pygame window after fixing the code will much appreciated :)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
mahoodi
  • 11
  • 1
  • @Rabbid76, though if the OP is using `sys.argv[0]`, they're trying to read either their Python source code or the Python interpreter itself as an image, and obviously neither are. – Charles Duffy Nov 08 '22 at 21:43

1 Answers1

1

This method does not catch any user errors (which is what you're experiencing).

#command line
image = sys.argv[1]

You need to check a few things here:

  • Does the sys.argv list actually have a second element (at index [1]).
  • Test the filename specified actually exists.
  • Use a try, except block to catch any errors loading the image.
    import os.path

    # See if we can load the user's image
    inputImage = None
    if ( len( sys.argv ) >= 1 ):
        image_filename = sys.argv[1] 
        if ( not os.path.exists( image_filename ) ):
            sys.stderr.write( "Filename [" + image_filename + "] does not exist\n" )
        else:
            try:
                inputImage = pygame.image.load( image_filename )
            except:
                sys.stderr.write( "Failed to load [" + image_filename + "] is it an image?\n" )
                inputImage = None
    else:
        sys.stderr.write( "No Image Filename argument specified\n" )


    ### inputImage is either a valid image, or None
Kingsley
  • 14,398
  • 5
  • 31
  • 53