From a133137d7bc3e12e1afcdf4e8feff5083e84696c Mon Sep 17 00:00:00 2001 From: Jiale Liu <460874737@qq.com> Date: Wed, 5 Feb 2020 21:45:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=20Init=20commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一次提交 Init commit --- README.md | 31 +++++++++++++++++-- color.py | 58 +++++++++++++++++++++++++++++++++++ entity.py | 36 ++++++++++++++++++++++ favicon.ico | Bin 0 -> 67646 bytes pdf.py | 32 ++++++++++++++++++++ server.py | 68 ++++++++++++++++++++++++++++++++++++++++++ templates/upload.html | 33 ++++++++++++++++++++ 7 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 color.py create mode 100644 entity.py create mode 100644 favicon.ico create mode 100644 pdf.py create mode 100644 server.py create mode 100644 templates/upload.html 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 0000000000000000000000000000000000000000..23e7e8aa139a03f097f9b8a1b606ad0194cad484 GIT binary patch literal 67646 zcmeI2yG~S57=;g&c6Qd5JO{)?Lm`O}DU7!_z(@*c(nf|o*x z@&?DfXL6VsoXgn|XuH4m|Yl z0R9iqf7OAXzV8G0KVbi{0}sMEVA=uv3;f3p+<#%&0sM>n#}3@{>j3_R{;LlBcx>~3 zL;Q>V#}3>L=YUBE@P7#YV+WQzI)MNE^S>UvuBEIe&*s;DFVC)p?-L%@x;n9wu1i#&@#FO{~5__ zuoe6_fR@oM_|HgYgRS7d0kn*6!GA_F8*Byt4WMOo%ej^F{QM^)pQ&3p`R-F|ZgsH? z=Rn=X&;L>(8(%;5al1ABezTl=S-S)HH};u2TJ#@Q+3CR5I%fd>Qz08)U-|EJ;7d3M z#&!Vz#y(R=>-FF6z=zm@YopEq{HH=TzJ9m=P6xi0XNPwH|HeL3M;qk7+kxwY&H?mBnE!4EZtU&={*8U6jyB4Frvu-@dqH0Z@Sh6V`1*1FyB)Y$odftc_L(|b9sk`9 z+=?B*e=20->+AaObl`h==G9aD8~aQhEyaKAKv?)sg=~C1{*C1bM#H}VvT+3djpYbN z!@mHsaRmO2|Y3;H{Ne}NZO#5n&0 zI)Hzb96?SU|LqQ3*f|IAFYuy@sO!J#!1U@C{#9}WIVt{Q2g1U?z>6vZ|0>d+Ir#VV zqM8c-D$<@g`1kapnhO6a(w;f^_w=Hg3jZq7o;mpU^rD&y|0>d+Ir#VVqM8c-D$<@g z`1kapnhO6a(w;f^_w=Hg3jZq7o;mpU^rD&y|0>d+Ir#VVqM8c-D$<@g`1kapnhO6a z(w;f^_w=Hg3jZq7o;mpU^ukmSfB*y_009U<00Izz00bZa0SG_<0uX=z1Rwwb2tWV= z5P$##AOHafKmY;|fPg1~l8>") +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。

+
+
+ + +
+
+
+ + +