当前位置:网站首页>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])
边栏推荐
- Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical
- With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
- 数据分片介绍
- 【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
- MySQL主从延迟的解决方案
- Redis fault handling "can't save in background: fork: cannot allocate memory“
- 模拟卷Leetcode【普通】1706. 球会落何处
- 阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
- IP地址的类别
- [MySQL] detailed explanation of trigger content of database advanced
猜你喜欢
Introduction to data fragmentation
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
Routing information protocol rip
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
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
Greenplum 6.x build_ Environment configuration
面板显示技术:LCD与OLED
【踩坑】nacos注册一直连接localhost:8848,no available server
Digital triangle model acwing 275 Pass a note
FPGA knowledge accumulation [6]
随机推荐
Frequently Asked Coding Problems
Skills that testers must know: Selenium's three waiting ways are interpreted clearly
QT charts use (rewrite qchartview to realize some custom functions)
MySQL主从延迟的解决方案
【踩坑】nacos注册一直连接localhost:8848,no available server
Frequently Asked Coding Problems
Why choose cloud native database
IP地址的类别
opencv 将16位图像数据转为8位、8转16
[Yugong series] February 2022 U3D full stack class 005 unity engine view
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
Golang etcdv3 reports an error. The attribute in grpc does not exist
Gson转换实体类为json时报declares multiple JSON fields named
LeetCode 736. LISP syntax parsing
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
Quick sorting (detailed illustration of single way, double way, three way)
Output all composite numbers between 6 and 1000
With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
【ChaosBlade:根据标签删除POD、Pod 域名访问异常场景、Pod 文件系统 I/O 故障场景】