(self, ctx: PipelineContext)
| 214 | ratio_threshold = 0.01 |
| 215 | |
| 216 | def process(self, ctx: PipelineContext): |
| 217 | buffer = ctx.get_buffer() |
| 218 | if not buffer: |
| 219 | return |
| 220 | real_ratio = 1. * int(ctx.get_exif().get('ImageWidth')) / int(ctx.get_exif().get('ImageHeight')) |
| 221 | if 'ratio' in ctx and MarginWithRatioFilter.ratio_pattern.match(ctx.get("ratio")): |
| 222 | ratio_w, ratio_h = ctx.get("ratio").split(':') |
| 223 | real_ratio = 1. * float(ratio_w) / float(ratio_h) |
| 224 | img = buffer[0] |
| 225 | cur_ratio = 1. * img.width / img.height |
| 226 | if cur_ratio - real_ratio > MarginWithRatioFilter.ratio_threshold: |
| 227 | # 图片太宽, 增加高度 |
| 228 | new_h = int(img.width / real_ratio) |
| 229 | pad_vertical = new_h - img.height |
| 230 | ctx.set('top_margin', pad_vertical / 2) |
| 231 | ctx.set('bottom_margin', pad_vertical - pad_vertical / 2) |
| 232 | elif cur_ratio - real_ratio < MarginWithRatioFilter.ratio_threshold: |
| 233 | # 图片太窄, 增加宽度 |
| 234 | new_w = int(img.height * real_ratio) |
| 235 | pad_horizontal = new_w - img.width |
| 236 | ctx.set('left_margin', pad_horizontal / 2) |
| 237 | ctx.set('right_margin', pad_horizontal - pad_horizontal / 2) |
| 238 | else: |
| 239 | return |
| 240 | MarginFilter().process(ctx) |
| 241 | ctx.save_buffer(self.name()).success() |
| 242 | |
| 243 | def name(self) -> str: |
| 244 | return "margin_with_ratio" |
nothing calls this directly
no test coverage detected