From 45610ff8b3d8868dec8586f51be4c8b91cdee4bd Mon Sep 17 00:00:00 2001 From: bai Date: Tue, 28 Jul 2020 04:06:01 -0400 Subject: Usando el mas nuevo check_output para crear miniaturas --- cgi/img.py | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'cgi/img.py') diff --git a/cgi/img.py b/cgi/img.py index ef64ac0..73bbdc7 100644 --- a/cgi/img.py +++ b/cgi/img.py @@ -95,15 +95,24 @@ def processImage(post, data, t, originalname, spoiler=False): logTime("Generating thumbnail") if used_filetype['mime'][:5] == 'video': - retcode = subprocess.call([ - Settings.FFMPEG_PATH, '-strict', '-2', '-ss', '0', '-i', file_path, - '-v', 'quiet', '-an', '-vframes', '1', '-f', 'mjpeg', '-vf', 'scale=%d:%d' % ( - file_thumb_width, file_thumb_height), - '-threads', '1', file_thumb_path]) - if spoiler: - args = [Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread", "1", "-background", "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height), "-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ( - (file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler", "-quality", str(Settings.THUMB_QUALITY), file_thumb_path] - retcode = subprocess.call(args) + # Create preview for video AND spoiler it if necessary + try: + subprocess.check_output([ + Settings.FFMPEG_PATH, '-strict', '-2', '-ss', '0', '-i', file_path, + '-v', 'quiet', '-an', '-vframes', '1', '-f', 'mjpeg', '-vf', 'scale=%d:%d' % ( + file_thumb_width, file_thumb_height), + '-threads', '1', file_thumb_path]) + if spoiler: + subprocess.check_output([Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread", + "1", "-background", "white", "-flatten", "-resize", + "%dx%d" % (file_thumb_width, file_thumb_height), "-blur", "0x12", + "-gravity", "center", "-fill", "rgba(0,0,0, .6)", + "-draw", "rectangle 0,%d,%d,%d" % ((file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), + "-fill", "white", "-annotate", "0", "Alerta de spoiler", + "-quality", str(Settings.THUMB_QUALITY), file_thumb_path]) + except subprocess.CalledProcessError, e: + os.remove(file_path) + raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' elif used_filetype['mime'][:5] == 'audio': # we do an exception and use png for audio waveform thumbnails since they # 1. are smaller 2. allow for transparency @@ -119,19 +128,14 @@ def processImage(post, data, t, originalname, spoiler=False): file_thumb_width = 150 file_thumb_height = 75 - retcode = subprocess.call([ - Settings.FFMPEG_PATH, '-t', '300', '-i', file_path, + try: + subprocess.check_output([Settings.FFMPEG_PATH, '-t', '300', '-i', file_path, '-filter_complex', 'showwavespic=s=%dx%d:split_channels=1' % ( int(file_thumb_width), int(file_thumb_height)), '-frames:v', '1', '-threads', '1', file_thumb_path]) -# elif used_filetype['mime'] == 'application/x-shockwave-flash' or used_filetype['mime'] == 'mime/x-shockwave-flash': -# retcode = subprocess.call([ -# './ffmpeg', '-i', file_path, '-vcodec', 'mjpeg', '-vframes', '1', '-an', '-f', 'rawvideo', -# '-vf', 'scale=%d:%d' % (file_thumb_width, file_thumb_height), '-threads', '1', file_thumb_path]) - - if retcode != 0: - os.remove(file_path) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(retcode)+')' + except subprocess.CalledProcessError, e: + os.remove(file_path) + raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' else: # use imagemagick to make thumbnail args = [Settings.CONVERT_PATH, file_path, "-limit", "thread", "1", "-background", @@ -143,10 +147,11 @@ def processImage(post, data, t, originalname, spoiler=False): # generate thumbnails logTime("Generating thumbnail") - retcode = subprocess.call(args) - if retcode != 0: + try: + subprocess.check_output(args) + except subprocess.CalledProcessError, e: os.remove(file_path) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(retcode)+')' + raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' # check if thumbnail was truly created try: -- cgit v1.2.1-18-gbd029 From e4a14199d57455083e71f7b68b78fc231d32fc77 Mon Sep 17 00:00:00 2001 From: bai Date: Tue, 28 Jul 2020 04:28:12 -0400 Subject: Usando logging correcto de Python --- cgi/img.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'cgi/img.py') diff --git a/cgi/img.py b/cgi/img.py index 73bbdc7..ff7cd74 100644 --- a/cgi/img.py +++ b/cgi/img.py @@ -4,6 +4,7 @@ import math #import random import os import subprocess +import logging from StringIO import StringIO from settings import Settings @@ -92,7 +93,7 @@ def processImage(post, data, t, originalname, spoiler=False): if used_filetype['ffmpeg_thumb'] == '1': # use ffmpeg to make thumbnail - logTime("Generating thumbnail") + logging.debug("Generating thumbnail") if used_filetype['mime'][:5] == 'video': # Create preview for video AND spoiler it if necessary @@ -112,6 +113,7 @@ def processImage(post, data, t, originalname, spoiler=False): "-quality", str(Settings.THUMB_QUALITY), file_thumb_path]) except subprocess.CalledProcessError, e: os.remove(file_path) + logging.error("Thumbnail creation failure: " + e.output) raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' elif used_filetype['mime'][:5] == 'audio': # we do an exception and use png for audio waveform thumbnails since they @@ -135,6 +137,7 @@ def processImage(post, data, t, originalname, spoiler=False): '-frames:v', '1', '-threads', '1', file_thumb_path]) except subprocess.CalledProcessError, e: os.remove(file_path) + logging.error("Thumbnail creation failure: " + e.output) raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' else: # use imagemagick to make thumbnail @@ -146,11 +149,12 @@ def processImage(post, data, t, originalname, spoiler=False): args += ["-quality", str(Settings.THUMB_QUALITY), file_thumb_path] # generate thumbnails - logTime("Generating thumbnail") + logging.debug("Generating thumbnail") try: subprocess.check_output(args) except subprocess.CalledProcessError, e: os.remove(file_path) + logging.error("Thumbnail creation failure: " + repr(e.output)) raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' # check if thumbnail was truly created -- cgit v1.2.1-18-gbd029 From 6513d960466a7819fbf2bf3f473c8133621ddaa9 Mon Sep 17 00:00:00 2001 From: bai Date: Tue, 28 Jul 2020 05:09:53 -0400 Subject: Manejando funciones de conversión de miniaturas correctamente --- cgi/img.py | 100 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 46 insertions(+), 54 deletions(-) (limited to 'cgi/img.py') diff --git a/cgi/img.py b/cgi/img.py index ff7cd74..9bc25b7 100644 --- a/cgi/img.py +++ b/cgi/img.py @@ -88,74 +88,62 @@ def processImage(post, data, t, originalname, spoiler=False): # Do we need to thumbnail it? if not used_filetype['image']: # make thumbnail + logging.debug("Generating thumbnail") file_thumb_width, file_thumb_height = getThumbDimensions( width, height, maxsize) - - if used_filetype['ffmpeg_thumb'] == '1': - # use ffmpeg to make thumbnail - logging.debug("Generating thumbnail") - - if used_filetype['mime'][:5] == 'video': - # Create preview for video AND spoiler it if necessary - try: - subprocess.check_output([ + + try: + if used_filetype['ffmpeg_thumb'] == '1': + # use ffmpeg to make thumbnail + if used_filetype['mime'][:5] == 'video': + # Create preview for video AND spoiler it if necessary + call_wrap([ Settings.FFMPEG_PATH, '-strict', '-2', '-ss', '0', '-i', file_path, '-v', 'quiet', '-an', '-vframes', '1', '-f', 'mjpeg', '-vf', 'scale=%d:%d' % ( file_thumb_width, file_thumb_height), '-threads', '1', file_thumb_path]) if spoiler: - subprocess.check_output([Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread", + call_wrap([Settings.CONVERT_PATH, file_thumb_path, "-limit", "thread", "1", "-background", "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height), "-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ((file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler", "-quality", str(Settings.THUMB_QUALITY), file_thumb_path]) - except subprocess.CalledProcessError, e: - os.remove(file_path) - logging.error("Thumbnail creation failure: " + e.output) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' - elif used_filetype['mime'][:5] == 'audio': - # we do an exception and use png for audio waveform thumbnails since they - # 1. are smaller 2. allow for transparency - file_thumb_name = file_thumb_name[:-3] + "png" - file_thumb_path = file_thumb_path[:-3] + "png" - file_mobile_path = file_mobile_path[:-3] + "png" - file_cat_path = file_cat_path[:-3] + "png" - - if int(board['thumb_px']) > 149: - file_thumb_width = board['thumb_px'] - file_thumb_height = float(int(board['thumb_px'])/2) - else: - file_thumb_width = 150 - file_thumb_height = 75 - - try: - subprocess.check_output([Settings.FFMPEG_PATH, '-t', '300', '-i', file_path, - '-filter_complex', 'showwavespic=s=%dx%d:split_channels=1' % ( + elif used_filetype['mime'][:5] == 'audio': + # we do an exception and use png for audio waveform thumbnails since they + # 1. are smaller 2. allow for transparency + file_thumb_name = file_thumb_name[:-3] + "png" + file_thumb_path = file_thumb_path[:-3] + "png" + file_mobile_path = file_mobile_path[:-3] + "png" + file_cat_path = file_cat_path[:-3] + "png" + + if int(board['thumb_px']) > 149: + file_thumb_width = board['thumb_px'] + file_thumb_height = float(int(board['thumb_px'])/2) + else: + file_thumb_width = 150 + file_thumb_height = 75 + + call_wrap([Settings.FFMPEG_PATH, '-t', '300', '-i', file_path, + '-filter_complex', 'showwavespic=s=%dx%d:split_channels=1' % ( int(file_thumb_width), int(file_thumb_height)), - '-frames:v', '1', '-threads', '1', file_thumb_path]) - except subprocess.CalledProcessError, e: - os.remove(file_path) - logging.error("Thumbnail creation failure: " + e.output) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' - else: - # use imagemagick to make thumbnail - args = [Settings.CONVERT_PATH, file_path, "-limit", "thread", "1", "-background", - "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height)] - if spoiler: - args += ["-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ( - (file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler"] - args += ["-quality", str(Settings.THUMB_QUALITY), file_thumb_path] - - # generate thumbnails - logging.debug("Generating thumbnail") - try: - subprocess.check_output(args) - except subprocess.CalledProcessError, e: - os.remove(file_path) - logging.error("Thumbnail creation failure: " + repr(e.output)) - raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' + '-frames:v', '1', '-threads', '1', file_thumb_path]) + else: + # use imagemagick to make thumbnail + args = [Settings.CONVERT_PATH, file_path, "-limit", "thread", "1", "-background", + "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height)] + if spoiler: + args += ["-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ( + (file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler"] + args += ["-quality", str(Settings.THUMB_QUALITY), file_thumb_path] + + # generate thumbnails + call_wrap(args) + except subprocess.CalledProcessError, e: + os.remove(file_path) + logging.error("Thumbnail creation failure: " + e.output) + raise UserError, _("Thumbnail creation failure.") + ' ('+str(e.returncode)+')' # check if thumbnail was truly created try: @@ -380,6 +368,10 @@ def ffprobe_f(filename): return json.loads(out) +def call_wrap(args): + subprocess.check_output(args, stderr=subprocess.STDOUT) + + def getThumbDimensions(width, height, maxsize): """ Calculate dimensions to use for a thumbnail with maximum width/height of -- cgit v1.2.1-18-gbd029 From 8102e461d107adfb8958bb7a8e28a9ca9ff540ee Mon Sep 17 00:00:00 2001 From: bai Date: Tue, 28 Jul 2020 05:33:19 -0400 Subject: Arreglado error al usar spoilers en miniaturas --- cgi/img.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'cgi/img.py') diff --git a/cgi/img.py b/cgi/img.py index 9bc25b7..48d4277 100644 --- a/cgi/img.py +++ b/cgi/img.py @@ -108,7 +108,7 @@ def processImage(post, data, t, originalname, spoiler=False): "%dx%d" % (file_thumb_width, file_thumb_height), "-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ((file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), - "-fill", "white", "-annotate", "0", "Alerta de spoiler", + "-fill", "white", "-font", "Liberation-Sans", "-annotate", "0", "Alerta de spoiler", "-quality", str(Settings.THUMB_QUALITY), file_thumb_path]) elif used_filetype['mime'][:5] == 'audio': # we do an exception and use png for audio waveform thumbnails since they @@ -135,7 +135,8 @@ def processImage(post, data, t, originalname, spoiler=False): "white", "-flatten", "-resize", "%dx%d" % (file_thumb_width, file_thumb_height)] if spoiler: args += ["-blur", "0x12", "-gravity", "center", "-fill", "rgba(0,0,0, .6)", "-draw", "rectangle 0,%d,%d,%d" % ( - (file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", "-annotate", "0", "Alerta de spoiler"] + (file_thumb_height/2)-10, file_thumb_width, (file_thumb_height/2)+7), "-fill", "white", + "-font", "Liberation-Sans", "-annotate", "0", "Alerta de spoiler"] args += ["-quality", str(Settings.THUMB_QUALITY), file_thumb_path] # generate thumbnails -- cgit v1.2.1-18-gbd029 From 27c48ff31e5ecc635f0620711699db996b3e996c Mon Sep 17 00:00:00 2001 From: root Date: Thu, 30 Jul 2020 05:59:43 -0400 Subject: Agregado soporte para WebP --- cgi/img.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'cgi/img.py') diff --git a/cgi/img.py b/cgi/img.py index 48d4277..1c57e37 100644 --- a/cgi/img.py +++ b/cgi/img.py @@ -31,6 +31,7 @@ def processImage(post, data, t, originalname, spoiler=False): # get image information content_type, width, height, size, extra = getImageInfo(data) + logging.info("{} {} {}".format(content_type, width, height)) # check the size is fine if size > int(board["maxsize"])*1024: @@ -291,6 +292,22 @@ def getImageInfo(data): except ValueError: pass + # handle WebP + if data[:4] == b'RIFF' and data[8:12] == b'WEBP': + chunk = data[12:] + if chunk[:4] == b"VP8 " and chunk[11:14] == b"\x9d\x01\x2a": + # Lossy VP8 + w, h = struct.unpack("HH", chunk[14:18]) + width = w & 0x3fff + height = h & 0x3fff + content_type = "image/webp" + elif chunk[:4] == b"VP8L": + # Lossless VP8 + b0, b1, b2, b3 = struct.unpack("BBBB", chunk[9:13]) + width = 1 + (((b1 & 0x3F) << 8) | b0) + height = 1 + (((b3 & 0xF) << 10) | (b2 << 2) | ((b1 & 0xC0) >> 6)) + content_type = "image/webp" + # handle WebM elif (size >= 4) and data.startswith("\x1A\x45\xDF\xA3"): content_type = "video/webm" -- cgit v1.2.1-18-gbd029