当前位置:网站首页>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])
边栏推荐
- 9c09730c0eea36d495c3ff6efe3708d8
- Redis fault handling "can't save in background: fork: cannot allocate memory“
- 注解@ConfigurationProperties的三种使用场景
- C language for calculating the product of two matrices
- 为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
- Image segmentation in opencv
- Newly found yii2 excel processing plug-in
- 硬核分享:硬件工程师常用工具包
- [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- GoLand set goproxy
猜你喜欢
Image segmentation in opencv
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
[Nanjing University] - [software analysis] course learning notes (I) -introduction
Calf problem
Digital triangle model acwing 1027 Grid access
面板显示技术:LCD与OLED
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
A bug using module project in idea
【Istio Network CRD VirtualService、Envoyfilter】
About using CDN based on Kangle and EP panel
随机推荐
C language for calculating the product of two matrices
Output a spiral matrix C language
let const
Digital triangle model acwing 1027 Grid access
数据分片介绍
数据分析方法论与前人经验总结2【笔记干货】
Other 7 features of TCP [sliding window mechanism ▲]
如何在图片的目标中添加目标的mask
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Greenplum 6.x reinitialization
leetcode134. gas station
NCS Chengdu Xindian interview experience
Greenplum 6.x monitoring software setup
2022-07-06 Unity核心9——3D动画
JS的操作
Find the original code, inverse code and complement of signed numbers [C language]
xray的简单使用
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量