diff --git a/README.md b/README.md index 424e1d2..8174a84 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,29 @@ -# - -把pdf变成五彩斑斓的pdf! +# 五彩斑斓的黑 + +## 项目简介 + +把pdf文件转化为五彩斑斓的pdf! +这样看黑白论文的时候就不无聊了( + +## 环境依赖 + +``` +conda install flask imagemagick wand ghostscript opencv -c conda-forge +yum install mesa-libGL.x86_64 +``` + +## 运行 + +``` +python server.py +``` + +## TODOS + +异步返回处理结果 +使用`OSS`减轻网络io负担 +更多文件图片格式支持 + +## 备注 + +写着玩的项目,看心情维护 diff --git a/color.py b/color.py new file mode 100644 index 0000000..046f540 --- /dev/null +++ b/color.py @@ -0,0 +1,58 @@ +import os +import cv2 +import numpy as np + + +def memoize(func): + cache = dict() + + def memoized_func(*args): + if args in cache: + return cache[args] + result = func(*args) + cache[args] = result + return result + + return memoized_func + + +@memoize +def get_color_img(width, height): + w = np.logspace(0, 255, height, base=1.01, dtype=np.uint8) + w = np.reshape(w, (-1, 1)) + h = np.linspace(0, 255, width, dtype=np.uint8) + h = np.reshape(h, (1, -1)) + shu = w * h + shu = np.reshape(shu, (height, -1)) + shu = cv2.cvtColor(shu, cv2.COLOR_GRAY2BGR) + shu = cv2.applyColorMap(shu, cv2.COLORMAP_HSV) + + w = np.linspace(0, 255, height, dtype=np.uint8) + w = np.reshape(w, (-1, 1)) + h = np.logspace(0, 255, width, base=1.01, dtype=np.uint8) + h = np.reshape(h, (1, -1)) + heng = w * h + heng = np.reshape(heng, (height, -1)) + heng = cv2.cvtColor(heng, cv2.COLOR_GRAY2BGR) + heng = cv2.applyColorMap(heng, cv2.COLORMAP_HSV) + + img = heng + shu + + img = cv2.medianBlur(img, 101) + return img + + +def mix_img(file_dir, filename, count): + path = os.path.join(file_dir, filename) + for i in range(1, count): + img = cv2.imread(path + str(i) + '.jpg') + color = get_color_img(img.shape[1], img.shape[0]) + mask = (img == (0, 0, 0))[:, :, 0] + img[mask] = color[mask] + mix = img + cv2.imwrite(path + str(i) + '.jpg', mix) + + +if __name__ == '__main__': + img = get_color_img(100, 100) + cv2.imwrite('tmp/1.jpg', img) diff --git a/entity.py b/entity.py new file mode 100644 index 0000000..06fa4b8 --- /dev/null +++ b/entity.py @@ -0,0 +1,36 @@ +import pdf +import color +import os + + +class Pdf: + def __init__(self, file_dir, filename, output_path): + self.file_dir = file_dir + self.filename = filename + self.pdf = pdf.parse_pdf(filepath=os.path.join(file_dir, filename)) + self.page_count = 0 + self.output_path = output_path + + def extract(self, tmp_dir='tmp/'): + return pdf.save_pdf_as_img(pdf=self.pdf, filename=tmp_dir + self.filename) + + def convert(self): + if self.page_count == 0: + return + color.mix_img(file_dir='tmp/', filename=self.filename, count=self.page_count) + return + + def save(self): + return pdf.save_img_as_pdf(file_dir='tmp/', filename=self.filename, + count=self.page_count, output_dir=self.output_path, output_filename=self.filename) + + def get_output_filename(self): + self.page_count = self.extract() + self.convert() + self.save() + return self.filename + + +if __name__ == '__main__': + test = Pdf('/Users/licsber/PycharmProjects/五彩斑斓的黑/pdf', '1580826601.pdf', 'output') + print(test.get_output_filename()) diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..23e7e8a Binary files /dev/null and b/favicon.ico differ diff --git a/pdf.py b/pdf.py new file mode 100644 index 0000000..809b363 --- /dev/null +++ b/pdf.py @@ -0,0 +1,32 @@ +from wand.image import Image as wi +import os + + +def parse_pdf(filepath, resolution=300): + pdf = wi(filename=filepath, resolution=resolution) + pdf = pdf.convert("jpeg") + return pdf + + +def save_pdf_as_img(pdf, filename): + page_count = 1 + for img in pdf.sequence: + page = wi(image=img) + page.save(filename=filename + str(page_count) + '.jpg') + page_count += 1 + return page_count + + +def save_img_as_pdf(file_dir, filename, count, output_dir, output_filename): + path = os.path.join(file_dir, filename) + output_path = os.path.join(output_dir, output_filename) + with wi() as w: + for i in range(1, count): + with wi(filename=path + str(i) + '.jpg') as page: + w.sequence.append(page) + w.save(filename=output_path) + return output_path + + +if __name__ == '__main__': + path = 'pdf/1580826601.pdf' diff --git a/server.py b/server.py new file mode 100644 index 0000000..3ffb10d --- /dev/null +++ b/server.py @@ -0,0 +1,68 @@ +import os +import time +from flask import Flask, render_template, send_from_directory, request + +import entity + +app = Flask(__name__) + +UPLOAD_FOLDER = 'pdf' +app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +basedir = os.path.abspath(os.path.dirname(__file__)) +ALLOWED_EXTENSIONS = {'pdf'} + +UPLOAD_HTML = 'upload.html' +OUTPUT_PATH = 'output' +output_dir = os.path.join(basedir, OUTPUT_PATH) +if not os.path.exists(output_dir): + os.makedirs(output_dir) + +if not os.path.exists('tmp'): + os.makedirs('tmp') + +file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER']) +if not os.path.exists(file_dir): + os.makedirs(file_dir) + + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS + + +@app.route('/') +def upload_test(): + message = '' + return render_template(UPLOAD_HTML, message=message) + + +@app.route('/api/upload', methods=['POST'], strict_slashes=False) +def api_upload(): + f = request.files['myfile'] + if f: + old_name = f.filename + ext = old_name.rsplit('.', 1)[1] + unix_time = int(time.time()) + new_filename = old_name + str(unix_time) + '.' + ext + print(new_filename) + path = os.path.join(file_dir, new_filename) + f.save(path) + else: + message = '你没上传文件哦' + return render_template(UPLOAD_HTML, message=message) + if f and allowed_file(f.filename): + pdf = entity.Pdf(file_dir, new_filename, OUTPUT_PATH) + return downloader(pdf.get_output_filename()) + else: + message = '文件类型不支持哦 重新上传试试呢' + return render_template(UPLOAD_HTML, message=message) + + +@app.route("/download/") +def downloader(filename): + dir_path = os.path.join(app.root_path, 'output') + return send_from_directory(dir_path, filename, as_attachment=True) + + +if __name__ == '__main__': + print('启动目录:' + basedir) + app.run(host='0.0.0.0', port=10088) diff --git a/templates/upload.html b/templates/upload.html new file mode 100644 index 0000000..da8accf --- /dev/null +++ b/templates/upload.html @@ -0,0 +1,33 @@ + + + + + 五彩斑斓的黑 - Licsber + + + + + +
+ +

欢迎试用一键五彩斑斓的黑,支持上传pdf。

+

创意By Licsber、Mikewang000000。

+
+
+ + +
+
+
+ + +