Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
douban-api-proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
douban-api-proxy
Commits
89516fc2
Commit
89516fc2
authored
Apr 17, 2019
by
Ozzieisaacs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make PIL optional #885
parent
e1e79a73
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
48 deletions
+58
-48
book_formats.py
cps/book_formats.py
+41
-37
helper.py
cps/helper.py
+17
-11
No files found.
cps/book_formats.py
View file @
89516fc2
...
...
@@ -62,7 +62,11 @@ except ImportError as e:
logger
.
warning
(
'cannot import fb2, extracting fb2 metadata will not work:
%
s'
,
e
)
use_fb2_meta
=
False
from
PIL
import
Image
try
:
from
PIL
import
Image
use_PIL
=
True
except
ImportError
:
use_PIL
=
False
def
process
(
tmp_file_path
,
original_file_name
,
original_file_extension
):
...
...
@@ -133,47 +137,47 @@ def pdf_preview(tmp_file_path, tmp_dir):
if
use_generic_pdf_cover
:
return
None
else
:
try
:
input1
=
PdfFileReader
(
open
(
tmp_file_path
,
'rb'
),
strict
=
False
)
page0
=
input1
.
getPage
(
0
)
xObject
=
page0
[
'/Resources'
][
'/XObject'
]
.
getObject
()
for
obj
in
xObject
:
if
xObject
[
obj
][
'/Subtype'
]
==
'/Image'
:
size
=
(
xObject
[
obj
][
'/Width'
],
xObject
[
obj
][
'/Height'
])
data
=
xObject
[
obj
]
.
_data
# xObject[obj].getData()
if
xObject
[
obj
][
'/ColorSpace'
]
==
'/DeviceRGB'
:
mode
=
"RGB"
else
:
mode
=
"P"
if
'/Filter'
in
xObject
[
obj
]:
if
xObject
[
obj
][
'/Filter'
]
==
'/FlateDecode'
:
if
use_PIL
:
try
:
input1
=
PdfFileReader
(
open
(
tmp_file_path
,
'rb'
),
strict
=
False
)
page0
=
input1
.
getPage
(
0
)
xObject
=
page0
[
'/Resources'
][
'/XObject'
]
.
getObject
()
for
obj
in
xObject
:
if
xObject
[
obj
][
'/Subtype'
]
==
'/Image'
:
size
=
(
xObject
[
obj
][
'/Width'
],
xObject
[
obj
][
'/Height'
])
data
=
xObject
[
obj
]
.
_data
# xObject[obj].getData()
if
xObject
[
obj
][
'/ColorSpace'
]
==
'/DeviceRGB'
:
mode
=
"RGB"
else
:
mode
=
"P"
if
'/Filter'
in
xObject
[
obj
]:
if
xObject
[
obj
][
'/Filter'
]
==
'/FlateDecode'
:
img
=
Image
.
frombytes
(
mode
,
size
,
data
)
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.png"
img
.
save
(
filename
=
os
.
path
.
join
(
tmp_dir
,
cover_file_name
))
return
cover_file_name
# img.save(obj[1:] + ".png")
elif
xObject
[
obj
][
'/Filter'
]
==
'/DCTDecode'
:
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.jpg"
img
=
open
(
cover_file_name
,
"wb"
)
img
.
write
(
data
)
img
.
close
()
return
cover_file_name
elif
xObject
[
obj
][
'/Filter'
]
==
'/JPXDecode'
:
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.jp2"
img
=
open
(
cover_file_name
,
"wb"
)
img
.
write
(
data
)
img
.
close
()
return
cover_file_name
else
:
img
=
Image
.
frombytes
(
mode
,
size
,
data
)
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.png"
img
.
save
(
filename
=
os
.
path
.
join
(
tmp_dir
,
cover_file_name
))
return
cover_file_name
# img.save(obj[1:] + ".png")
elif
xObject
[
obj
][
'/Filter'
]
==
'/DCTDecode'
:
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.jpg"
img
=
open
(
cover_file_name
,
"wb"
)
img
.
write
(
data
)
img
.
close
()
return
cover_file_name
elif
xObject
[
obj
][
'/Filter'
]
==
'/JPXDecode'
:
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.jp2"
img
=
open
(
cover_file_name
,
"wb"
)
img
.
write
(
data
)
img
.
close
()
return
cover_file_name
else
:
img
=
Image
.
frombytes
(
mode
,
size
,
data
)
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.png"
img
.
save
(
filename
=
os
.
path
.
join
(
tmp_dir
,
cover_file_name
))
return
cover_file_name
# img.save(obj[1:] + ".png")
except
Exception
as
ex
:
print
(
ex
)
except
Exception
as
ex
:
print
(
ex
)
try
:
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.jpg"
with
Image
(
filename
=
tmp_file_path
+
"[0]"
,
resolution
=
150
)
as
img
:
...
...
cps/helper.py
View file @
89516fc2
...
...
@@ -35,7 +35,6 @@ from flask_babel import gettext as _
from
flask_login
import
current_user
from
babel.dates
import
format_datetime
from
datetime
import
datetime
from
PIL
import
Image
import
shutil
import
requests
try
:
...
...
@@ -52,6 +51,12 @@ try:
except
ImportError
:
use_unidecode
=
False
try
:
from
PIL
import
Image
use_PIL
=
True
except
ImportError
:
use_PIL
=
False
# Global variables
# updater_thread = None
global_WorkerThread
=
worker
.
WorkerThread
()
...
...
@@ -481,16 +486,17 @@ def save_cover(img, book_path):
web
.
app
.
logger
.
error
(
"Only jpg/jpeg/png/webp files are supported as coverfile"
)
return
False
# convert to jpg because calibre only supports jpg
if
content_type
in
(
'image/png'
,
'image/webp'
):
if
hasattr
(
img
,
'stream'
):
imgc
=
Image
.
open
(
img
.
stream
)
else
:
imgc
=
Image
.
open
(
io
.
BytesIO
(
img
.
content
))
im
=
imgc
.
convert
(
'RGB'
)
tmp_bytesio
=
io
.
BytesIO
()
im
.
save
(
tmp_bytesio
,
format
=
'JPEG'
)
img
.
_content
=
tmp_bytesio
.
getvalue
()
if
use_PIL
:
# convert to jpg because calibre only supports jpg
if
content_type
in
(
'image/png'
,
'image/webp'
):
if
hasattr
(
img
,
'stream'
):
imgc
=
Image
.
open
(
img
.
stream
)
else
:
imgc
=
Image
.
open
(
io
.
BytesIO
(
img
.
content
))
im
=
imgc
.
convert
(
'RGB'
)
tmp_bytesio
=
io
.
BytesIO
()
im
.
save
(
tmp_bytesio
,
format
=
'JPEG'
)
img
.
_content
=
tmp_bytesio
.
getvalue
()
if
ub
.
config
.
config_use_google_drive
:
tmpDir
=
gettempdir
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment