473,388 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

manipulaton of image in memeory

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
file = StringIO()
image.save(file, "JPEG")
cached_image = file.getvalue()
but i can't apply resize operation on cached_image as it a str object
but i found out that Image.formstring(mode, size, data, decoder, parameters) Creates an image memory from pixel data in a string so i did the following

image=Image.fromstring('P',(128,128),cached_image, 'gif')

but the i got following error shown below can any one tell me what was the error .. or tell me an alternative to modify the image in memory with out saving in disk ..

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
im.fromstring(data, decoder_name, args)
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 577, in fromstring
raise ValueError("cannot decode image data")
ValueError: cannot decode image data
Feb 13 '08 #1
2 3210
dshimer
136 Expert 100+
I'm not sure I totally understand your question, but it looks like you are trying to make it too difficult. It seems to me like PIL modifies everything in memory. For example when playing around with new methods I have the script open an image, do several things to it, then just show it to the screen. When things are finalized, I put in the code to re-write the new image. In the following session, I


  1. import most everything I need because we work with several types of images.
  2. create an open image object.
  3. get the size just to show what it is
  4. rotate (with expansion) it and show it's new size
  5. rotate, resize, then show the new size just to show the progression of what is happening in memory.
Expand|Select|Wrap|Line Numbers
  1. >>> import Image,JpegImagePlugin,TiffImagePlugin,PngImagePlugin,BmpImagePlugin>>> im=Image.open('/tmp/test.jpg','r')
  2. >>> im.size
  3. (1122, 777)
  4. >>> im.rotate(45,0,1).size
  5. (1344, 1343)
  6. >>> im.rotate(45,0,1).resize(100,100).size
  7. >>> im.rotate(45,0,1).resize([100,100],3).size
  8. (100, 100)
  9.  
In place of size, I could have used show() if I wanted to see the results visually, or save() if I wanted to write it out after final manipulation. I have programs that use these simple methods to do things like rotating, resizing, compositing, applying level adjustments and so on.


Using these methods, I haven't had much success manipulating images in the 1Gb range but I'm beginning to think that is more a problem with Windows than with PIL (because I have 2 other programs written by "real programmers "which should work fine that also choke on images over 1 Gb).


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
file = StringIO()
image.save(file, "JPEG")
cached_image = file.getvalue()
but i can't apply resize operation on cached_image as it a str object
but i found out that Image.formstring(mode, size, data, decoder, parameters) Creates an image memory from pixel data in a string so i did the following

image=Image.fromstring('P',(128,128),cached_image, 'gif')

but the i got following error shown below can any one tell me what was the error .. or tell me an alternative to modify the image in memory with out saving in disk ..

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
im.fromstring(data, decoder_name, args)
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 577, in fromstring
raise ValueError("cannot decode image data")
ValueError: cannot decode image data
Feb 13 '08 #2
thanks for replying
let me make it clear to u ... what i need to really do is to implement an image server which gets images from other server and store it cache (not in disk) when i get a request from any clent for a particular image of particular size i need to get the image from the cache resize it and return the image to the client . as the data got from the cache by memcahe is of type str object i cannot do reize operation on it so i need to convert it to a image in memory so that i can do resize operation on it ..
so i did the following.
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.StringIO()
im.save(fil,"GIF")
cached_image=fil.getvalue()

or

for jpeg image
import Image
import StringIO
im = Image.open("/home/bharath/Desktop/photos/lena.jpg")
fil=StringIO.StringIO()
im.save(fil,"JPEG")
cached_image=fil.getvalue()

but i can't apply resize operation on cached_image as it a str object
but i found out that Image.formstring(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.fromstring('P',(128,128),cached_image,'g if')
imr=ima.resize((64,64),Image.ANTIALIAS)
imr.save("/home/bharath/Desktop/photos/resize_lena3.gif","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.fromstring('P',(128,128),cached_image,'g if')
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
im.fromstring(data, decoder_name, args)
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 577, in fromstring
raise ValueError("cannot decode image data")
ValueError: cannot decode image data

for jpeg image
ma=Image.fromstring('P',(128,128),cached_image,'jp eg')
imr=ima.resize((64,64),Image.ANTIALIAS)
imr.save("/home/bharath/Desktop/photos/resize_lena3.gif","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.fromstring('RGB',(128,128),cached_image, 'jpeg')
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1744, in fromstring
im.fromstring(data, decoder_name, args)
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 570, in fromstring
d = _getdecoder(self.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.formstring(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.fromstring(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 )


I'm not sure I totally understand your question, but it looks like you are trying to make it too difficult. It seems to me like PIL modifies everything in memory. For example when playing around with new methods I have the script open an image, do several things to it, then just show it to the screen. When things are finalized, I put in the code to re-write the new image. In the following session, I


  1. import most everything I need because we work with several types of images.
  2. create an open image object.
  3. get the size just to show what it is
  4. rotate (with expansion) it and show it's new size
  5. rotate, resize, then show the new size just to show the progression of what is happening in memory.
Expand|Select|Wrap|Line Numbers
  1. >>> import Image,JpegImagePlugin,TiffImagePlugin,PngImagePlugin,BmpImagePlugin>>> im=Image.open('/tmp/test.jpg','r')
  2. >>> im.size
  3. (1122, 777)
  4. >>> im.rotate(45,0,1).size
  5. (1344, 1343)
  6. >>> im.rotate(45,0,1).resize(100,100).size
  7. >>> im.rotate(45,0,1).resize([100,100],3).size
  8. (100, 100)
  9.  
In place of size, I could have used show() if I wanted to see the results visually, or save() if I wanted to write it out after final manipulation. I have programs that use these simple methods to do things like rotating, resizing, compositing, applying level adjustments and so on.


Using these methods, I haven't had much success manipulating images in the 1Gb range but I'm beginning to think that is more a problem with Windows than with PIL (because I have 2 other programs written by "real programmers "which should work fine that also choke on images over 1 Gb).
Feb 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Phil Powell | last post by:
I borrowed this code from a source: for($a=0;$a<imagecolorstotal ($image_id);$a++) { $color = imageColorsForIndex($image_id,$i); $R=.299 * ($color)+ .587 * ($color)+ .114 * ($color);...
8
by: Jef Driesen | last post by:
I'm implementing some image processing algorithms in C++. I created a class called 'image' (see declaration below), that will take care of the memory allocations and some basic (mathematical)...
6
by: QuasiChameleon | last post by:
Hi, I'm trying to create a grayscale image class that reads and writes grayscale Targa format. This works well with smaller images, but corrupts larger images and creates a "Segmentation fault...
15
by: Anand Ganesh | last post by:
HI All, I have an Image. I want to clip a portion of it and copy to another image. How to do this? I know the bounding rectangle to clip. Any suggestions please. Thanks for your time and...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
6
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional)...
1
by: bharathv6 | last post by:
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...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.