0

What i'm trying to do is to create a png image using specified parameters, like:

img = Image(200, 100, Color(0, 0, 255))
h_line(img, 20, Color(255, 0, 0))
c = img.get_pixel(20, 20)
c.b = 200
img2 = img.copy()
h_line(img, 40, Color(255, 0, 0))
save('file01_01_out.png', img)

This is what I wrote so far:

    import png

class Color(object):
    def __init__(self, r, g, b):
        self.r = r
        self.g = g
        self.b = b

    def copy(self):
        return Color(self.r, self.g, self.b)

class Image(object):
    #'''Class must have height and width attributes'''
    def __init__(self, width, height, c):
        self.width = width
        self.height = height
        self.c = Color
        #'''Initializes the image with width, height and every pixel with copies of c
       # c being an object of Color type'''
 #    

    def set_pixel(self, x, y, c):
        self.img.put("{" + c +"}", (x, y))
       # '''Sets the color in position (x, y) with a copy of object
        #    c of type Color. If the position (x, y) is beyond the
        #    object, then it won't do anything'''

    def get_pixel(self, x, y):
        value = self.img.get(x,y) 
        if type(value) ==  type(0):
            return [value, value, value]
        else:
            return None 
#        '''Returns a copy of the color (Color type) in position
#           (x, y). If (x, y) is beyond the picture, it will return
#           None'''

    def copy(self):
        return Image(self.width, self.height, self.c)


def h_line(img, y, c):
    for x in range(img.width):
        img.set_pixel(x, y, c)

def save(filename, img):
    png_img = []
    for i in range(img.height):
        png_row = []
        for j in range(img.width):
            c = img.get_pixel(j, i)
            png_row.extend([c.r, c.g, c.b])
        png_img.append(png_row)
    with open(filename, 'wb') as f:
        png.Writer(img.width, img.height).write(f, png_img)

The problem is that the program won't crash but it won't save anything! I tried with different examples, yet the result is always the same. What am I doing wrong?

1
  • I'll be surprised if anyone can work with this (it's not self-contained or runnable for anyone else) - a lot of code appears to be missing or just not properly indented - for instance: self.img is what exactly? (And if I understand what you're doing correctly, it's just a couple of lines using PIL anyway...) Commented Dec 23, 2012 at 19:36

1 Answer 1

1

Update - just noticed the Py3 tag - so answer may not be valid - but potentially of some use

As an example - using PIL - you can create a 200x200 image with a certain RGB colour, and then just save it with a .png extension...

>>> from PIL import Image
>>> img = Image.new('RGB', (200, 200), (127, 127, 127))
>>> img.save('/path/to/some/file.png')

Gives you:

Example image

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but I really need to use the PNG library unfortunately :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.