I'm working on some python programming and I need some help as to how to double the size of a picture using the image module.... any ideas?!

Recommended Answers

All 14 Replies

Is the picture you are loading smaller than what you want to display? If yes, the quality will suffer quite a bit! The program has to make up missing information.

Are you using the Python Image Library PIL for this?

I know the quality will be degraded, but I'm going for making it larger either way.... and yes I'm using the PIL.... and yes I am going from smaller to larger.....

Here is what I have done... some steps i did may not have been necessary, for example the extra color variables.... though now i need help with my zoom factor.... because this works for zooming in two times... but when higher numbers come about it skips pixels... i messed around with an extra for loop but its still the same... any other ideals?

def enlarge(oImage,w,h,x,y,zoom):
    nImage= EmptyImage(w*zoom,h*zoom)
    for q in range(w):
        x = x +zoom
        y = 0
        for z in range(h):
            color = oImage.getPixel(q,z)
            red= color[0]
            green= color[1]
            blue= color[2]
            nImage.setPixel(x ,y ,(red,green,blue))
            nImage.setPixel(x+1,y +1,(red,green,blue))
            nImage.setPixel(x+1, y,(red,green,blue))
            nImage.setPixel(x, y+1,(red,green,blue))
            for lea in range(1,zoom):
                nImage.setPixel(x+lea,y+lea,(red,green,blue))
                nImage.setPixel(x, y+lea,(red,green,blue))
                nImage.setPixel(x+lea,y, (red, green,blue))
            y = y +zoom
    r = nImage
    winner = ImageWin("Big Picture", w*zoom, h*zoom)
    r.draw(winner)
    winner.getMouse()
    winner.close()
            

def main():
    picname= raw_input("Enter Picture name with .jpg, .bmp, or ending: ")
    oImage= FileImage(picname)
    h= oImage.getHeight()
    w= oImage.getWidth()
    win = ImageWin("Original", w,h)
    oImage.draw(win)
    y = 0
    zoom = input("Number of times to zoom: ")
    x = -(zoom)

    enlarge(oImage,w,h,x,y,zoom)

    win.getMouse()
    win.close()


main()

Nice project c_shaft05 you do much thinking here!

I need to refraise what i said... I need help being able to take a user input for the number of times to zoom in and put it into my program but I need help with the for loop in how to do it.... pixels are being skipped when i go in to higher numbers!

I remember in school they talked about DIB (device indepentant bitmaps) to be best for stretching/enlarging. Was C class for graph artists.

Meh.... yea... I don't know what you are talking about ... but yea... I just need help looping it to the point where its not skipping Pixels......

There is something missing in your code. What do you import?

Need to import image(not Image).... so on the top needs to be "from image import * "

If I use

from image import *

all I get is: ImportError: No module named image

Is this a custom module?

Yes it is........... it is attached... just resave it as a .py file (which you probably already knew to do)...... and you'll be set! I forgot it was a library we used for my comp sci class (this project was due 2 weeks ago.. but I'm just expanding on it is all!!!)

is there anyone else who could take a look at this module and mess around with what i have? thanks

I did manage to test your code and found the problem. With zoom > 3 you will have to expand the code accordingly ...

# double the size of an image attempt

from image import *

def enlarge(oImage, w, h, x, y, zoom):
    nImage = EmptyImage(w*zoom, h*zoom)
    
    for q in range(w):
        x = x + zoom
        y = 0

        for z in range(h):
            color = oImage.getPixel(q, z)
            #print x, y, q, z, color  # test
            red = color[0]
            green = color[1]
            blue = color[2]
            # this takes care of zoom=2
            nImage.setPixel(x , y, (red, green, blue))
            nImage.setPixel(x+1, y+1, (red, green, blue))
            nImage.setPixel(x+1, y, (red, green, blue))
            nImage.setPixel(x, y+1, (red, green, blue))
            
            if zoom > 2:
                # problem here, skips pixel loacations and defaults to black (0,0,0)
                # (x+2,y+1) and (x+1,y+2) are skipped on zoom = 3
                # even more pixels are skipped on zoom = 4
                for lea in range(2, zoom):
                    #print lea,  # test
                    nImage.setPixel(x+lea, y+lea, (red,green,blue))
                    nImage.setPixel(x, y+lea, (red, green, blue))
                    nImage.setPixel(x+1, y+lea, (red, green, blue))  # added for zoom=3
                    nImage.setPixel(x+lea, y, (red, green, blue))
                    nImage.setPixel(x+lea, y+1, (red, green, blue))  # added for zoom=3
            
            y = y + zoom

    r = nImage
    winner = ImageWin("Big Picture", w*zoom, h*zoom)
    r.draw(winner)
    winner.getMouse()
    winner.close()
            

def main():
    #picname= raw_input("Enter Picture name with .jpg, .bmp, or ending: ")
    
    picname = "white80x80.jpg"  # uses a totally white 80x80 test jpeg
    
    oImage = FileImage(picname)
    h = oImage.getHeight()
    w = oImage.getWidth()
    win = ImageWin("Original", w, h)
    oImage.draw(win)
    """
    zoom = input("Number of times to zoom: ")
    """
    
    zoom = 3  # added for testing
    
    x = -zoom
    y = 0

    enlarge(oImage, w, h, x, y, zoom)

    win.getMouse()
    win.close()


main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.