Created
August 2, 2018 12:47
-
-
Save dario61081/832148dbbecd46aea45bd0c316f24097 to your computer and use it in GitHub Desktop.
Exportar imagenes de la bbdd a un directorio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dotenv import * | |
from sqlalchemy import * | |
from sqlalchemy.sql import text as txt | |
from sqlalchemy.pool import NullPool | |
from PIL import Image | |
import io | |
import os | |
class dataserver: | |
username = 'drgg' | |
userpass = 'melica' | |
host = '192.168.0.254' | |
port = 1521 | |
database = 'prod' | |
def __init__(self): | |
self.url = 'oracle+cx_oracle://{}:{}@{}:{}/{}'.format(self.username, self.userpass, self.host, self.port, self.database) | |
self.engine = create_engine(self.url, poolclass=NullPool) | |
self.conexion = self.engine.connect() | |
self.metadata = MetaData(bind=self.engine) | |
# self.metadata.reflect(bind=self.engine, schema='farma') | |
def select(self, sql,*args, **kwargs): | |
return self.conexion.execute(sql, *args, **kwargs) | |
def gettable(self, tablename): | |
return self.metadata.tables[tablename] | |
def select_many(self, sql, *args, **kwargs): | |
return self.conexion.execute(txt(sql), *args, **kwargs) | |
if __name__ == '__main__': | |
sizes = [900,217,245,315,69] | |
image_path = os.path.join(os.path.dirname(__file__),'imagenes') | |
print image_path | |
no_image = 0 | |
afectados = 0 | |
# os.path.makedirs(image_path, exist_ok=True) | |
d = dataserver() | |
datos = d.select_many('select PROD_CODIGO, IMAG_CODIGO, IMAG_OBJETO from IMAGENES where rownum < 10') | |
print "Iniciando exportacion, aguarde...\r" | |
for item in datos: | |
for s in sizes: | |
filename = "{0}_{1}x{1}.jpg".format(item.imag_codigo, s) | |
# print filename | |
if item.imag_objeto: | |
image = Image.open(io.BytesIO(item.imag_objeto)) | |
image = image.resize((s,s), Image.ANTIALIAS) | |
image.save(filename, "JPEG") | |
afectados+=1 | |
else: | |
no_image+=1 | |
# image = Image.open(io.BytesIO(item.imag_codigo)) | |
#image.save(os.path.join(image_path, str(item.prod_codigo),'.jpg')) | |
print "Fin de exportacion" | |
print "{} afectado(s) {} no contienen imagenes".format(afectados, no_image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment