Re: [psycopg] OT? plpython2u

From: Nahum Castro <nahumcastro(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: pgsql-es-ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: [psycopg] OT? plpython2u
Date: 2016-10-21 01:26:45
Message-ID: CABqArLgH0oFoh2Be5URzqkd_U8GMfVRL++hQ7AqvB9F59qhj_A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda psycopg

At last here is the solution to resize images on bytea columns with
plpythonu
Thanks to all especially to Adrian

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)
salida = io.BytesIO
img.save(salida, format='JPEG')
hex_data = output.getvalue()
img.close()
return hex_data
$function$

And here is how to resize all images from database:

update personal set foto=ajustar(foto) where foto is not null;

2016-10-20 19:48 GMT-05:00 Nahum Castro <nahumcastro(at)gmail(dot)com>:

> As far as I know the process to resize the image is:
>
> read(bytea) -> load_to_memory(image) -> resize(image) ->
> recode_to_bytea(image) -> store(bytea)
>
> You helped me with the first two.
>
> When I run the function ajustar(image)
> update personal set image=ajustar(image)
>
> I tried to do a direct resizing from bytea but python can't understand the
> hexadecimal data from bytea as an jpeg image. Though I tried with with
> plpython2u.
> Thanks
>
> 2016-10-20 19:00 GMT-05:00 Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>:
>
>> On 10/20/2016 04:51 PM, Nahum Castro wrote:
>>
>>> Hello Adrian.
>>>
>>> The result image to replace the big one.
>>>
>>
>> So if you are replacing the data in a bytea column with a function that
>> returns bytea, why encode it?
>>
>> Unless I am missing something why not just UPDATE with the raw(bytea)
>> return value?
>>
>>
>>> I have a table with images @ 12K but, went on vacation an when returned
>>> someone replaced the pictures with high resolution images @6M. The app
>>> then become very slow and halted.
>>>
>>> I have already made the script in python to resize all the images from a
>>> folder before to upload to the database, so it occurred to me do the
>>> same but on the database.
>>>
>>> Thanks again.
>>>
>>> 2016-10-20 18:32 GMT-05:00 Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com
>>> <mailto:adrian(dot)klaver(at)aklaver(dot)com>>:
>>>
>>> On 10/20/2016 02:42 PM, Nahum Castro wrote:
>>>
>>> Hello,
>>>
>>> The script always returns.
>>> select encode(ajustar(foto), 'escape') from personal where
>>> id=193;
>>> encode
>>> ------------------------------------------------------------
>>> -----
>>> <PIL.Image.Image image mode=RGB size=300x347 at 0x7F08A11B1650>
>>>
>>>
>>>
>>> So what do you want it to return a where do you want to return?
>>>
>>>
>>> --
>>> 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
>>
>
>
>
> --
> *Nahum Castro González*
> Blvd. Perdigón 214, Brisas del Lago.
> CP 37207
> León, Guanajuato, México
> Tel (477)1950304
> Cel (477)1274694
>

--
*Nahum Castro González*
Blvd. Perdigón 214, Brisas del Lago.
CP 37207
León, Guanajuato, México
Tel (477)1950304
Cel (477)1274694

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Kernel 2016-10-21 16:52:59 Re: Consulta recursiva
Previous Message Nahum Castro 2016-10-21 00:48:33 Re: [psycopg] OT? plpython2u

Browse psycopg by date

  From Date Subject
Next Message Emanuel Calvo 2016-10-21 17:24:23 Re: [psycopg] OT? plpython2u
Previous Message Nahum Castro 2016-10-21 00:48:33 Re: [psycopg] OT? plpython2u