Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python在HTML中调整图像大小-如何正确调整?_Python_Html_Css - Fatal编程技术网

使用Python在HTML中调整图像大小-如何正确调整?

使用Python在HTML中调整图像大小-如何正确调整?,python,html,css,Python,Html,Css,我在wordpress的帖子中嵌入了10张图片。我采用了html,这样我就可以用260px的宽度(高度应该相应地缩放)替换它们最初的宽度。我还想将它们全部对齐到右边,换行文本 我所做的是将一些html复制到一个文本文件: <img class="alignright wp-image-3087" src="https://wordpress-346062-1147012.cloudwaysapps.com/wp-content/uploads/2020/02/Signature-Desig

我在wordpress的帖子中嵌入了10张图片。我采用了html,这样我就可以用260px的宽度(高度应该相应地缩放)替换它们最初的宽度。我还想将它们全部对齐到右边,换行文本

我所做的是将一些html复制到一个文本文件:

<img class="alignright wp-image-3087" src="https://wordpress-346062-1147012.cloudwaysapps.com/wp-content/uploads/2020/02/Signature-Design-By-Ashley-coffee-table-300x159.jpg" alt="Signature Design By Ashley coffee table" width="300" height="159" />

解决了!我用了漂亮的苏打和re

from bs4 import BeautifulSoup
import re

infile = raw_input("Enter file name: ")
outfile = "resized_image_%s"%(infile)
outfile2 = 'resized_image_2_%s'%(infile)

f1 = open(infile)

for line in f1:
    if any(re.findall(r'<img', str(line), re.IGNORECASE)) == True:
        soup = BeautifulSoup(line)
        img = soup.img
        old_width = float(img['width'])
        old_height = float(img['height'])
        img['height'] = int(260*(old_height/old_width))
        img['width'] = 260
        with open(outfile,'a') as o:
            o.write(unicode(soup))
    else:
        with open(outfile,'a') as o:
            o.write(unicode(line))
f1.close()


f2 = open(outfile)
f3 = open(outfile2,"w+")
for line in f2:
    if any(re.findall(r'<img', str(line), re.IGNORECASE)) == True:
        y = re.sub(r'alignnone','alignright',line)
        f3.write(unicode(y))
    else:
        f3.write(unicode(line))
f2.close()
f3.close()
从bs4导入美化组
进口稀土
infle=原始输入(“输入文件名:”)
outfile=“已调整大小的图像大小为%s”%(填充)
outfile2='调整大小的\u图像\u 2\u%s'(填充)
f1=打开(填充)
对于f1中的行:
如果有(关于findall(r'
from bs4 import BeautifulSoup
import re

infile = raw_input("Enter file name: ")
outfile = "resized_image_%s"%(infile)
outfile2 = 'resized_image_2_%s'%(infile)

f1 = open(infile)

for line in f1:
    if any(re.findall(r'<img', str(line), re.IGNORECASE)) == True:
        soup = BeautifulSoup(line)
        img = soup.img
        old_width = float(img['width'])
        old_height = float(img['height'])
        img['height'] = int(260*(old_height/old_width))
        img['width'] = 260
        with open(outfile,'a') as o:
            o.write(unicode(soup))
    else:
        with open(outfile,'a') as o:
            o.write(unicode(line))
f1.close()


f2 = open(outfile)
f3 = open(outfile2,"w+")
for line in f2:
    if any(re.findall(r'<img', str(line), re.IGNORECASE)) == True:
        y = re.sub(r'alignnone','alignright',line)
        f3.write(unicode(y))
    else:
        f3.write(unicode(line))
f2.close()
f3.close()