parameters for Image.fromstring function in pil

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bharathv6
    New Member
    • Feb 2008
    • 3

    parameters for Image.fromstring function in pil

    i need to do is modify the image in memory like resizing the image in memory etc ... with out saving it disk as i have to return back the image with out saving it disk
    PIL supports the use of StringIO objects being passed in place of file
    objects. StringIO objects are binary strings of variable length that are kept
    in memory so i saved the image in stringio objects the following code does that

    for gif image
    import Image
    import StringIO
    im = Image.open("/home/bharath/Desktop/photos/lena.gif")
    fil=StringIO.St ringIO()
    im.save(fil,"GI F")
    cached_image=fi l.getvalue()

    or

    for jpeg image
    import Image
    import StringIO
    im = Image.open("/home/bharath/Desktop/photos/lena.jpg")
    fil=StringIO.St ringIO()
    im.save(fil,"JP EG")
    cached_image=fi l.getvalue()

    but i can't apply resize operation on cached_image as it a str object
    but i found out that Image.formstrin g(mode, size, data, decoder, parameters) Creates an image memory from pixel data in a string so i did the following

    for gif image
    ima=Image.froms tring('P',(128, 128),cached_ima ge,'gif')
    imr=ima.resize( (64,64),Image.A NTIALIAS)
    imr.save("/home/bharath/Desktop/photos/resize_lena3.gi f","GIF")

    i am getting the following error

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/bharath/Desktop/image.py", line 8, in <module>
    ima=Image.froms tring('P',(128, 128),cached_ima ge,'gif')
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
    im.fromstring(d ata, decoder_name, args)
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 577, in fromstring
    raise ValueError("can not decode image data")
    ValueError: cannot decode image data

    for jpeg image
    ma=Image.fromst ring('P',(128,1 28),cached_imag e,'jpeg')
    imr=ima.resize( (64,64),Image.A NTIALIAS)
    imr.save("/home/bharath/Desktop/photos/resize_lena3.gi f","JPEG")

    i am getting the following error ...

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/bharath/Desktop/image.py", line 8, in <module>
    ima=Image.froms tring('RGB',(12 8,128),cached_i mage,'jpeg')
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
    im.fromstring(d ata, decoder_name, args)
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 570, in fromstring
    d = _getdecoder(sel f.mode, decoder_name, args)
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 373, in _getdecoder
    return apply(decoder, (mode,) + args + extra)
    TypeError: function takes at least 3 arguments (1 given)

    but if i remove the 4th parameter that is decoder_name i.e 'gif' or 'jpeg' in function Image.formstrin g(mode, size, data, decoder, parameters) there is no error(when decoder_name is not given it defaults to raw image type) .. image even gets resized and gets saved but that image is distorted image that shows that it is not decoded in correct way .. and when i want a image to be decoded according to a format i.e gif or jpeg i think i need to pass a extra parameter in the function Image.fromstrin g(mode, size, data, decoder, parameters) along with a decoder name to be decoded correctly .

    pls can u tell what extra parameters i have to pass with an example (taking my example itself )
    Reply

    Forward
  • hulaohu
    New Member
    • Mar 2008
    • 1

    #2
    I'm not sure what to pass in for the fromstring module. But you can do something like this to save a JPEG image from string buffer:

    newImage = Image.fromstrin g('L', (325, 256), cached_image_st ring)
    newImage.save(f ilename, "JPEG")


    Originally posted by bharathv6
    i need to do is modify the image in memory like resizing the image in memory etc ... with out saving it disk as i have to return back the image with out saving it disk
    PIL supports the use of StringIO objects being passed in place of file
    objects. StringIO objects are binary strings of variable length that are kept
    in memory so i saved the image in stringio objects the following code does that

    for gif image
    import Image
    import StringIO
    im = Image.open("/home/bharath/Desktop/photos/lena.gif")
    fil=StringIO.St ringIO()
    im.save(fil,"GI F")
    cached_image=fi l.getvalue()

    or

    for jpeg image
    import Image
    import StringIO
    im = Image.open("/home/bharath/Desktop/photos/lena.jpg")
    fil=StringIO.St ringIO()
    im.save(fil,"JP EG")
    cached_image=fi l.getvalue()

    but i can't apply resize operation on cached_image as it a str object
    but i found out that Image.formstrin g(mode, size, data, decoder, parameters) Creates an image memory from pixel data in a string so i did the following

    for gif image
    ima=Image.froms tring('P',(128, 128),cached_ima ge,'gif')
    imr=ima.resize( (64,64),Image.A NTIALIAS)
    imr.save("/home/bharath/Desktop/photos/resize_lena3.gi f","GIF")

    i am getting the following error

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/bharath/Desktop/image.py", line 8, in <module>
    ima=Image.froms tring('P',(128, 128),cached_ima ge,'gif')
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
    im.fromstring(d ata, decoder_name, args)
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 577, in fromstring
    raise ValueError("can not decode image data")
    ValueError: cannot decode image data

    for jpeg image
    ma=Image.fromst ring('P',(128,1 28),cached_imag e,'jpeg')
    imr=ima.resize( (64,64),Image.A NTIALIAS)
    imr.save("/home/bharath/Desktop/photos/resize_lena3.gi f","JPEG")

    i am getting the following error ...

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/bharath/Desktop/image.py", line 8, in <module>
    ima=Image.froms tring('RGB',(12 8,128),cached_i mage,'jpeg')
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
    im.fromstring(d ata, decoder_name, args)
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 570, in fromstring
    d = _getdecoder(sel f.mode, decoder_name, args)
    File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 373, in _getdecoder
    return apply(decoder, (mode,) + args + extra)
    TypeError: function takes at least 3 arguments (1 given)

    but if i remove the 4th parameter that is decoder_name i.e 'gif' or 'jpeg' in function Image.formstrin g(mode, size, data, decoder, parameters) there is no error(when decoder_name is not given it defaults to raw image type) .. image even gets resized and gets saved but that image is distorted image that shows that it is not decoded in correct way .. and when i want a image to be decoded according to a format i.e gif or jpeg i think i need to pass a extra parameter in the function Image.fromstrin g(mode, size, data, decoder, parameters) along with a decoder name to be decoded correctly .

    pls can u tell what extra parameters i have to pass with an example (taking my example itself )
    Reply

    Forward

    Comment

    Working...