在 python 中调整图像大小,使其免于拉伸
resize image in python so it can be saved from being stretch
有没有人知道我可以用来创建 CSS 属性 -> “object-fit:scale-down”的功能,如果图像以不同的宽高比出现,则在生成 pdf 时我试过只给一个地方固定或传递高度/重量 css 基于高度大于宽度 20% 然后将它移动到垂直矩形框如果宽度大于高度 20% 移动到水平框如果它是将其移至方框的差异低于 20%,但其中 none 有效且图像最终显示拉伸。如果可能的话,任何逻辑或解决方法都会有所帮助我想使用 PIL 并实现这一目标...
语言- python3
使用的库- jinja,xhtml2pdf, PIL
稍后将转换为 pdf 的示例 jinja 代码
<img src="{{path}}" style="height:{{height}};width:{{width}};" />
Python 代码已尝试
try:
#logo fixes for diffrent size of logo
im = Image.open('something')
width,height = im.size
#logic if logo is higher then 20% of width then it's vertically image if width is more then 20% of height then it's comes under horizontal catogory (20*width)/100 default is 2cm to 2cm for square image
if height+(20*width)/100>width and height!=width: #horizontal
data['width']='2cm'
data['height']='4cm'
elif width+(20*height)/100>height and height!=width: #vertical
data['width']='4cm'
data['height']='2cm'
else: #default
data['width']='2cm'
data['height']='2cm'
except Exception as imageerror:
data['width']='2cm'
data['height']='2cm'
我不能完全按照你的逻辑 30%
但这样的事情会起作用:
from PIL import Image
image = Image.open('./image.png')
width, height = image.size
if width * 1.3 > height:
new_height = width
new_width = width
else:
new_height = height
new_width = height
image.resize([new_width, new_height]).save('./new_image.png')
try:
image = Image.open('something')
image.thumbnail((700,700), Image.ANTIALIAS)
image.save('something','JPEG',quality=100) #replace existing file
height,width = image.size
except exception as e:
#Size extraction failed Print e
pass
这样我们可以为我的图像提供最大边界,PIL 将负责调整大小并将适合该边界的图像的高度和宽度传递给我...问题已解决。
有没有人知道我可以用来创建 CSS 属性 -> “object-fit:scale-down”的功能,如果图像以不同的宽高比出现,则在生成 pdf 时我试过只给一个地方固定或传递高度/重量 css 基于高度大于宽度 20% 然后将它移动到垂直矩形框如果宽度大于高度 20% 移动到水平框如果它是将其移至方框的差异低于 20%,但其中 none 有效且图像最终显示拉伸。如果可能的话,任何逻辑或解决方法都会有所帮助我想使用 PIL 并实现这一目标...
语言- python3
使用的库- jinja,xhtml2pdf, PIL
稍后将转换为 pdf 的示例 jinja 代码
<img src="{{path}}" style="height:{{height}};width:{{width}};" />
Python 代码已尝试
try:
#logo fixes for diffrent size of logo
im = Image.open('something')
width,height = im.size
#logic if logo is higher then 20% of width then it's vertically image if width is more then 20% of height then it's comes under horizontal catogory (20*width)/100 default is 2cm to 2cm for square image
if height+(20*width)/100>width and height!=width: #horizontal
data['width']='2cm'
data['height']='4cm'
elif width+(20*height)/100>height and height!=width: #vertical
data['width']='4cm'
data['height']='2cm'
else: #default
data['width']='2cm'
data['height']='2cm'
except Exception as imageerror:
data['width']='2cm'
data['height']='2cm'
我不能完全按照你的逻辑 30%
但这样的事情会起作用:
from PIL import Image
image = Image.open('./image.png')
width, height = image.size
if width * 1.3 > height:
new_height = width
new_width = width
else:
new_height = height
new_width = height
image.resize([new_width, new_height]).save('./new_image.png')
try:
image = Image.open('something')
image.thumbnail((700,700), Image.ANTIALIAS)
image.save('something','JPEG',quality=100) #replace existing file
height,width = image.size
except exception as e:
#Size extraction failed Print e
pass
这样我们可以为我的图像提供最大边界,PIL 将负责调整大小并将适合该边界的图像的高度和宽度传递给我...问题已解决。