Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 10, 2011 15:09:13

g.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django and PIL problem?


I try to create thumbnail via PIL

import Image
ARTICLE_LARGE_SIZE = 230,300
tmp_file = Image.open(form.cleaned_data)
tmp_file = tmp_file.thumbnail(ARTICLE_SMALL_SIZE)
tmp_file.save(location + '/small.jpg', 'JPEG')

I get: 'NoneType' object has no attribute 'save'
WTF?
When i replace tmp_file.thumbnail to tmp_file.resize all is fine but i loose
proportions:/

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 10, 2011 15:41:59

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django and PIL problem?


Image.resize returns a new instance of Image of the new size, leaving
the internal data of the original intact, so:

>> tmp_file = tmp_file.thumbnail(ARTICLE_SMALL_SIZE)

will create a new Image instance based on the original, with the new
size, then it sets the value of the tmp_file variable to the new image
instead of the original.

Image.thumbnail replaces the data in the current instance, it doesn't
return anything, so:

>> tmp_file = tmp_file.thumbnail(ARTICLE_SMALL_SIZE)

Will change the internal data for tmp_file, then it will set the value
of the tmp_file variable to None (the return value of Image.thumbnail).
If you simply leave out the "tmp_file = " part, it should work the way
you expect:

>> tmp_file.thumbnail(ARTICLE_SMALL_SIZE)http://www.pythonware.com/library/pil/handbook/image.htm--
Michael <mhall...@gmail.com>

On Mon, 2011-01-10 at 07:09 -0800, galago wrote:
> I try to create thumbnail via PIL
>
>
> import Image
>
> ARTICLE_LARGE_SIZE = 230,300
>
> tmp_file = Image.open(form.cleaned_data)
> tmp_file = tmp_file.thumbnail(ARTICLE_SMALL_SIZE)
>
> tmp_file.save(location + '/small.jpg', 'JPEG')
>
>
>
> I get: 'NoneType' object has no attribute 'save'
> WTF?
> When i replace tmp_file.thumbnail to tmp_file.resize all is fine but i
> loose proportions:/
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to django-users
> +unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 10, 2011 16:32:58

Steve M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django and PIL problem?


Unlike some of the other methods in PIL "thumbnail" modifies the file
in place and returns None. This will probably do what you expect:

import Image
ARTICLE_LARGE_SIZE = 230,300
tmp_file = Image.open(form.cleaned_data)
tmp_file.thumbnail(ARTICLE_SMALL_SIZE)
tmp_file.save(location + '/small.jpg', 'JPEG')


On 10 January 2011 15:09, galago <prog...@gmail.com> wrote:
> I try to create thumbnail via PIL
> import Image
> ARTICLE_LARGE_SIZE = 230,300
> tmp_file = Image.open(form.cleaned_data)
> tmp_file = tmp_file.thumbnail(ARTICLE_SMALL_SIZE)
> tmp_file.save(location + '/small.jpg', 'JPEG')
>
> I get: 'NoneType' object has no attribute 'save'
> WTF?
> When i replace tmp_file.thumbnail to tmp_file.resize all is fine but i loose
> proportions:/
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>



--
stevehttp://stevemcconville.com/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 10, 2011 19:56:03

g.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django and PIL problem?


Thanks. It works great :)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 10, 2011 21:52:00

g.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django and PIL problem?


I get another strange error - this time only while adding article. In
editing all is fine.
def save_model(self, request, obj, form, change):

if change == False:

obj.save()

if ('image' in form.changed_data) or (change == False):

tmp_file = Image.open(form.cleaned_data)


It gives me an error:

Exception Type: IOError Exception Value:

cannot identify image file

What is wrong in my code? I checked its type() and it's the same when i edit
model - I'm confused. Type is the same, but in add method it throws an
error.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Jan. 10, 2011 22:26:44

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django and PIL problem?


I have the same problem:/
Anyone cah help?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Jan. 11, 2011 02:49:13

Martin O.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django and PIL problem?


What's the value of form.cleaned_data? That IOError is
usually thrown when the contents of what you pass in to open() don't
constitute a valid image.

On Jan 10, 5:26 pm, atcive <galo...@gmail.com> wrote:
> I have the same problem:/
> Anyone cah help?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support