i have a given picture and have to create a picture that is twice as wide as the given picture. For each pixel (x, y) in the original picture, the pixels (2 * x, y) and (2 * x + 1, y) in the new picture should be set to the same color as it.
i am not allowed to use any other module( like PIL)
so i have to create a picture that is twice width
plz help me out here
here is what i have so far

import media

def widen(new_pic):

height = media.get_height(pic)
width = media.get_width(pic)
new_pic = media.create_picture( width * 2, height)
new_height = media.get_height(new_pic)
new_width = media.get_width(new_pic)
for y in range(0, new_height):

for x in range(0, new_width):

from_p = media.get_pixels(pic)

to_p = media.get_pixels(new_pic, 2 * x + 1, y)

media.set_color(to_p, media.get_color(from_p))

return new_pic

im not sure that i have used range function right but i dont really know how to use pixel pix to make same color

Dani AI

Generated

A few focused notes and a minimal, safe pattern to finish this task.

The goal is simple: loop the original image's width and height, read each original pixel color, and write that color into two adjacent pixels in the destination. Common mistakes in the code shown by are: iterating over the new image's width instead of the original width, calling a pixels-list helper incorrectly, and returning from inside the loop (which stops work after the first pixel). The example from is close, but watch out for x/2 — in Python 3 that yields a float; use integer division or iterate original coordinates instead.

A compact, readable pattern (different from the snippets already posted) is:

def widen_image(src):
    h = src.get_height()
    w = src.get_width()
    dst = media.create_picture(w * 2, h)

    for y in range(h):
        for x in range(w):
            color = src.get_pixel(x, y).get_color()
            dst.get_pixel(x * 2, y).set_color(color)
            dst.get_pixel(x * 2 + 1, y).set_color(color)

    return dst

Troubleshooting tips

  • Make sure return is after the loops. Returning early makes the function stop immediately.
  • Use integer indices only. In Python 3, prefer // or iterate the original x values (as above) so you never pass floats to get_pixel.
  • If get_pixel/get_color are slow for large images, reduce attribute lookups by binding methods to locals before the loops (e.g., g = src.get_pixel) or use a library like Pillow when allowed.
  • Test on a tiny image first so you can quickly spot off-by-one or coordinate-mixups.

This addresses the specific bugs in the thread and gives a clear, minimal implementation to copy and adapt.

I'm not a very experienced programmer like others around here but I took a look at this since there are no other answers. I was able to accomplish what you describe in the following manner, no guarantees that this is a recommended way to do this and obviously there is more than one way to skin the proverbial cat.

import picture
import media
import time

#open the image file
fd = open("path to file...", "rb")

#loaded picture from file--> returns image object
img = picture.load_image(fd)

#close file
fd.close()

#create picture object from image
Pic_obj = picture.Picture(image=img)

Pic_obj.show()
time.sleep(2)
Pic_obj.close_inspect()

#get width/height of original
h = Pic_obj.get_height()
w = Pic_obj.get_width()

#create new picture object for the resized image
new_pic = media.create_picture( width * 2, height)

#loop over the x,y in some way to fill in the new image

for y in range(0, height):
    for x in range(0, width*2, 2):        

        old_pixel = Pic_obj.get_pixel(x/2, y)

        old_pixel_color = old_pixel.get_color()

        new_pixel1 = new_pic.get_pixel(x,y)
        new_pixel2 = new_pic.get_pixel(x+1,y)

        new_pixel1.set_color(old_pixel_color)
        new_pixel2.set_color(old_pixel_color)

new_pic.show()

i have a given picture and have to create a picture that is twice as wide as the given picture. For each pixel (x, y) in the original picture, the pixels (2 * x, y) and (2 * x + 1, y) in the new picture should be set to the same color as it.
i am not allowed to use any other module( like PIL)
so i have to create a picture that is twice width
plz help me out here
here is what i have so far

import media

def widen(new_pic):
    
     height = media.get_height(pic)
     width = media.get_width(pic)
     new_pic = media.create_picture( width * 2, height)
     new_height = media.get_height(new_pic)
     new_width = media.get_width(new_pic)
     for y in range(0, new_height):
        
          for x in range(0, new_width):
            
               from_p = media.get_pixels(pic)

               to_p = media.get_pixels(new_pic, 2 * x + 1, y)
            
               media.set_color(to_p, media.get_color(from_p))
            
               return new_pic

im not sure that i have used range function right but i dont really know how to use pixel pix to make same color

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.