Skip to content

Instantly share code, notes, and snippets.

@thaisviana
Created May 25, 2020 13:54
Show Gist options
  • Save thaisviana/859c8a2d0c594e2fc783e57e1e866bba to your computer and use it in GitHub Desktop.
Save thaisviana/859c8a2d0c594e2fc783e57e1e866bba to your computer and use it in GitHub Desktop.
import dropbox
from PIL import Image
import os, sys
import requests
from io import BytesIO
from PIL import TiffImagePlugin
#TiffImagePlugin.DEBUG = True
dbx = dropbox.Dropbox(key, timeout=120.0)
DROPBOX_INPUT_PATH_TIF='/Users/thaisviana/hub9/laestampa-api/home/print/tif/'
DROPBOX_INPUT_PATH_SMALL='/Users/thaisviana/hub9/laestampa-api/home/print/small/'
codes_small = []
result_tif = dbx.files_list_folder(DROPBOX_INPUT_PATH_TIF, recursive=True)
result_small = dbx.files_list_folder(DROPBOX_INPUT_PATH_SMALL, recursive=True)
file_list = []
def process_entries(entries, type):
for entry in entries:
if isinstance(entry, dropbox.files.FileMetadata):
if type == 'tif':
file_list.append(entry.name.replace('.tif', ''))
else:
codes_small.append(entry.name.replace('.jpeg', ''))
process_entries(result_tif.entries, 'tif')
process_entries(result_small.entries, 'small')
while result_tif.has_more:
result_tif = dbx.files_list_folder_continue(result_tif.cursor)
process_entries(result_tif.entries, 'tif')
while result_small.has_more:
result_small = dbx.files_list_folder_continue(result_small.cursor)
process_entries(result_small.entries, 'small')
erro_estampas = []
print(len(codes_small))
print(len(file_list))
novos = list(set(file_list) - set(codes_small))
Image.MAX_IMAGE_PIXELS = None
print('__________', len(novos), '__________')
tamanho = len(novos)
if 'half1' in sys.argv:
novos = novos[:tamanho//2]
if 'half2' in sys.argv:
novos = novos[tamanho//2:]
file_list.sort()
for index, code in enumerate(novos):
try:
if True:
meta = dbx.files_get_temporary_link(f"{DROPBOX_INPUT_PATH_TIF}{code}.tif")
response = requests.get(meta.link)
im = TiffImagePlugin.TiffImageFile(BytesIO(response.content))
#im = Image.open()
im = im.convert('RGB')
if im.width > im.height:
im = im.rotate(90, expand=1)
new_width = 1024 * im.width // im.height
im = im.resize((new_width,1024), Image.ANTIALIAS)
im.save(f'jpeg_corr/{code}.jpeg',quality=100)
with open(f'jpeg_corr/{code}.jpeg', 'rb') as f:
dbx.files_upload(f.read(), f"{DROPBOX_INPUT_PATH_SMALL}{code}.jpeg", mode=dropbox.files.WriteMode.overwrite)
print(code)
except Exception as e:
print(e, code)
print('end', index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment