473,789 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parameters for Image.fromstrin g function in pil

3 New Member
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
Feb 14 '08 #1
1 11093
hulaohu
1 New Member
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")


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
Mar 10 '08 #2

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

Similar topics

0
2005
by: Bob | last post by:
My understanding of the image string function is this, that it allows the display of text in Graphics As per the parameters it receives: ImageString($image,6,$xpos,$ypos,$out,$col) these params: 1. name of thew image to write on 2. size of the font 3. X co-ordinate of the first letter of the string
3
4462
by: tlviewer | last post by:
hello, The script below is a prototype for loading playing card images from the bitmap resource in cards.dll (WinXP) I like the idea of not keeping copies of the images as files. I'm able to extract the card images as files, then load them in pygame as a surface, but I keep getting errors
6
1813
by: simon | last post by:
Always when I need data reader in my programs, I simply have functions, which creates it for me: Dim rdr As SqlDataReader dim sql as string sql="myStoredProcedure" rdr = createDataReader(sql, False) And the functions are:
4
5210
by: Steve Neill | last post by:
Here's an interesting problem... I have 2 functions, aFunc() and bFunc(). I call aFunc() passing a call to bFunc() as a parameter. Function aFunc() executes bFunc(). Function bFunc() lists the parameter values passed to it. OK, here's the twist... I want to add some parameters to bFunc() from inside aFunc(). Can that be done?
0
1553
by: Paul Allan | last post by:
I am new to ASP.net (intermediate ASP developer). I am developing a ASP.net web application and I am having some difficulty calling and passing parameters to a function that is declared in my "codebehind" page. Function declaration: Sub MyFunction(ByVal sender As Object, ByVal e As System.EventArgs)
2
1844
by: Chris Ochs | last post by:
I am trying to move all of the sql statements from my code into a pl/pgsql function. I have one function for a transaction block that contains 10 inserts and I need to pass 50 parameters to it. Well I can't do that, so I was playing around with passing the parameters in as a varchar array, but then I have the issue of the variables being the wrong type when I insert them. I was playing around with trying to convert types, but it isn't...
4
10652
by: astri | last post by:
#include "Unit1.h" #include "math.h" #include "fixed_math.hpp" #include "algorithm.h" #define MBIT 0x4000 #define CBIT 16 long constbl; void __fastcall TForm1::Button1Click(TObject *Sender)
0
2667
by: Question123 | last post by:
Hi I am developing windows c# application in which i have given reference of third party DLL. "Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang." Does anybody knows why this error comes.?? plz help me..
6
1267
by: Janwillem Borleffs | last post by:
Tuxedo schreef: You can check the Image.complete property; example: img = new Image(100,100); interval = setInterval('checkcomplete()', 100); img.src = 'someimg'; function checkcomplete() { if (window.complete) {
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.