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
a7ab7fcf
Commit
a7ab7fcf
authored
Nov 03, 2019
by
Ozzieisaacs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix #1068 PDF cover Images inverted or incorrect single image grabbed from cover page
parent
6f61e80c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
118 deletions
+7
-118
helper.py
cps/helper.py
+3
-3
uploader.py
cps/uploader.py
+4
-115
No files found.
cps/helper.py
View file @
a7ab7fcf
...
...
@@ -54,7 +54,7 @@ except ImportError:
use_unidecode
=
False
try
:
from
PIL
import
Image
from
PIL
import
Image
as
PILImage
use_PIL
=
True
except
ImportError
:
use_PIL
=
False
...
...
@@ -497,9 +497,9 @@ def save_cover(img, book_path):
# 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
)
imgc
=
PIL
Image
.
open
(
img
.
stream
)
else
:
imgc
=
Image
.
open
(
io
.
BytesIO
(
img
.
content
))
imgc
=
PIL
Image
.
open
(
io
.
BytesIO
(
img
.
content
))
im
=
imgc
.
convert
(
'RGB'
)
tmp_bytesio
=
io
.
BytesIO
()
im
.
save
(
tmp_bytesio
,
format
=
'JPEG'
)
...
...
cps/uploader.py
View file @
a7ab7fcf
...
...
@@ -20,11 +20,7 @@
from
__future__
import
division
,
print_function
,
unicode_literals
import
os
import
hashlib
import
struct
from
tempfile
import
gettempdir
from
flask_babel
import
gettext
as
_
from
.
import
logger
,
comic
from
.constants
import
BookMeta
...
...
@@ -69,16 +65,13 @@ except ImportError as e:
use_fb2_meta
=
False
try
:
from
PIL
import
Image
,
ImageOps
from
PIL
import
PILImage
from
PIL
import
__version__
as
PILversion
use_PIL
=
True
except
ImportError
as
e
:
log
.
debug
(
'cannot import Pillow, using png and webp images as cover will not work:
%
s'
,
e
)
use_generic_pdf_cover
=
True
use_PIL
=
False
__author__
=
'lemmsh'
...
...
@@ -146,119 +139,15 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
languages
=
""
)
def
CMYKInvert
(
img
):
return
Image
.
merge
(
img
.
mode
,[
ImageOps
.
invert
(
b
.
convert
(
'L'
))
for
b
in
img
.
split
()])
def
tiff_header_for_CCITT
(
width
,
height
,
img_size
,
CCITT_group
=
4
):
tiff_header_struct
=
'<'
+
'2s'
+
'h'
+
'l'
+
'h'
+
'hhll'
*
8
+
'h'
return
struct
.
pack
(
tiff_header_struct
,
b
'II'
,
# Byte order indication: Little indian
42
,
# Version number (always 42)
8
,
# Offset to first IFD
8
,
# Number of tags in IFD
256
,
4
,
1
,
width
,
# ImageWidth, LONG, 1, width
257
,
4
,
1
,
height
,
# ImageLength, LONG, 1, lenght
258
,
3
,
1
,
1
,
# BitsPerSample, SHORT, 1, 1
259
,
3
,
1
,
CCITT_group
,
# Compression, SHORT, 1, 4 = CCITT Group 4 fax encoding
262
,
3
,
1
,
0
,
# Threshholding, SHORT, 1, 0 = WhiteIsZero
273
,
4
,
1
,
struct
.
calcsize
(
tiff_header_struct
),
# StripOffsets, LONG, 1, len of header
278
,
4
,
1
,
height
,
# RowsPerStrip, LONG, 1, lenght
279
,
4
,
1
,
img_size
,
# StripByteCounts, LONG, 1, size of image
0
# last IFD
)
def
pdf_preview
(
tmp_file_path
,
tmp_dir
):
if
use_generic_pdf_cover
:
return
None
else
:
if
use_PIL
:
try
:
input1
=
PdfFileReader
(
open
(
tmp_file_path
,
'rb'
),
strict
=
False
)
page0
=
input1
.
getPage
(
0
)
mediaBox
=
page0
[
'/MediaBox'
]
box
=
page0
[
'/CropBox'
]
if
'/CropBox'
in
page0
else
mediaBox
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
mode
=
"P"
if
xObject
[
obj
][
'/ColorSpace'
]
==
'/DeviceRGB'
:
mode
=
"RGB"
if
xObject
[
obj
][
'/ColorSpace'
]
==
'/DeviceCMYK'
:
mode
=
"CMYK"
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
()
# Post processing
img2
=
Image
.
open
(
cover_file_name
)
width
,
height
=
img2
.
size
if
mode
==
'CMYK'
:
img2
=
CMYKInvert
(
img2
)
img2
=
img2
.
crop
((
box
[
0
]
/
mediaBox
[
2
]
*
width
,
box
[
1
]
/
mediaBox
[
3
]
*
height
,
box
[
2
]
/
mediaBox
[
2
]
*
width
,
box
[
3
]
/
mediaBox
[
3
]
*
height
))
img2
.
save
(
cover_file_name
)
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
()
# Post processing
img2
=
Image
.
open
(
cover_file_name
)
width
,
height
=
img2
.
size
if
mode
==
'CMYK'
:
img2
=
CMYKInvert
(
img2
)
img2
=
img2
.
crop
((
box
[
0
]
/
mediaBox
[
2
]
*
width
,
box
[
1
]
/
mediaBox
[
3
]
*
height
,
box
[
2
]
/
mediaBox
[
2
]
*
width
,
box
[
3
]
/
mediaBox
[
3
]
*
height
))
img2
.
save
(
cover_file_name
)
return
cover_file_name
elif
xObject
[
obj
][
'/Filter'
]
==
'/CCITTFaxDecode'
:
if
xObject
[
obj
][
'/DecodeParms'
][
'/K'
]
==
-
1
:
CCITT_group
=
4
else
:
CCITT_group
=
3
width
=
xObject
[
obj
][
'/Width'
]
height
=
xObject
[
obj
][
'/Height'
]
img_size
=
len
(
data
)
tiff_header
=
tiff_header_for_CCITT
(
width
,
height
,
img_size
,
CCITT_group
)
cover_file_name_tiff
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
obj
[
1
:]
+
'.tiff'
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
obj
[
1
:]
+
'.jpg'
img
=
open
(
cover_file_name_tiff
,
"wb"
)
img
.
write
(
tiff_header
+
data
)
img
.
close
()
# Post processing
img2
=
Image
.
open
(
cover_file_name_tiff
)
if
img2
.
mode
==
'1'
:
img2
=
ImageOps
.
invert
(
img2
.
convert
(
'RGB'
))
img2
.
save
(
cover_file_name
)
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
)
try
:
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.jpg"
with
Image
(
filename
=
tmp_file_path
+
"[0]"
,
resolution
=
150
)
as
img
:
with
Image
()
as
img
:
img
.
options
[
"pdf:use-cropbox"
]
=
"true"
img
.
read
(
filename
=
tmp_file_path
+
'[0]'
,
resolution
=
150
)
img
.
compression_quality
=
88
img
.
save
(
filename
=
os
.
path
.
join
(
tmp_dir
,
cover_file_name
))
return
cover_file_name
...
...
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