more conversion handling

This commit is contained in:
Cyberes 2023-11-11 11:16:03 -07:00
parent 1f118b1dc9
commit ace6f641b6
2 changed files with 19 additions and 7 deletions

View File

@ -3,6 +3,7 @@ from pathlib import Path
import PIL
from PIL import Image
from tqdm import tqdm
def random_file_width(base_path: Path):
@ -25,3 +26,10 @@ def is_png(file_path):
except PIL.UnidentifiedImageError as e:
# tqdm.write(str(e))
return False, None
def convert(file_path):
img = Image.open(file_path)
if img.format != 'PNG':
img.save(file_path, format='PNG')
tqdm.write(f'Converted {file_path} from {img.format} to PNG')

View File

@ -6,7 +6,7 @@ from PIL import Image
from tqdm import tqdm
from pkg.proxies import PROXIES
from .image import is_png
from .image import convert, is_png
def del_path(p: Path):
@ -23,11 +23,17 @@ def download_tile(task):
output_path: Path = output / f"{row}_{col}.png"
if output_path.exists():
valid_png_file, image_type = is_png(output_path)
if not valid_png_file:
if convert_to_png and image_type != 'PNG':
# The image was read sucessfully by PIL but it's in the wrong format.
convert(output_path)
if not is_png(output_path):
tqdm.write(f'PNG conversion for {output_path} failed. Deleting and retrying...')
corrupted_image = True
elif not valid_png_file:
# We will re-download the image. Don't need to delete it, just overwrite it.
# del_path(output_path)
corrupted_image = True
tqdm.write(f'Cannot identify image file: "{output_path}" (is {image_type}), deleting and retrying...')
tqdm.write(f'Bad image file: "{output_path}" (is format: {image_type}), deleting and retrying...')
else:
return row, col, 'exist'
tile_url = f"{base_url}/{row}/{col}".replace('//', '/').replace(':/', '://')
@ -40,10 +46,8 @@ def download_tile(task):
f.write(response.content)
if convert_to_png:
img = Image.open(output_path)
if img.format != 'PNG':
img.save(output_path, format='PNG')
tqdm.write(f'Converted {output_path} from {img.format} to PNG')
convert(output_path)
corrupted_image = True # force a re-check
# Recheck the PNG if it was corrupted.
if corrupted_image and not is_png(output_path):