1
1
import re
2
2
import requests
3
- from PIL import Image
3
+ from PIL import Image , ImageFilter
4
4
from lxml import html
5
5
from urllib .parse import urlparse , parse_qs , urlunparse , urlencode
6
6
from library_db .utils .db_utils import get_user_data , get_user_type , get_media_types
@@ -68,27 +68,47 @@ def update_query_params(url: str, **params):
68
68
)
69
69
70
70
71
+ def crop_image (img : Image .Image , w : int , h : int ) -> Image .Image :
72
+ crop_w = w
73
+ crop_h = h
74
+
75
+ new_w = int (crop_h * (img .width / img .height ))
76
+ img_resized = img .resize ((new_w , crop_h ))
77
+ img_resized = img_resized .convert ('RGB' )
78
+
79
+ if img_resized .width >= crop_w :
80
+ (left , upper ) = (int (img_resized .width / 2 - crop_w / 2 ), 0 )
81
+ (right , lower ) = (
82
+ int (img_resized .width / 2 + crop_w / 2 ),
83
+ img_resized .height ,
84
+ )
85
+
86
+ img_cropped = img_resized .crop ((left , upper , right , lower ))
87
+ return img_cropped
88
+ else :
89
+ img_resized = img_resized .resize ((crop_w , img_resized .height ))
90
+ return img_resized
91
+
92
+
71
93
def process_cover_image (path : str ):
72
- with Image .open (path ) as img :
73
- new_h = 400
74
- new_w = int (new_h * (img .width / img .height ))
75
- img_resized = img .resize ((new_w , new_h ))
76
- img_resized = img_resized .convert ('RGB' )
77
-
78
- crop_w = 270
79
- if img_resized .width >= crop_w :
80
- (left , upper ) = (int (img_resized .width / 2 - crop_w / 2 ), 0 )
81
- (right , lower ) = (
82
- int (img_resized .width / 2 + crop_w / 2 ),
83
- img_resized .height ,
84
- )
85
-
86
- img_cropped = img_resized .crop ((left , upper , right , lower ))
87
- img_cropped .save (path , "JPEG" )
88
- else :
89
- img_resized = img_resized .resize ((crop_w , img_resized .height ))
90
- img_resized .save (path , "JPEG" )
94
+ image = Image .open (path )
95
+ image = crop_image (image , 270 , 400 )
96
+ image .save (path , "JPEG" )
91
97
98
+ def process_cd_cover (path : str ):
99
+ cover = Image .open (path )
100
+ cover = crop_image (cover , 270 , 270 )
101
+
102
+ background = Image .open (path )
103
+ background = crop_image (background , 270 , 400 )
104
+ background = background .filter (ImageFilter .GaussianBlur (8 ))
105
+
106
+ background .paste (
107
+ cover , (0 , int ((background .height - cover .height ) / 2 ))
108
+ )
109
+ background .save (path , "JPEG" )
110
+
111
+
92
112
def goodreads_search (query : str ):
93
113
res = requests .get (
94
114
"https://www.goodreads.com/book/auto_complete" , params = {'format' : 'json' , 'q' : query }
@@ -126,4 +146,4 @@ def download_image(url: str, path: str):
126
146
f .write (chunk )
127
147
return 1
128
148
else :
129
- return 0
149
+ return 0
0 commit comments