How to write and save image from data?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbtunx
    New Member
    • Feb 2011
    • 8

    How to write and save image from data?

    in simple word i want to have an image saved by the program to hard disc that the size is 100x100 pixels the left upper pixel is black and the right lower pixel is white. this is not my problem but as an example will help me to know how use commands to wright an image from data.
    thank you for your help.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    This is not exactly what you want, but writes an image to disc that varies from black to white, left to right.
    Code:
    from PIL import Image
    
    im = Image.new("RGB", (255,100))
    for x in range(im.size[0]):
        for y in range(im.size[1]):
            im.putpixel((x,y), (x,x,x))
            
    im.save("pix.png")

    Comment

    • sbtunx
      New Member
      • Feb 2011
      • 8

      #3
      thank you bvdet that is helpful for me. also i have question about putpixel command it seams to be slow . how can we use other commands for better response? completing the above code.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Would im.putdata() work better for you?
        Code:
        from PIL import Image
        
        im = Image.new("RGB", (255,100))
        data = [(x,x,x) for y in range(im.size[1]) for x in range(im.size[0])]
        im.putdata(data)
        im.save("pix.png")

        Comment

        Working...