forked from rebillar/site-accueil-insa
Added thumbnails support and script to create them
This commit is contained in:
parent
1321def3e1
commit
eeb3b480bc
8 changed files with 160 additions and 86 deletions
|
@ -29,7 +29,9 @@
|
|||
|
||||
.photos img {
|
||||
display: inline-flex;
|
||||
width: 25%;
|
||||
/* 4/3 format */
|
||||
width: 140px;
|
||||
height: 105px;
|
||||
opacity: 1;
|
||||
cursor: pointer;
|
||||
transition: 0.3s;
|
||||
|
|
|
@ -18,6 +18,10 @@ function displayBig(elem) {
|
|||
photoOverlay.fadeIn(500);
|
||||
}
|
||||
|
||||
function getSourceFromThumbnail(source) {
|
||||
return source.replace("photos_thumb/", "photos/");
|
||||
}
|
||||
|
||||
/*
|
||||
* Hide showcase image
|
||||
*/
|
||||
|
@ -107,8 +111,8 @@ function displayNext(direction) {
|
|||
var currentSrc = showcase.attr('src');
|
||||
var photos = document.getElementsByClassName("photo");
|
||||
var current = 0;
|
||||
for (i = 0; i < photos.length; i++) {
|
||||
if ($(photos[i]).attr('src') == currentSrc) {
|
||||
for (var i = 0; i < photos.length; i++) {
|
||||
if (getSourceFromThumbnail($(photos[i]).attr('src')) === currentSrc) {
|
||||
current = i;
|
||||
}
|
||||
}
|
||||
|
@ -129,8 +133,9 @@ function displayNext(direction) {
|
|||
/*
|
||||
* Change image source, link and download
|
||||
*/
|
||||
function changeImage(src) {
|
||||
showcase.attr("src", src);
|
||||
showcaseLink.attr("href", src);
|
||||
showcaseDownload.attr("href", src);
|
||||
function changeImage(thumb) {
|
||||
var source = getSourceFromThumbnail(thumb);
|
||||
showcase.attr("src", source);
|
||||
showcaseLink.attr("href", source);
|
||||
showcaseDownload.attr("href", source);
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ function getDirectories($dir)
|
|||
// Get all photos in the specified path
|
||||
function getPhotos($dir)
|
||||
{
|
||||
$dir = "photos".$dir;
|
||||
$dir = "photos_thumb".$dir;
|
||||
$files = scandir($dir);
|
||||
$displayedItems = 0;
|
||||
foreach ($files as $key => $value) {
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 1.2 MiB |
Binary file not shown.
Before Width: | Height: | Size: 1.2 MiB |
Binary file not shown.
Before Width: | Height: | Size: 1.2 MiB |
|
@ -1,78 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import os
|
||||
import sys
|
||||
from zipfile import ZipFile, ZIP_DEFLATED
|
||||
|
||||
|
||||
def zip_dir(path):
|
||||
"""
|
||||
Compress files in the specified directory and sub-directories
|
||||
Create one zip per folder
|
||||
|
||||
:param path: directory to get files in
|
||||
:return:
|
||||
"""
|
||||
for root, dirs, files in os.walk(path):
|
||||
if is_directory_valid(root, len(files)):
|
||||
print("Compressing '" + root + "'", end="")
|
||||
with ZipFile(os.path.join(root, get_current_dir(root)) + ".zip", "w", ZIP_DEFLATED) as zip_file:
|
||||
for fn in files:
|
||||
if not fn.endswith("zip"):
|
||||
print(".", end="")
|
||||
absolute_file_name = os.path.join(root, fn)
|
||||
zippped_file_name = absolute_file_name[len(root)+len(os.sep):] # XXX: relative path
|
||||
zip_file.write(absolute_file_name, zippped_file_name)
|
||||
print("done")
|
||||
print("COMPRESSION FINISHED")
|
||||
|
||||
|
||||
def is_directory_valid(path, files_number):
|
||||
"""
|
||||
Check if the given path is not hidden or emptty
|
||||
:param files_number: number of files in the folder
|
||||
:param path: Path to check
|
||||
:return: True if path contains a hidden folder or is empty, False otherwise
|
||||
"""
|
||||
directories = path.split(os.sep)
|
||||
valid = files_number > 0
|
||||
for dn in directories:
|
||||
if dn.startswith(".") or not valid:
|
||||
valid = False
|
||||
break
|
||||
return valid
|
||||
|
||||
|
||||
def get_current_dir(path):
|
||||
"""
|
||||
Get the name of the current directory
|
||||
:param path: Path to search the name in
|
||||
:return: directory name
|
||||
"""
|
||||
return os.path.basename(os.path.normpath(path))
|
||||
|
||||
|
||||
def get_confirmation(path):
|
||||
"""
|
||||
Tell the user which folders will be compressed, and asks for confirmation
|
||||
:param path: Root path for search
|
||||
:return: True if user confirmed, False otherwise
|
||||
"""
|
||||
print("The following folders will be compressed (hidden and empty folders are ignored):")
|
||||
for root, dirs, files in os.walk(path):
|
||||
if is_directory_valid(root, len(files)):
|
||||
print(root)
|
||||
confirmation = input("Are you sure you want to proceed? [Y/n]")
|
||||
return confirmation == "Y" or confirmation == "y"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Get path from arguments or use the script's path
|
||||
if len(sys.argv) > 1:
|
||||
directory = sys.argv[1]
|
||||
else:
|
||||
directory = os.path.dirname(os.path.realpath(__file__))
|
||||
if get_confirmation(directory):
|
||||
zip_dir(directory)
|
||||
else:
|
||||
print("Compression aborted")
|
145
photos/compress_photos.py
Normal file
145
photos/compress_photos.py
Normal file
|
@ -0,0 +1,145 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import os
|
||||
import sys
|
||||
from zipfile import ZipFile, ZIP_DEFLATED
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def get_images(path):
|
||||
"""
|
||||
get images in the folder specified and all its sub folders (hidden and empty folders are ignored)
|
||||
|
||||
:param path: directory to get images in
|
||||
:return: images list
|
||||
"""
|
||||
file_list = []
|
||||
for root, dirs, files in os.walk(path):
|
||||
if is_directory_valid(root, len(files)):
|
||||
for fn in files:
|
||||
if fn.endswith("png") or fn.endswith("jpg") or fn.endswith("bmp"):
|
||||
file_list.append(os.path.join(root, fn))
|
||||
return file_list
|
||||
|
||||
|
||||
def compress_images(images):
|
||||
"""
|
||||
Compress images to a 140x105 format, cropping in the middle if needed
|
||||
:param images: list o images paths to compress
|
||||
:return:
|
||||
"""
|
||||
size = 140, 105 # 4/3 format
|
||||
print("Creating thumbnails", end="")
|
||||
for current_img in images:
|
||||
print(".", end="")
|
||||
# If height is higher we resize vertically, if not we resize horizontally
|
||||
img = Image.open(current_img)
|
||||
# Get current and desired ratio for the images
|
||||
img_ratio = img.size[0] / float(img.size[1])
|
||||
ratio = size[0] / float(size[1])
|
||||
# The image is scaled/cropped vertically or horizontally depending on the ratio
|
||||
if ratio > img_ratio:
|
||||
img = img.resize((size[0], round(size[0] * img.size[1] / img.size[0])), Image.BILINEAR)
|
||||
# Crop in the middle
|
||||
box = (0, round((img.size[1] - size[1]) / 2), img.size[0], round((img.size[1] + size[1]) / 2))
|
||||
img = img.crop(box)
|
||||
elif ratio < img_ratio:
|
||||
img = img.resize((round(size[1] * img.size[0] / img.size[1]), size[1]), Image.BILINEAR)
|
||||
# Crop in the middle
|
||||
box = (round((img.size[0] - size[0]) / 2), 0, round((img.size[0] + size[0]) / 2), img.size[1])
|
||||
img = img.crop(box)
|
||||
else:
|
||||
img = img.resize((size[0], size[1]), Image.BILINEAR)
|
||||
# If the scale is the same, we do not need to crop
|
||||
filename = get_new_path(current_img)
|
||||
if not os.path.exists(os.path.dirname(filename)):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(filename))
|
||||
except OSError:
|
||||
sys.exit("Fatal : Directory '" + os.path.dirname(filename) + "' does not exist and cannot be created")
|
||||
|
||||
img.save(filename, "JPEG")
|
||||
print("Done. Thumbnails saved in 'photos_thumb' directory")
|
||||
|
||||
|
||||
def get_new_path(img):
|
||||
"""
|
||||
Replace the original path to the thumbnail one (replace 'photos' by 'photos_thumb')
|
||||
:param img: original path
|
||||
:return: modified path
|
||||
"""
|
||||
return img.replace("/photos/", "/photos_thumb/", 1)
|
||||
|
||||
|
||||
def zip_dir(path):
|
||||
"""
|
||||
Compress files in the specified directory and sub-directories
|
||||
Create one zip per folder
|
||||
|
||||
:param path: directory to get files in
|
||||
:return:
|
||||
"""
|
||||
for root, dirs, files in os.walk(path):
|
||||
if is_directory_valid(root, len(files)):
|
||||
print("Compressing '" + root + "'", end="")
|
||||
with ZipFile(os.path.join(root, get_current_dir(root)) + ".zip", "w", ZIP_DEFLATED) as zip_file:
|
||||
for fn in files:
|
||||
if not fn.endswith("zip"):
|
||||
print(".", end="")
|
||||
absolute_file_name = os.path.join(root, fn)
|
||||
zippped_file_name = absolute_file_name[len(root)+len(os.sep):] # XXX: relative path
|
||||
zip_file.write(absolute_file_name, zippped_file_name)
|
||||
print("done")
|
||||
print("COMPRESSION FINISHED")
|
||||
|
||||
|
||||
def is_directory_valid(path, files_number):
|
||||
"""
|
||||
Check if the given path is not hidden or empty
|
||||
:param files_number: number of files in the folder
|
||||
:param path: Path to check
|
||||
:return: True if path contains a hidden folder or is empty, False otherwise
|
||||
"""
|
||||
directories = path.split(os.sep)
|
||||
valid = files_number > 0
|
||||
for dn in directories:
|
||||
if dn.startswith(".") or not valid:
|
||||
valid = False
|
||||
break
|
||||
return valid
|
||||
|
||||
|
||||
def get_current_dir(path):
|
||||
"""
|
||||
Get the name of the current directory
|
||||
:param path: Path to search the name in
|
||||
:return: directory name
|
||||
"""
|
||||
return os.path.basename(os.path.normpath(path))
|
||||
|
||||
|
||||
def get_confirmation(path):
|
||||
"""
|
||||
Tell the user which folders will be compressed, and asks for confirmation
|
||||
:param path: Root path for search
|
||||
:return: True if user confirmed, False otherwise
|
||||
"""
|
||||
print("The following folders will be compressed (hidden and empty folders are ignored):")
|
||||
for root, dirs, files in os.walk(path):
|
||||
if is_directory_valid(root, len(files)):
|
||||
print(root)
|
||||
confirmation = input("Are you sure you want to proceed? [Y/n]")
|
||||
return confirmation == "Y" or confirmation == "y"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Get path from arguments or use the script's path
|
||||
if len(sys.argv) > 1:
|
||||
directory = sys.argv[1]
|
||||
else:
|
||||
directory = os.path.dirname(os.path.realpath(__file__))
|
||||
if get_confirmation(directory):
|
||||
zip_dir(directory)
|
||||
compress_images(get_images(directory))
|
||||
else:
|
||||
print("Compression aborted")
|
Loading…
Reference in a new issue