Crie capturas de tela perfeitas de sites ou converta HTML diretamente em imagens usando os seguintes recursos de API Python do GrabzIt. No entanto, antes de começar, lembre-se de que depois de ligar para o URLToImage, HTMLToImage or FileToImage métodos os Save or SaveTo O método deve ser chamado para fazer a captura de tela.
Apenas um parâmetro é necessário para tirar uma captura de tela de uma página da web ou converter HTML intoa imagem como mostrado no exemplo a seguir.
grabzIt.URLToImage("https://www.tesla.com") # Then call the Save or SaveTo method
grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>") # Then call the Save or SaveTo method
grabzIt.FileToImage("example.html") # Then call the Save or SaveTo method
A API Python do GrabzIt pode capturar capturas de tela de imagem em vários formatos, incluindo JPG, PNG, WEBP, BMP (bits 8, bits 16, bits 24 ou bits 32) e TIFF. O formato padrão para capturas de tela de imagens é JPG. No entanto, a qualidade de uma imagem JPG pode não ser boa o suficiente para alguns aplicativos nessas circunstâncias. O formato PNG é recomendado para capturas de tela de imagem, pois oferece um bom equilíbrio entre qualidade e tamanho do arquivo. O exemplo abaixo mostra uma captura de tela da imagem sendo usada no formato PNG.
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.format = "png" grabzIt.URLToImage("https://www.tesla.com", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.png")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.format = "png" grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.png")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.format = "png" grabzIt.FileToImage("example.html", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.png")
O tamanho do navegador se refere ao tamanho da janela do navegador que será usada ao capturar a captura de tela na maioria dos casos, isso não precisa ser definido, pois o tamanho padrão do navegador será suficiente para quase todas as tarefas. Para usar o tamanho padrão do navegador, basta passar 0
ao browserWidth
e browserHeight
atributos do GrabzItImageOptions classe.
É fácil alterar o tamanho de uma imagem, sem distorcer a imagem, é um pouco mais difícil. Para simplificar todo o processo, recomendamos que você use este calculadora de dimensão de imagem simples.
Se você deseja aumentar a largura e a altura da imagem para um tamanho maior que a largura e a altura do navegador, que por padrão é 1366 por pixels 728, a largura e a altura do navegador também devem ser aumentadas para corresponder.
Você pode passar um identificador personalizado para o imagem Como mostrado abaixo, esse valor é retornado ao seu manipulador GrabzIt Python. Por exemplo, esse identificador personalizado pode ser um identificador de banco de dados, permitindo que uma captura de tela seja associada a um registro específico do banco de dados.
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.customId = "123456" grabzIt.URLToImage("https://www.tesla.com", options) # Then call the Save method grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.customId = "123456" grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options) # Then call the Save method grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.customId = "123456" grabzIt.FileToImage("example.html", options) # Then call the Save method grabzIt.Save("http://www.example.com/handler.py")
O GrabzIt permite que você tire uma captura de tela completa de uma página da web inteira para fazer isso. Você precisa passar um -1 para o browserHeight
atributo. Para garantir que a imagem corresponda ao tamanho do navegador, passe -1 para o height
e width
atributos.
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.browserHeight = -1 options.width = -1 options.height = -1 grabzIt.URLToImage("https://www.tesla.com", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.browserHeight = -1 options.width = -1 options.height = -1 grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.browserHeight = -1 options.width = -1 options.height = -1 grabzIt.FileToImage("example.html", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")
Você também pode retornar miniaturas que não são cortadas, mas cuidado, isso pode criar imagens grandes. Para fazer isso, passe um -1 para o height
e / ou width
atributos. A dimensão que é passada por um -1 não será cortada.
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.width = -1 options.height = -1 grabzIt.URLToImage("https://www.tesla.com", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.width = -1 options.height = -1 grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzItImageOptions.GrabzItImageOptions() options.width = -1 options.height = -1 grabzIt.FileToImage("example.html", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")
O uso desses valores especiais significa que você pode criar uma captura de tela que seja uma versão em escala completa de toda a página da web, se desejar!
O GrabzIt permite tirar uma captura de tela de um elemento HTML, como um div
or span
tag e capture todo o seu conteúdo. Para fazer isso, o elemento HTML que você deseja capturar a tela deve ser especificado como um Seletor CSS.
... <div id="features"> <img src="http://www.example.com/race.jpg"/><h3>Car Race Tommorow</h3> </div> ...
Para o exemplo abaixo, selecionaremos a div com o ID "features" e a produziremos como uma imagem JPEG 250 x 250px.
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") # The 250 parameters indicates that image should be sized to 250 x 250 px options = GrabzItImageOptions.GrabzItImageOptions() options.width = 250 options.height = 250 options.format = "jpg" options.targetElement = "#features" grabzIt.URLToImage("http://www.bbc.co.uk/news", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")
O próximo exemplo tira outra captura de tela da div "features", mas desta vez gera uma imagem JPEG com o tamanho exato da div.
from GrabzIt import GrabzItImageOptions from GrabzIt import GrabzItClient grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret") # The -1 indicates that image should not be cropped options = GrabzItImageOptions.GrabzItImageOptions() options.width = 250 options.height = 250 options.browserHeight = -1 options.format = "jpg" options.targetElement = "#features" grabzIt.URLToImage("http://www.bbc.co.uk/news", options) # Then call the Save or SaveTo method grabzIt.SaveTo("result.jpg")