是否有一个简单的解决方案来修剪PIL中的图像空白?
ImageMagick通过以下方式轻松支持它:
convert test.jpeg -fuzz 7% -trim test_trimmed.jpeg
Run Code Online (Sandbox Code Playgroud)
我找到了PIL的解决方案:
from PIL import Image, ImageChops
def trim(im, border):
bg = Image.new(im.mode, im.size, border)
diff = ImageChops.difference(im, bg)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
Run Code Online (Sandbox Code Playgroud)
但是这个解决方案有缺点:
border颜色,这对我来说不是什么大问题,我的图像有白色背景-fuzz密钥.添加一些模糊裁剪.因为我可以有一些jpeg压缩工件和不需要的巨大阴影.可能PIL有一些内置的功能吗?还是有一些快速解决方案?
这听起来有点奇怪,但我需要将Pandas控制台输出字符串保存到png图片.例如:
>>> df
sales net_pft ROE ROIC
STK_ID RPT_Date
600809 20120331 22.1401 4.9253 0.1651 0.6656
20120630 38.1565 7.8684 0.2567 1.0385
20120930 52.5098 12.4338 0.3587 1.2867
20121231 64.7876 13.2731 0.3736 1.2205
20130331 27.9517 7.5182 0.1745 0.3723
20130630 40.6460 9.8572 0.2560 0.4290
20130930 53.0501 11.8605 0.2927 0.4369
Run Code Online (Sandbox Code Playgroud)
有没有什么方法df.output_as_png(filename='df_data.png')可以生成一个只显示上面内容的图片文件?
是否可以将Pandas数据框导出为图像文件?像df.to_png()或的东西df.to_table().savefig('table.png').
目前我使用导出数据帧df.to_csv().然后,我在Excel中打开此csv文件以使数据看起来漂亮,然后将Excel表格复制/粘贴到Powerpoint中作为图像.我看到matplotlib有一个.table()方法,但我无法让它与我的df一起工作.
我正在使用的数据框有5列5行,每个'单元'是一个数字.
我想在Python中将以下HTML转换为PNG图像.
<html>
<b>Bold text</b>
</html>
Run Code Online (Sandbox Code Playgroud)
当然,这个HTML就是一个例子.
我尝试了'pisa',但它将html转换为PDF,而不是图像.我可以将HTML转换为PDF然后将PDF转换为PNG,但我想知道是否有任何直接解决方案(即HTML到PNG).任何内置或外置模块都可以很好地工作.
如果这可以在Graphicsmagick或Imagemagick中完成,那么它将是完美的.
这似乎是一个无用的功能,但它对我非常有帮助.我想保存Canopy IDE中的输出.我不认为这是Canopy特有的,但为了清楚起见,这就是我使用的.例如,我的控制台Out [2]是我想要的:

我认为格式化非常好并且每次重现这一点而不仅仅是保存输出将是浪费时间.所以我的问题是,我怎样才能掌握这个数字呢?理想情况下,实施方式与标准方法类似,因此可以这样做:
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('Output.pdf')
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
df.plot(how='table')
pp.savefig()
pp.close()
Run Code Online (Sandbox Code Playgroud)
注意:我意识到之前已经问过一个非常类似的问题(如何将Pandas数据帧/系列数据保存为数字?)但它从未收到答案,我想我已经更清楚地说明了这个问题.
在jupyter笔记本中运行时,下面的代码呈现一个颜色渐变格式的表,我想将其导出到图像文件.
笔记本呈现的结果"styled_table"对象是pandas.io.formats.style.Styler类型.
我一直无法找到将Styler导出到图像的方法.
我希望有人可以分享一个出口的工作示例,或者给我一些指示.
import pandas as pd
import seaborn as sns
data = {('count', 's25'):
{('2017-08-11', 'Friday'): 88.0,
('2017-08-12', 'Saturday'): 90.0,
('2017-08-13', 'Sunday'): 93.0},
('count', 's67'):
{('2017-08-11', 'Friday'): 404.0,
('2017-08-12', 'Saturday'): 413.0,
('2017-08-13', 'Sunday'): 422.0},
('count', 's74'):
{('2017-08-11', 'Friday'): 203.0,
('2017-08-12', 'Saturday'): 227.0,
('2017-08-13', 'Sunday'): 265.0},
('count', 's79'):
{('2017-08-11', 'Friday'): 53.0,
('2017-08-12', 'Saturday'): 53.0,
('2017-08-13', 'Sunday'): 53.0}}
table = pd.DataFrame.from_dict(data)
table.sort_index(ascending=False, inplace=True)
cm = sns.light_palette("seagreen", as_cmap=True)
styled_table = table.style.background_gradient(cmap=cm)
styled_table
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用matplotlib创建一个表,我已经设法获取我的数据,但我正在努力进行最终格式化.我需要编辑图形的大小以包含我的所有数据,因为有些内容正在被删除.这是我目前的代码:
for struct, energy, density in clust_data:
fig=plt.figure()
ax = plt.gca()
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
colLabels=("Structure", "Energy", "Density")
rows=len(clust_data)
cellText=[]
for row in clust_data:
cellText.append(row)
the_table = ax.table(cellText=cellText,
colLabels=colLabels,
loc='center')
plt.savefig("table.png")
Run Code Online (Sandbox Code Playgroud)
这会创建一个像这样的表(我不完全确定如何通过某些行来获取行):

