Re: OT? plpython2u

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Nahum Castro <nahumcastro(at)gmail(dot)com>
Cc: Jonathan Rogers <jrogers(at)socialserve(dot)com>, psycopg(at)postgresql(dot)org
Subject: Re: OT? plpython2u
Date: 2016-10-20 19:55:21
Message-ID: 8b1a5964-6f8a-ae00-2030-e249ef04eed0@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda psycopg

On 10/20/2016 12:52 PM, Nahum Castro wrote:
> Oh, Awesome.
>
> Thanks a lot.

FYI, I forgot to close the mem_file in the example I sent.

>
> Nahum.
>
> 2016-10-20 14:47 GMT-05:00 Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com
> <mailto:adrian(dot)klaver(at)aklaver(dot)com>>:
>
> On 10/20/2016 12:03 PM, Nahum Castro wrote:
>
>
>
> 2016-10-20 13:06 GMT-05:00 Jonathan Rogers
> <jrogers(at)socialserve(dot)com <mailto:jrogers(at)socialserve(dot)com>
> <mailto:jrogers(at)socialserve(dot)com <mailto:jrogers(at)socialserve(dot)com>>>:
>
>
> On 10/20/2016 01:14 PM, Nahum Castro wrote:
> > Hello all.
> >
> > I need to process images on my database, they are stored
> on a bytea column.
> >
> > The process consist on scaling and I have done this so far:
> >
> > CREATE OR REPLACE FUNCTION ajustar(randstring bytea)
> > RETURNS bytea AS
> > $$
> > import PIL
> > from PIL import Image
> > basewidth = 300
> > img = randstring
> > wpercent = (basewidth/float(img.size[0]))
> > hsize = int((float(img.size[1])*float(wpercent)))
> > img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
> >
> > return img
> > $$
> > LANGUAGE 'plpython2u' VOLATILE;
>
> As you probably know, Postgres procedural languages are
> server-side
> extensions and therefore have nothing to do with psycopg,
> which is a
> client-side library.
>
> ​Thanks, but rather than help in plpython is how to pass the
> variable
> bytea from postgres to an image in python.
>
>
> >
> > The objective of this pl is read the bytea from the
> database, scale,
> > then replace the scaled image in the database, but I don't
> know what
> > type use to pass to the variable img.
>
> AFAICT, if you pass a bytea value to a plpythonu function,
> it will
> receive a Python str object.
>
>
> ​That is where i get confused, what is stored in the db is in
> bytea, how
> I do to transform in a str?​
>
>
> It is done for you:
>
> https://www.postgresql.org/docs/9.5/static/plpython-data.html#AEN65498
> <https://www.postgresql.org/docs/9.5/static/plpython-data.html#AEN65498>
>
> "PostgreSQL bytea is converted to Python str in Python 2 and to
> bytes in Python 3. In Python 2, the string should be treated as a
> byte sequence without any character encoding.
>
> When the PostgreSQL return type is bytea, the return value will be
> converted to a string (Python 2) or bytes (Python 3) using the
> respective Python built-ins, with the result being converted to bytea."
>
>
> So something like this:
>
> CREATE OR REPLACE FUNCTION public.ajustar(randstring bytea)
> RETURNS bytea
> LANGUAGE plpythonu
> AS $function$
> from io import BytesIO
> import PIL
> from PIL import Image
> basewidth = 300
> mem_file = BytesIO()
> mem_file.write(randstring)
> img = Image.open(mem_file)
> wpercent = (basewidth/float(img.size[0]))
> hsize = int((float(img.size[1])*float(wpercent)))
> img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
>
> return img
> $function$
>
>
> postgres(at)test=# select ajustar(barcode) from pict_test where
> full_upc = '744835000544';
> ajustar
> ----------------------------------------------------------------------------------------------------------------------------------
>
> \x3c50494c2e496d6167652e496d61676520696d616765206d6f64653d5247422073697a653d333030783230392061742030783746393343314532333635303e
> (1 row)
>
>
> barcode are small png's in this case.
>
>
>
>
>
> >
> > This is the query.
> > update personal set foto=ajustar(encode(foto, 'hex')::bytea);
>
> Why are you encoding the binary data into a textual
> representation? What
> image format is stored in column "foto"? Depending on the
> format, you
> probably want to use one of these constructors in function
> "ajustar":
>
>
> ​Sorry I forgot, all images are in jpeg​ format.
>
>
>
>
> http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.fromstring
> <http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.fromstring>
>
> <http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.fromstring
> <http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.fromstring>>
>
>
> http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.open
> <http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.open>
>
> <http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.open
> <http://pillow.readthedocs.io/en/3.4.x/reference/Image.html?highlight=Image#PIL.Image.open>>
>
>
> ​Again thanks.​
> Nahum
>
>
>
> --
> Jonathan Rogers
> Socialserve.com by Emphasys Software
> jrogers(at)emphasys-software(dot)com
> <mailto:jrogers(at)emphasys-software(dot)com>
> <mailto:jrogers(at)emphasys-software(dot)com
> <mailto:jrogers(at)emphasys-software(dot)com>>
>
> --
> Jonathan Rogers
> Socialserve.com by Emphasys Software
> jrogers(at)emphasys-software(dot)com
> <mailto:jrogers(at)emphasys-software(dot)com>
> <mailto:jrogers(at)emphasys-software(dot)com
> <mailto:jrogers(at)emphasys-software(dot)com>>
>
>
> --
> Sent via psycopg mailing list (psycopg(at)postgresql(dot)org
> <mailto:psycopg(at)postgresql(dot)org>
> <mailto:psycopg(at)postgresql(dot)org <mailto:psycopg(at)postgresql(dot)org>>)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/psycopg
> <http://www.postgresql.org/mailpref/psycopg>
> <http://www.postgresql.org/mailpref/psycopg
> <http://www.postgresql.org/mailpref/psycopg>>
>
>
>
>
> --
> *Nahum Castro González*
> Blvd. Perdigón 214, Brisas del Lago.
> CP 37207
> León, Guanajuato, México
> Tel (477)1950304
> Cel (477)1274694
>
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com <mailto:adrian(dot)klaver(at)aklaver(dot)com>
>
>
>
>
> --
> *Nahum Castro González*
> Blvd. Perdigón 214, Brisas del Lago.
> CP 37207
> León, Guanajuato, México
> Tel (477)1950304
> Cel (477)1274694

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Nahum Castro 2016-10-20 23:51:18 Re: [psycopg] OT? plpython2u
Previous Message Nahum Castro 2016-10-20 19:52:24 Re: OT? plpython2u

Browse psycopg by date

  From Date Subject
Next Message Nahum Castro 2016-10-20 23:51:18 Re: [psycopg] OT? plpython2u
Previous Message Nahum Castro 2016-10-20 19:52:24 Re: OT? plpython2u