merge two png pic

Discussion in 'Python' started by cocobear, Jul 29, 2009.

  1. cocobear

    cocobear Guest

    This two png file has their own palette

    >>> im1.mode

    'P'
    >>> im.mode

    'P'
    >>> im.getpalette == im1.getpalette

    False

    I can use this code to merge two png pic together:

    Map = Image.new("RGB", (x,y))
    Map.paste(im, box)
    Map.paste(im1,box)

    Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)

    But if the two png pic is too big , or if I have to merge more pic
    together, I will get MemoryError:

    >>> Image.new("RGB",(44544,38656))

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in
    new
    return Image()._new(core.fill(mode, size, color))
    MemoryError

    How can I directly merge two pic to a ‘P' mode png with palette.
    cocobear, Jul 29, 2009
    #1
    1. Advertising

  2. cocobear

    cocobear Guest

    On Jul 29, 9:20 am, cocobear <> wrote:
    > Thistwopngfile has their own palette
    >
    > >>> im1.mode

    > 'P'
    > >>> im.mode

    > 'P'
    > >>> im.getpalette == im1.getpalette

    >
    > False
    >
    > I can use this code tomergetwopngpictogether:
    >
    > Map = Image.new("RGB", (x,y))
    > Map.paste(im, box)
    > Map.paste(im1,box)
    >
    > Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)
    >
    > But if thetwopngpicis too big , or if I have tomergemorepic
    > together, I will get MemoryError:
    >
    > >>> Image.new("RGB",(44544,38656))

    >
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    >   File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in
    > new
    >     return Image()._new(core.fill(mode, size, color))
    > MemoryError
    >
    > How can I directlymergetwopicto a ‘P' modepngwith palette.



    Nobody replied.
    cocobear, Jul 31, 2009
    #2
    1. Advertising

  3. cocobear

    Peter Otten Guest

    cocobear wrote:

    > On Jul 29, 9:20 am, cocobear <> wrote:
    >> Thistwopngfile has their own palette
    >>
    >> >>> im1.mode

    >> 'P'
    >> >>> im.mode

    >> 'P'
    >> >>> im.getpalette == im1.getpalette

    >>
    >> False
    >>
    >> I can use this code tomergetwopngpictogether:
    >>
    >> Map = Image.new("RGB", (x,y))
    >> Map.paste(im, box)
    >> Map.paste(im1,box)
    >>
    >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)
    >>
    >> But if thetwopngpicis too big , or if I have tomergemorepic
    >> together, I will get MemoryError:
    >>
    >> >>> Image.new("RGB",(44544,38656))


    As a workaround you could split the image into tiles that fit into your
    machine's RAM. Or you could try to do the rendering on a 64-bit system.
    You'll need at least

    >>> 3*44544*38656./(2**30)

    4.8109130859375

    5 gigabytes for the target image plus memory for the biggest source image.
    Alternatively there are probably tools that can keep parts of the image on
    disk (imagemagick, maybe? you'll have to check yourself or ask in a
    specialized forum). As a last resort you should be able to write such a tool
    yourself. I don't know how hard it would be to generate the PNG, but the RGB
    pasting should be trivial.

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in
    >> new
    >> return Image()._new(core.fill(mode, size, color))
    >> MemoryError
    >>
    >> How can I directlymergetwopicto a ‘P' modepngwith palette.

    >
    >
    > Nobody replied.


    What do you want to do with such a big image? You will run into the same
    limitation when you are trying to display it.

    Peter
    Peter Otten, Jul 31, 2009
    #3
  4. cocobear

    Terry Reedy Guest

    cocobear wrote:
    > On Jul 29, 9:20 am, cocobear <> wrote:
    >> Thistwopngfile has their own palette
    >>
    >>>>> im1.mode

    >> 'P'
    >>>>> im.mode

    >> 'P'
    >>>>> im.getpalette == im1.getpalette

    >> False
    >>
    >> I can use this code tomergetwopngpictogether:
    >>
    >> Map = Image.new("RGB", (x,y))
    >> Map.paste(im, box)
    >> Map.paste(im1,box)
    >>
    >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)
    >>
    >> But if thetwopngpicis too big , or if I have tomergemorepic
    >> together, I will get MemoryError:
    >>
    >>>>> Image.new("RGB",(44544,38656))

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in
    >> new
    >> return Image()._new(core.fill(mode, size, color))
    >> MemoryError
    >>
    >> How can I directlymergetwopicto a ‘P' modepngwith palette.

    >
    >
    > Nobody replied.


    A. Leavingoutspacesmakesyourposthardtoread.

    B. Nobody knows how to avoid mem error.

    tjr
    Terry Reedy, Jul 31, 2009
    #4
  5. cocobear

    cocobear Guest

    On Jul 31, 2:52 pm, Peter Otten <> wrote:
    > cocobear wrote:
    > > On Jul 29, 9:20 am, cocobear <> wrote:
    > >> Thistwopngfile has their own palette

    >
    > >> >>> im1.mode
    > >> 'P'
    > >> >>> im.mode
    > >> 'P'
    > >> >>> im.getpalette == im1.getpalette

    >
    > >> False

    >
    > >> I can use this code tomergetwopngpictogether:

    >
    > >> Map = Image.new("RGB", (x,y))
    > >> Map.paste(im, box)
    > >> Map.paste(im1,box)

    >
    > >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)

    >
    > >> But if thetwopngpicis too big , or if I have tomergemorepic
    > >> together, I will get MemoryError:

    >
    > >> >>> Image.new("RGB",(44544,38656))

    >
    > As a workaround you could split the image into tiles that fit into your
    > machine's RAM. Or you could try to do the rendering on a 64-bit system.
    > You'll need at least
    >
    > >>> 3*44544*38656./(2**30)

    >
    > 4.8109130859375
    >
    > 5 gigabytes for the target image plus memory for the biggest source image..
    > Alternatively there are probably tools that can keep parts of the image on
    > disk (imagemagick, maybe? you'll have to check yourself or ask in a
    > specialized forum). As a last resort you should be able to write such a tool
    > yourself. I don't know how hard it would be to generate the PNG, but the RGB
    > pasting should be trivial.
    >


    Thanks for you reply.

    Map = Image.new("RGB", ((x2-x1+1)*256, (y2-y1+1)*256))
    for x in range(x1,x2+1):
    for y in range(y1,y2+1):
    #print x,y
    filename = "v=cn1.11&hl=zh-CN&x=%d&y=%d&z=%d&s=Galile" %
    (x,y,z)
    #
    box = ((x-x1)*256, (y-y1)*256, (x-x1)*256+256, (y-y1)
    *256+256)
    #print box
    im = Image.open(filename+".png")
    Map.paste(im, box)

    temp = "temp_map.png"
    Map.save(temp)
    del Map
    print "converting"
    Map = Image.open(temp)
    Map = Map.convert("P", palette=Image.ADAPTIVE)

    Now I use code above, the problem is when I create a RGB image, I need
    four times spaces then a 'P' mode image.

    Can I directly create a 'P' mode image, and paste those images on?
    (NOTICE: this small image has different palette)


    > >> Traceback (most recent call last):
    > >> File "<stdin>", line 1, in <module>
    > >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in
    > >> new
    > >> return Image()._new(core.fill(mode, size, color))
    > >> MemoryError

    >
    > >> How can I directlymergetwopicto a ‘P' modepngwith palette.

    >
    > > Nobody replied.

    >
    > What do you want to do with such a big image? You will run into the same
    > limitation when you are trying to display it.
    >


    I want to download a map from google map with high precision


    > Peter
    cocobear, Aug 4, 2009
    #5
  6. cocobear

    News123 Guest

    Hi,

    cocobear wrote:

    >> > >> Map = Image.new("RGB", (x,y))
    >> > >> Map.paste(im, box)
    >> > >> Map.paste(im1,box)

    >
    >> > >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)

    >
    >> > >> But if thetwopngpicis too big , or if I have tomergemorepic
    >> > >> together, I will get MemoryError:

    >
    >>>>> > >> >>> Image.new("RGB",(44544,38656))

    >>
    >> What do you want to do with such a big image? You will run into the same
    >> limitation when you are trying to display it.
    >>

    >
    > I want to download a map from google map with high precision
    >



    For me it sounds strange to handle such big files.

    Is the map you download from Google really that big?
    Mostly Google maps uses normally tiles of sizes 256x256 pixel, wich you
    have to compose to get the big image.

    If your problem is to combine tiles with different palettes, then you
    should pobably look at a way to 'unify' the palette of all tiles befor
    combining them.

    Being no expert of PIL I don't know whether this is easily possible though?


    bye


    N
    News123, Aug 4, 2009
    #6
  7. cocobear

    cocobear Guest

    On Aug 4, 3:24 pm, News123 <> wrote:
    > Hi,
    >
    >
    >
    >
    >
    > cocobear wrote:
    > >> > >> Map = Image.new("RGB", (x,y))
    > >> > >> Map.paste(im, box)
    > >> > >> Map.paste(im1,box)

    >
    > >> > >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)

    >
    > >> > >> But if thetwopngpicis too big , or if I have tomergemorepic
    > >> > >> together, I will get MemoryError:

    >
    > >>>>> > >> >>> Image.new("RGB",(44544,38656))

    >
    > >> What do you want to do with such a big image? You will run into the same
    > >> limitation when you are trying to display it.

    >
    > > I want to download a map from google map with high precision

    >
    > For me it sounds strange to handle such big files.
    >
    > Is the map you download from Google really that big?
    > Mostly Google maps uses normally tiles of sizes 256x256 pixel, wich you
    > have to compose to get the  big image.
    >


    The map I downloaded from Google is small(256x256). These small maps
    will compose a big image.



    > If your problem is to combine tiles with different palettes, then you
    > should pobably look at a way to  'unify' the palette of all tiles befor
    > combining them.
    >


    I think it is impossible to 'unify' all this tiles.


    > Being no expert of PIL I don't know whether this is easily possible though?
    >
    > bye
    >
    > N
    cocobear, Aug 4, 2009
    #7
  8. cocobear

    News123 Guest

    You didn't really tell us why you need one big file, which contains more
    pixel, than you can probably store in RAM

    Is it for printing?
    Not knwoing your goal makes it difficult to suggest solutions.


    Can't you use other image formats like raw RGB or
    What other image formats are supported?
    tiff? Raw? PPM ( http://en.wikipedia.org/wiki/Portable_pixmap )?

    concatenating raw/ppm files with the same width would be really trivial.

    so you could use python to create horizontal raw/ppm stripes.

    then you could concatenate them without having to have the whole image
    in RAM.


    Perhaps there's even converters from ppm to png, that don't need the
    whole image in RAM.



    cocobear wrote:
    > On Aug 4, 3:24 pm, News123 <> wrote:
    >> Hi,
    >>
    >>
    >>
    >>
    >>
    >> cocobear wrote:
    >>>>>>> Map = Image.new("RGB", (x,y))
    >>>>>>> Map.paste(im, box)
    >>>>>>> Map.paste(im1,box)
    >>>>>>> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)
    >>>>>>> But if thetwopngpicis too big , or if I have tomergemorepic
    >>>>>>> together, I will get MemoryError:
    >>>>>>>>>>>>> Image.new("RGB",(44544,38656))
    >>>> What do you want to do with such a big image? You will run into the same
    >>>> limitation when you are trying to display it.
    >>> I want to download a map from google map with high precision

    >> For me it sounds strange to handle such big files.
    >>
    >> Is the map you download from Google really that big?
    >> Mostly Google maps uses normally tiles of sizes 256x256 pixel, wich you
    >> have to compose to get the big image.
    >>

    >
    > The map I downloaded from Google is small(256x256). These small maps
    > will compose a big image.
    >
    >
    >
    >> If your problem is to combine tiles with different palettes, then you
    >> should pobably look at a way to 'unify' the palette of all tiles befor
    >> combining them.
    >>

    >
    > I think it is impossible to 'unify' all this tiles.
    >
    >
    >> Being no expert of PIL I don't know whether this is easily possible though?
    >>
    >> bye
    >>
    >> N

    >
    News123, Aug 5, 2009
    #8
  9. cocobear

    cocobear Guest

    On Aug 5, 3:44 pm, News123 <> wrote:
    > You didn't really tell us why you need one big file, which contains more
    > pixel, than you can probably store in RAM
    >


    In fact I do not need such a big image.
    I just want to know the solution.


    > Is it for printing?
    > Not knwoing your goal makes it difficult to suggest solutions.
    >
    > Can't you use other image formats like raw RGB or
    > What other image formats are supported?
    > tiff? Raw? PPM (http://en.wikipedia.org/wiki/Portable_pixmap)?
    >


    I want to view image use Windows Pic View.

    I don't familiar with PPM, photoshop don't support PPM, does PIL
    support this format?


    > concatenating raw/ppm files with the same width would be really trivial.
    >
    > so you could use python to create horizontal raw/ppm stripes.
    >
    > then you could concatenate them without having to have the whole image
    > in RAM.
    >
    > Perhaps there's even converters from ppm to png, that don't need the
    > whole image in RAM.
    >
    >
    >
    > cocobear wrote:
    > > On Aug 4, 3:24 pm, News123 <> wrote:
    > >> Hi,

    >
    > >> cocobear wrote:
    > >>>>>>> Map = Image.new("RGB", (x,y))
    > >>>>>>> Map.paste(im, box)
    > >>>>>>> Map.paste(im1,box)
    > >>>>>>> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE)
    > >>>>>>> But if thetwopngpicis too big , or if I have tomergemorepic
    > >>>>>>> together, I will get MemoryError:
    > >>>>>>>>>>>>> Image.new("RGB",(44544,38656))
    > >>>> What do you want to do with such a big image? You will run into the same
    > >>>> limitation when you are trying to display it.
    > >>> I want to download a map from google map with high precision
    > >> For me it sounds strange to handle such big files.

    >
    > >> Is the map you download from Google really that big?
    > >> Mostly Google maps uses normally tiles of sizes 256x256 pixel, wich you
    > >> have to compose to get the  big image.

    >
    > > The map I downloaded from Google is small(256x256). These small maps
    > > will compose a big image.

    >
    > >> If your problem is to combine tiles with different palettes, then you
    > >> should pobably look at a way to  'unify' the palette of all tiles befor
    > >> combining them.

    >
    > > I think it is impossible to 'unify' all this tiles.

    >
    > >> Being no expert of PIL I don't know whether this is easily possible though?

    >
    > >> bye

    >
    > >> N
    cocobear, Aug 6, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dixon
    Replies:
    4
    Views:
    552
    Piotr Dobrowolski
    Jan 30, 2006
  2. Onur Bozkurt
    Replies:
    0
    Views:
    379
    Onur Bozkurt
    Jun 30, 2003
  3. Daniel
    Replies:
    1
    Views:
    305
    clintonG
    Jul 10, 2004
  4. Replies:
    0
    Views:
    458
  5. nntp
    Replies:
    9
    Views:
    190
    Anno Siegel
    Oct 12, 2004
Loading...

Share This Page