简体   繁体   中英

generate image (e.g. jpg) of a web page?

I want to create an image what a web page looks like, eg create a small thumbnail of the html + images. it does not have to be perfect (eg flash /javascript rendering).

I will call use the code on linux, ideally would be some java library, but a command line tool would be cool as well.

any ideas?

Try CutyCapt , a command-line utility. It uses Webkit for rendering and outputs in various formats (SVG, PNG, etc.).

To take a screenshot in the terminal with ImageMagick, type the following line into a terminal and then click-and-drag the mouse over a section of the screen:

import MyScreenshot.png

To capture the entire screen and after some delay and resize it, use the following command:

import -window root -resize 400×300 -delay 200 screenshot.png 

You may use a mixture of xwininfo and import to retrieve the window id of the browser and make a screenshot of that window. A bash script to automate this process would be something like this:

#!/bin/bash
window_id=`xwininfo -tree -root | grep Mozilla | awk '{print $1}'`
import -window $window_id -resize 100x100 tumb.png

This script will create a 100x100 screenshot of Firefox on the current directory under the name tumb.png

Several sources show how to run a bash script from inside a Java application, google can help you on that. If you are in a hurry, check this and this .

If you're interested in Java, maybe you could look at browser automation using Selenium-RC http://seleniumhq.com

It's a little java server that you can install on the box and the program itself will execute remote commands in a web browser.

Steps like (this is pseudo code by the way, I code my Selenium in php and I can't recall 100% of the specifics off the top of my head)

selenium.location("http://foo.com")
selenium.open("/folder/sub/bar.html")
selenium.captureScreenshot("/tmp/" + this.getClass().getName() + "."
                               + testMethodName + ".png");

Actually, I just did a quick websearch for the exact syntax on that last one ... and this guy has a blog with what might actually be working code in java :) https://dev.youdevise.com/YDBlog/index.php?title=capture_screenshots_of_selenium_browser_&more=1&c=1&tb=1&pb=1

There's also a number of websites that provide this service "cross browser and OS" I just can't recall what they are. Basically they've got a cloud of every single operating system and browser combination, and they log on with each machine, take a screen and store it on their site for you to come back to in a few hours when they're done.

Ahh... another websearch and it's yours :) http://browsershots.org/

After reading this page, I was thinking, let me fire up midori browser: http://midori-browser.org/ and when I tried the -h option, I have seen:

  -s, --snapshot      Take a snapshot of the specified URI

QutyCapt is difficult to compile, and has many dependencies. Midori has it less. It outputs the PNG of the website into TMP folder. One can get the file with:

midori -s http://www.rcdwealth.com new.png 2>/dev/null | awk '{ print $4}'

After that, the file can be converted to thumbnail size by using ImageMagick's convert program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM