# coding: utf-8 # Copyright (C) 2022, [Breezedeus](https://github.com/breezedeus). # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. import os from PIL import Image, ImageFilter import streamlit as st from cnocr import CnOcr from antiocr import AntiOcr, BG_IMAGE_FP, FONT_NAMES, set_logger, download_font logger = set_logger() st.set_page_config(layout="wide") FONT_LOCAL_DIR = 'fonts' @st.cache(allow_output_mutation=True) def get_ocr_model(): return CnOcr() def save_image(img): from io import BytesIO buf = BytesIO() img.save(buf, format="JPEG") byte_im = buf.getvalue() st.download_button( label="下载图片", data=byte_im, file_name="antiOCR.jpeg", mime="image/jpeg", ) def main(): st.sidebar.header('输出设置') with st.spinner('Downloading fonts ...'): for fnt_fp in FONT_NAMES: download_font(os.path.join(FONT_LOCAL_DIR, fnt_fp)) font_fn = st.sidebar.selectbox('选择字体', FONT_NAMES, index=0) font_fp = os.path.join(FONT_LOCAL_DIR, font_fn) char_reverse_ratio = st.sidebar.slider( '文字倒转概率', min_value=0.0, max_value=1.0, value=0.1 ) char_to_pinyin_ratio = st.sidebar.slider( '文字转拼音概率', min_value=0.0, max_value=1.0, value=0.1 ) cols = st.sidebar.columns(2) min_font_size = int(cols[0].number_input('最小文字大小', 2, 80, value=15)) max_font_size = int( cols[1].number_input( '最大文字大小', min_font_size + 1, 120, value=max(40, min_font_size + 1) ) ) text_color = st.sidebar.color_picker('文字颜色', value='#5087DC') st.sidebar.markdown('----') use_random_bg = st.sidebar.checkbox('随机生成背景图片') if use_random_bg: bg_text_density = st.sidebar.slider( '背景图片文字密度', min_value=0.0, max_value=3.0, value=1.0 ) cols = st.sidebar.columns(2) bg_min_size = int( cols[0].number_input('背景图片最小文字', 2, 80, key='bg_min', value=15) ) bg_max_size = int( cols[1].number_input( '背景图片最大文字', bg_min_size + 1, 120, key='bg_max', value=max(70, bg_min_size + 1), ) ) bg_text_color = st.sidebar.color_picker('背景图片文字颜色', value='#07BCE0') bg_gen_config = dict( text_density=bg_text_density, text_color=bg_text_color, min_font_size=bg_min_size, max_font_size=bg_max_size, ) bg_image = None else: bg_gen_config = None bg_image = Image.open(BG_IMAGE_FP) bg_image = bg_image.filter(ImageFilter.MaxFilter(3)) title = '让文字自由传播:antiOCR' st.markdown(f"