当前位置:网站首页>Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
2022-07-07 08:55:00 【Sugarcane causes bitterness】
List of articles
One 、 Source of problem
When I write blog everyday , use first typora edit markdown file , And then in CSDN Of markdown Import... In the editor .md file . But use typora We all know that ,typora When zooming pictures in zoom attribute , However CSDN This property is not supported . Therefore, it is used in import typora Written .md When you file , stay CSDN When I watch it on, the picture is the first , And will not automatically center , So very uncomfortable .
Two 、 Solution
Here I use python Regular matching is used to replace the corresponding url Content . After testing , Only need to .md File into the project project , function python After script , Put the new .md File copy to CSDN Then you can . The specific scheme is introduced below :
( One ) Create an engineering project
Use pycharm Create an engineering project , Create two folders under the project folder in and out.in Folders store pending .md file ,out The folder is the output folder :
Of course , If you're using vscode Or other idle, Similar , The configuration is not complicated .
( Two ) Code
Write code , Here I attach my own code , For reference only :
import re
file_name = "Unity The core 8—— Model import .md" # The file name to be converted
default_scale = 50 # The default zoom percentage of the picture
zoom_scale = 0.5 # Adjust the original as a whole typora Medium zoom Property size
# Watermark related parameters
watermark = "x-oss-process=image/watermark" # Add a picture or text watermark to the picture
type = "ZHJvaWRzYW5zZmFsbGJhY2s" # Text watermark font (Base64 code )
shadow = "50" # Specifies the shadow transparency of the text watermark
text = "Q1NETiBAemhlbGlrdQ==" # Text watermark content (Base64 code )
size = "20" # Text watermark size
color = "FFFFFF" # Text watermark color
t = "70" # Transparency of watermark image or watermark text
g = "se" # Watermark location
if_watermark = True # Whether to add watermark to the picture
def generate_watermark(): # The watermark is generated according to the above parameters
return f"?{
watermark},type_{
type},shadow_{
shadow},text_{
text},size_{
size},color_{
color},t_{
t},g_{
g}"
def alter(s): # Deal with pictures of unmodified size
return '<img src="' + s.group(2) + (
generate_watermark() if if_watermark else "") + f'#pic_center" width="{
default_scale}%">'
def alter_zoom(s): # Handle typora of use zoom Attribute to the image with modified size
scale = zoom_scale * int(s.group(3))
return s.group(1) + (generate_watermark() if if_watermark else "") + "#pic_center" + s.group(
2) + f'width="{
scale}%"' + s.group(4)
with open("./in/" + file_name, "r", encoding="utf-8") as fin, open("./out/" + file_name, "w", encoding="utf-8") as fout:
pattern = re.compile(r"(!\[image-\d+]\()(.+)(\))") # Matching rules for pictures with unmodified size
pattern_zoom = re.compile(r'(<img src=".*)(" .* )style="zoom:\s?(\d+)%;"(\s?/>)') # with zoom The matching rule of the image of attribute
content = fin.read() # Read file contents
content1 = pattern.subn(alter, content) # First processing
content2 = pattern_zoom.subn(alter_zoom, content1[0]) # Second treatment
fout.write(content2[0]) # Write new file
print(" Number of original image replacements :", content1[1])
print("zoom Attribute image replacement times :", content2[1])
print(" Total number of replacements :", content1[1] + content2[1])
About watermark parameters , What is not clear can be referred to This article
Here I divide the pictures into two kinds , One is to copy directly into typora Of , namely ![…](…) This type of link image . The other is in typora Middle right click the modified size with zoom Attribute link image .
- For the first picture , The default zoom size after replacement is default_scale%;
- For the second , Originally, I wanted to keep the original zoom Property value , Directly replace with width attribute . Later, I thought about setting a zoom ratio zoom_scale. namely , The original picture size is zoom: 50% Pictures of the , After treatment, it becomes width: 50 * zoom_scale%
Of course, my picture link here is not local , I set up a cloud chart bed . If your picture link is a local link ( namely , Used assets Folder ), So copy to CSDN The conversion may fail . I haven't tried local links here , Attach here typora The configuration tutorial of drawing bed
When the configuration is complete , take .md File put in in Folder , modify file_name, After running the script , Put... Directly out Newly generated in folder .md File copy to CSDN Of markdown The editor can . Just fine tune the size of the corresponding image . You can also modify the code and adjust the parameters by yourself ~
When uploading today , Discovery cannot be used directly zoom Property substitution width Percentage attribute of , The two properties are still different . Therefore, on the basis of the original code, the function of obtaining the image size is added , Set the picture size to a fixed value , The value is Original size * zoom Property value :
import io
import re
import urllib.request
from PIL import Image
file_name = "Unity The core 8—— Model import .md" # The file name to be converted
scale = 0.6 # The overall zoom ratio of the picture
# Watermark related parameters
watermark = "x-oss-process=image/watermark" # Add a picture or text watermark to the picture
type = "ZHJvaWRzYW5zZmFsbGJhY2s" # Text watermark font (Base64 code )
shadow = "50" # Specifies the shadow transparency of the text watermark
text = "Q1NETiBAemhlbGlrdQ==" # Text watermark content (Base64 code )
size = "20" # Text watermark size
color = "FFFFFF" # Text watermark color
t = "70" # Transparency of watermark image or watermark text
g = "se" # Watermark location
if_watermark = True # Whether to add watermark to the picture
def generate_watermark(): # The watermark is generated according to the above parameters
return f"?{
watermark},type_{
type},shadow_{
shadow},text_{
text},size_{
size},color_{
color},t_{
t},g_{
g}"
def get_size(img_path): # Get the size of the picture according to the picture link
response = urllib.request.urlopen(img_path)
temp_img = io.BytesIO(response.read())
img = Image.open(temp_img)
return img.size
def alter(s): # Deal with pictures of unmodified size
w, h = get_size(s.group(2))
new_w, new_h = int(scale * w), int(scale * h)
return '<img src="' + s.group(2) + (
generate_watermark() if if_watermark else "") + f'#pic_center" width="{
new_w}" height="{
new_h}">'
def alter_zoom(s): # Handle typora of use zoom Attribute to the image with modified size
zoom_value = int(s.group(4)) / 100
w, h = get_size(s.group(2))
new_w, new_h = int(zoom_value * w), int(zoom_value * h)
return s.group(1) + s.group(2) + (generate_watermark() if if_watermark else "") + "#pic_center" + s.group(
3) + f'width="{
new_w}" height="{
new_h}"' + s.group(5)
with open("./in/" + file_name, "r", encoding="utf-8") as fin, open("./out/" + file_name, "w", encoding="utf-8") as fout:
pattern = re.compile(r"(!\[image-\d+]\()(.+)(\))") # Matching rules for pictures with unmodified size
pattern_zoom = re.compile(r'(<img src=")(.*)(" .* )style="zoom:\s?(\d+)%;"(\s?/>)') # with zoom The matching rule of the image of attribute
content = fin.read() # Read file contents
content1 = pattern.subn(alter, content) # First processing
content2 = pattern_zoom.subn(alter_zoom, content1[0]) # Second treatment
fout.write(content2[0]) # Write new file
print(" Number of original image replacements :", content1[1])
print("zoom Attribute image replacement times :", content2[1])
print(" Total number of replacements :", content1[1] + content2[1])
边栏推荐
- oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
- Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
- Category of IP address
- 平台化,强链补链的一个支点
- UnityShader入门精要个人总结--基础篇(一)
- Shell script for changing the current folder and the file date under the folder
- NCS Chengdu New Electric interview Experience
- Several common database connection methods
- Required String parameter ‘XXX‘ is not present
- PPT模板、素材下载网站(纯干货,建议收藏)
猜你喜欢
数字三角形模型 AcWing 275. 传纸条
Three series of BOM elements
Platformization, a fulcrum of strong chain complementing chain
About using CDN based on Kangle and EP panel
Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical
Greenplum 6.x monitoring software setup
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
Data analysis methodology and previous experience summary 2 [notes dry goods]
ncs成都新電面試經驗
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
随机推荐
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
JS的操作
FPGA knowledge accumulation [6]
[南京大学]-[软件分析]课程学习笔记(一)-introduction
Greenplum 6.x build_ Environment configuration
Platformization, a fulcrum of strong chain complementing chain
Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and
MySQL主从延迟的解决方案
Digital triangle model acwing 1027 Grid access
Data analysis methodology and previous experience summary 2 [notes dry goods]
Quick sorting (detailed illustration of single way, double way, three way)
[step on the pit] Nacos registration has been connected to localhost:8848, no available server
Mock.js用法详解
Frequently Asked Coding Problems
Why choose cloud native database
How to realize sliding operation component in fast application
Greenplum6.x常用语句
Greenplum 6.x build_ install
Pointer advanced, string function