任何帮助是极大的赞赏!
我在一周前开始学习python,并希望编写一个小程序,将电子邮件转换为图像(.png),以便可以在论坛上共享它,而不会冒很多垃圾邮件的风险.
似乎python标准库不包含可以做到这一点的模块,但我发现它有一个PIL模块(PIL.ImageDraw).
我的问题是我似乎无法让它发挥作用.
基本上我的问题是:
谢谢你的帮助 :)
现行代码:
import Image
import ImageDraw
import ImageFont
def getSize(txt, font):
testImg = Image.new('RGB', (1, 1))
testDraw = ImageDraw.Draw(testImg)
return testDraw.textsize(txt, font)
if __name__ == '__main__':
fontname = "Arial.ttf"
fontsize = 11
text = "example@gmail.com"
colorText = "black"
colorOutline = "red"
colorBackground = "white"
font = ImageFont.truetype(fontname, fontsize)
width, height = getSize(text, font)
img = Image.new('RGB', (width+4, height+4), colorBackground)
d = ImageDraw.Draw(img)
d.text((2, height/2), text, fill=colorText, font=font)
d.rectangle((0, 0, width+3, height+3), outline=colorOutline) …Run Code Online (Sandbox Code Playgroud) 我编写了读取两个字符串的代码,然后将它们比较相似的单词。然后产生一个带有数据的表格。
我的问题是它不断分裂为两个。我需要对此进行纠正,以便能够将其合并到HTML中。我将不胜感激,在此先感谢您!:)
我也尝试只打印第一行。
完整代码:
import string
from os import path
import pandas as pd
pd.set_option('display.max_columns', None) #prevents trailing elipses
pd.set_option('display.max_rows', None)
import os.path
BASE = os.path.dirname(os.path.abspath(__file__))
file1 = open(os.path.join(BASE, "samp.txt"))
sampInput=file1.read().replace('\n', '')
file2 = open(os.path.join(BASE, "ref.txt"))
refInput=file2.read().replace('\n', '')
sampArray = [word.strip(string.punctuation) for word in sampInput.split()]
refArray = [word.strip(string.punctuation) for word in refInput.split()]
out=pd.DataFrame(index=sampArray,columns=refArray)
for i in range(0, out.shape[0]): #from 0 to total number of rows
for word in refArray: #for each word in the samplearray
df1 = out.iloc[0, 0:16].copy()
top …Run Code Online (Sandbox Code Playgroud) 我需要在 Python 中将一些 HTML 转换为图像,并且我正在使用WeasyPrint。我希望图像大小能够适应内容。
使用以下命令时,我得到的图像比内容大得多(A4?):
# !pip install weasyprint
import weasyprint as wsp
img_filepath = 'test.png'
html_source = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ciao!</title>
</head>
<body>
Hello World!
</body>
</html>
'''
html = wsp.HTML(string=html_source)
html.write_png(img_filepath)
Run Code Online (Sandbox Code Playgroud)
我尝试使用CSS的@page size指令成功修改图像大小:
# !pip install weasyprint
import weasyprint as wsp
img_filepath = 'test2.png'
css = wsp.CSS(string='@page { size: 400px; }')
html_source = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ciao!</title>
</head>
<body>
Hello World!
</body> …Run Code Online (Sandbox Code Playgroud) python ×10
pandas ×5
matplotlib ×3
html ×2
image ×2
imagemagick ×2
canopy ×1
css ×1
dataframe ×1
django ×1
export ×1
ipython ×1
python-3.7 ×1
seaborn ×1
weasyprint ×1