廣告
请使用 http://www.luhui.net 網址高速訪問本站,歡迎關注微信公衆賬号:guanfu8 平台長期發布海量社科數據, 本站QQ交流群:84142868 探讨人文理想,共謀和諧世界!
黄金广告位招租,投放热线15900718107,13386070835 详情查看=》   鲁之虺:“  上网就是搜索,生活就是分类,科技服务大众,信息引领生活,地方分类站长,开启您的事业之旅。生活分类信息整体提供商:http://huike.luhui.net”。
×

鲁虺繁体中文社区

 找回密碼
 立即註冊
廣告

 

查看: 18|回復: 2

[❆JavaScript资源] JavaScript智能切图脚本

[複製鏈接]
發表於 2023-2-17 23:11:36 | 顯示全部樓層 |閱讀模式
廣告
搜索看看︰JavaScript智能切图脚本
廣告
JavaScript智能切图脚本分享:



图片可以让页面更有吸引力,尤其是处理的恰当的时候,但问题是,图片的处理是个麻烦事情,它的创建和裁剪对于一个普通人来说是个困难的任务,尤其是那些自己上传图片的用户,比如头像图片,logo图片,有可能是各式各样的图片格式,大小不一的图片尺寸。
最近发现了一个有趣的浏览器端智能处理图片的JavaScript脚本,smartcrop.js,它能智能的分析图片的内容,识别出图片的主体内容和人像、人脸,我可以用它轻松的将一个图片裁剪成合适大小,让人物、人脸或图景主体等最重要的部分突出,裁剪掉多余的部分。
观看演示
下面是一个样本,使用了两张Lady GaGa的照片,经过使用smartcrop.js处理后,使得两张图片非常适合做头像照片,图片中的头像放到了正中。
background-size-2-500x331.jpg
background-size-2-smartcrop.jpg
background-size-3-500x331.jpg
background-size-3-smartcrop.jpg
观看演示
这个js工具脚本的调用方法十分的简单,只需要几行代码就能生效。
SmartCrop.crop(image, {width: 100, height: 100}, function(result){console.log(result);});// {topCrop: {x: 300, y: 200, height: 200, width: 200}}详细的介绍请查看它的官方文档,也可以下载它的源代码理解它的工作原理。











廣告
廣告
 樓主| 發表於 2023-2-17 23:20:21 | 顯示全部樓層
廣告
廣告
About
Content aware image cropping
[color=var(--color-accent-fg)]29a.ch/2014/04/03/smartcrop-content-aware-image-cropping

Topics[color=var(--color-accent-fg)][backcolor=var(--color-accent-subtle)]nodejs
[color=var(--color-accent-fg)][backcolor=var(--color-accent-subtle)]javascript [color=var(--color-accent-fg)][backcolor=var(--color-accent-subtle)]image-processing [color=var(--color-accent-fg)][backcolor=var(--color-accent-subtle)]smartcrop

Resources[color=var(--color-fg-muted)  !important] Readme
License[color=var(--color-fg-muted)  !important] MIT license
Stars[color=var(--color-fg-muted)  !important] 12.6k stars
Watchers[color=var(--color-fg-muted)  !important] 218 watching
Forks[color=var(--color-fg-muted)  !important] 615 forks


[color=var(--color-fg-default)  !important]Releases
[color=var(--color-fg-default)][backcolor=var(--color-neutral-muted)]6
[color=var(--color-fg-default)  !important]v2.0.5[color=var(--color-success-fg)]Latest
[color=var(--color-fg-muted)  !important]on Sep 2, 2021

[color=var(--color-accent-fg)]+ 5 releases


[color=var(--color-fg-default)  !important]Packages[color=var(--color-fg-muted)  !important]No packages published



[color=var(--color-fg-default)  !important]Used by
[color=var(--color-fg-default)][backcolor=var(--color-neutral-muted)]389
[color=var(--color-accent-fg)]

+ 381


[color=var(--color-fg-default)  !important]Contributors
[color=var(--color-fg-default)][backcolor=var(--color-neutral-muted)]14
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
  • [color=var(--color-accent-fg)]
[color=var(--color-accent-fg)]+ 3 contributors


Languages[backcolor=var(--color-neutral-muted)]
  • [color=var(--color-fg-muted)  !important][color=var(--color-fg-default)  !important]JavaScript91.8%
  • [color=var(--color-fg-muted)  !important][color=var(--color-fg-default)  !important]HTML3.2%
  • [color=var(--color-fg-muted)  !important][color=var(--color-fg-default)  !important]Shell2.3%
  • [color=var(--color-fg-muted)  !important][color=var(--color-fg-default)  !important]Dockerfile2.3%
  • Other0.4%
    [color=var(--color-fg-default)  !important]README.md

    [color=var(--color-accent-fg)]smartcrop.js
    [color=var(--color-accent-fg)]
    Smartcrop.js implements an algorithm to find good crops for images. It can be used in the browser, in node or via a CLI.
    [color=var(--color-accent-fg)] Image: [color=var(--color-accent-fg)]https://www.flickr.com/photos/endogamia/5682480447/ by N. Feans
    [color=var(--color-accent-fg)  !important]Note I'm currently working on a more advanced version of smartcrop.js based on machine learning. As part of that I'm looking for a large dataset of manually cropped images. If you know of such a dataset, please [color=var(--color-accent-fg)]let me know.
    [color=var(--color-accent-fg)]Demos[color=var(--color-accent-fg)]Simple Example[color=var(--color-prettylights-syntax-comment)]// you pass in an image as well as the width & height of the crop you[color=var(--color-prettylights-syntax-comment)]// want to optimize.smartcrop.[color=var(--color-prettylights-syntax-entity)]crop(image, { [color=var(--color-prettylights-syntax-constant)]width: [color=var(--color-prettylights-syntax-constant)]100, [color=var(--color-prettylights-syntax-constant)]height: [color=var(--color-prettylights-syntax-constant)]100 }).[color=var(--color-prettylights-syntax-entity)]then([color=var(--color-prettylights-syntax-keyword)]function(result) {  [color=var(--color-prettylights-syntax-storage-modifier-import)]console.[color=var(--color-prettylights-syntax-entity)]log(result);});
    Output:
    [color=var(--color-prettylights-syntax-comment)]// smartcrop will output you its best guess for a crop[color=var(--color-prettylights-syntax-comment)]// you can now use this data to crop the image.{[color=var(--color-prettylights-syntax-constant)]topCrop: {[color=var(--color-prettylights-syntax-constant)]x: [color=var(--color-prettylights-syntax-constant)]300, [color=var(--color-prettylights-syntax-constant)]y: [color=var(--color-prettylights-syntax-constant)]200, [color=var(--color-prettylights-syntax-constant)]height: [color=var(--color-prettylights-syntax-constant)]200, [color=var(--color-prettylights-syntax-constant)]width: [color=var(--color-prettylights-syntax-constant)]200}}
    [color=var(--color-accent-fg)]Download/ Installation
    npm install smartcrop or just download [color=var(--color-accent-fg)]smartcrop.js from the git repository.
    Smarcrop requires support for [color=var(--color-accent-fg)]Promises, use a [color=var(--color-accent-fg)]polyfill for unsupported browsers or set smartcrop.Promise to your favorite promise implementation (I recommend [color=var(--color-accent-fg)]bluebird).
    [color=var(--color-accent-fg)]Consider avoiding crops using dont-crop
    If you are interested in using smartcrop.js to crop your images you should also consider to avoid cropping them by using [color=var(--color-accent-fg)]dont-crop. Dont-crop gives you matching gradients and colors to pad and complement your images.
    [color=var(--color-accent-fg)]
    [color=var(--color-accent-fg)]Command Line Interface
    The [color=var(--color-accent-fg)]smartcrop-cli offers command line interface to smartcrop.js.
    [color=var(--color-accent-fg)]Node
    You can use smartcrop from nodejs via either [color=var(--color-accent-fg)]smartcrop-gm (which is using image magick via gm) or [color=var(--color-accent-fg)]smartcrop-sharp (which is using libvips via sharp). The [color=var(--color-accent-fg)]smartcrop-cli can be used as an example of using smartcrop from node.
    [color=var(--color-accent-fg)]Stability
    While smartcrop.js is a small personal project it is currently being used on high traffic production sites. It has a basic set of automated tests and a test coverage of close to 100%. The tests run in all modern browsers thanks to [color=var(--color-accent-fg)]saucelabs. If in any doubt the code is short enough to perform a quick review yourself.
    [color=var(--color-accent-fg)]Algorithm Overview
    Smartcrop.js works using fairly dumb image processing. In short:
    • Find edges using laplace
    • Find regions with a color like skin
    • Find regions high in saturation
    • Boost regions as specified by options (for example detected faces)
    • Generate a set of candidate crops using a sliding window
    • Rank them using an importance function to focus the detail in the center and avoid it in the edges.
    • Output the candidate crop with the highest rank
    [color=var(--color-accent-fg)]Face detection
    The smartcrop algorithm itself is designed to be simple, relatively fast, small and generic.
    In many cases it does make sense to add face detection to it to ensure faces get the priority they deserve.
    There are multiple javascript libraries which can be easily integrated into smartcrop.js.
    You can experiment with all of these in the [color=var(--color-accent-fg)]smartcrop.js testbed
    On the client side I would recommend using tracking.js because it's small and simple. Opencv.js is compiled from c++ and very heavy (~7.6MB of javascript + 900kb of data). jquery.facedetection has dependency on jquery and from my limited experience seems to perform worse than the others.
    On the server side node-opencv can be quicker but comes with some [color=var(--color-accent-fg)]annoying issues as well.
    It's also worth noting that all of these libraries are based on the now dated [color=var(--color-accent-fg)]viola-jones object detection framework. It would be interesting to see how more [color=var(--color-accent-fg)]state of the art techniques could be implemented in browser friendly javascript.
    [color=var(--color-accent-fg)]Supported Module Formats
    • CommonJS
    • AMD
    • global export / window
    [color=var(--color-accent-fg)]Supported Browsers
    See [color=var(--color-accent-fg)]caniuse.com/canvas. A [color=var(--color-accent-fg)]polyfill for [color=var(--color-accent-fg)]Promises is recommended if you need to support old browsers.
    [color=var(--color-accent-fg)]API[color=var(--color-accent-fg)]smartcrop.crop(image, options)
    Find the best crop for image using options.
    image: anything ctx.drawImage() accepts, usually HTMLImageElement, HTMLCanvasElement or HTMLVideoElement.
    Keep in mind that [color=var(--color-accent-fg)]origin policies apply to the image source. You may not use cross-domain images without [color=var(--color-accent-fg)]CORS clearance.
    options: [color=var(--color-accent-fg)]cropOptions
    returns: A promise for a [color=var(--color-accent-fg)]cropResult.
    [color=var(--color-accent-fg)]cropOptions
    minScale: minimal scale of the crop rect, set to 1.0 to prevent smaller than necessary crops (lowers the risk of chopping things off).
    width: width of the crop you want to use.
    height: height of the crop you want to use.
    boost: optional array of regions whose 'interestingness' you want to boost (for example faces). See [color=var(--color-accent-fg)]boost;
    ruleOfThirds: optional boolean if set to false it will turn off the rule of thirds composition weight.
    debug (internal): if true, cropResults will contain a debugCanvas and the complete results array.
    There are many more (for now undocumented) options available. Check the [color=var(--color-accent-fg)]source and be advised that they might change in the future.
    [color=var(--color-accent-fg)]cropResult
    Result of the promise returned by smartcrop.crop.
    {  topCrop: crop;}
    [color=var(--color-accent-fg)]crop
    An individual crop.
    {  [color=var(--color-prettylights-syntax-constant)]x: [color=var(--color-prettylights-syntax-constant)]11, [color=var(--color-prettylights-syntax-comment)]// pixels from the left side  [color=var(--color-prettylights-syntax-constant)]y: [color=var(--color-prettylights-syntax-constant)]20, [color=var(--color-prettylights-syntax-comment)]// pixels from the top  [color=var(--color-prettylights-syntax-constant)]width: [color=var(--color-prettylights-syntax-constant)]1, [color=var(--color-prettylights-syntax-comment)]// pixels  [color=var(--color-prettylights-syntax-constant)]height: [color=var(--color-prettylights-syntax-constant)]1 [color=var(--color-prettylights-syntax-comment)]// pixels}
    [color=var(--color-accent-fg)]boost
    Describes a region to boost. A usage example of this is to take into account faces in the image. See [color=var(--color-accent-fg)]smartcrop-cli for an example on how to integrate face detection.
    {  [color=var(--color-prettylights-syntax-constant)]x: [color=var(--color-prettylights-syntax-constant)]11, [color=var(--color-prettylights-syntax-comment)]// pixels from the left side  [color=var(--color-prettylights-syntax-constant)]y: [color=var(--color-prettylights-syntax-constant)]20, [color=var(--color-prettylights-syntax-comment)]// pixels from the top  [color=var(--color-prettylights-syntax-constant)]width: [color=var(--color-prettylights-syntax-constant)]32, [color=var(--color-prettylights-syntax-comment)]// pixels  [color=var(--color-prettylights-syntax-constant)]height: [color=var(--color-prettylights-syntax-constant)]32, [color=var(--color-prettylights-syntax-comment)]// pixels  [color=var(--color-prettylights-syntax-constant)]weight: [color=var(--color-prettylights-syntax-constant)]1 [color=var(--color-prettylights-syntax-comment)]// in the range [0, 1]}
    Note that the impact the boost has is proportional to it's weight and area.
    [color=var(--color-accent-fg)]Tests
    You can run the tests using grunt test. Alternatively you can also just run grunt (the default task) and open [color=var(--color-accent-fg)]http://localhost:8000/test/.
    [color=var(--color-accent-fg)]Benchmark
    There are benchmarks for both the browser (test/benchmark.html) and node (node test/benchmark-node.js [requires node-canvas]) both powered by [color=var(--color-accent-fg)]benchmark.js.
    If you just want some rough numbers: It takes < 20 ms to find a square crop of a 640x427px picture on an i7. In other words, it's fine to run it on one image, it's suboptimal to run it on an entire gallery on page load.
    [color=var(--color-accent-fg)]Contributors[color=var(--color-accent-fg)]Ports, Alternatives[color=var(--color-accent-fg)]Version history[color=var(--color-accent-fg)]2.0.5
    Fix TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier..
    [color=var(--color-accent-fg)]2.0.4
    Typescript type definitions.
    [color=var(--color-accent-fg)]2.0.2
    In short: It's a lot faster when calculating bigger crops. The quality of the crops should be comparable but the results are going to be different so this will be a major release.
    [color=var(--color-accent-fg)]1.1.1
    Removed useless files from npm package.
    [color=var(--color-accent-fg)]1.1
    Creating github releases. Added options.input which is getting passed along to iop.open.
    [color=var(--color-accent-fg)]1.0
    Refactoring/cleanup to make it easier to use with node.js (dropping the node-canvas dependency) and enable support for boosts which can be used to do face detection. This is a 1.0 in the semantic meaning (denoting backwards incompatible API changes). It does not denote a finished product.
    [color=var(--color-accent-fg)]License
    Copyright (c) 2018 Jonas Wagner, licensed under the MIT License (enclosed)




廣告
廣告
 樓主| 發表於 2023-2-17 23:46:40 | 顯示全部樓層
廣告
廣告
https://github.com/jwagner/smart ... refs/heads/main.zip演示:JavaScript智能切图脚本
这样页面用来演示用户使用自己的图片测试smartcrop.js脚本的智能切图功能。更多的说明请浏览JavaScript智能切图脚本介绍
Width
250px
Height
250px
minScale
0.9

请把你的用来测试的图片拖拽到这个页面上,看看它自动识别的效果


http://www.luhui.net/JavaScript/JavaScript智能切图脚本jwagner smartcrop.js.rar - RAR 压缩文件, 解包大小为 5,293,031 字节




演示:JavaScript智能切图脚本.jpg
廣告
廣告
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

廣告

廣告
操作系统 Fuchsia OS  |Microsoft Windows  |Android  |Chrome OS  |Novell  |Linux  |Mac OS X  |Unix  |FreeBSD  | Ubuntu
廣告

QQ|關于我們|聯繫我們|合作共赢|免責聲明|隐私条款|Archiver|手機版|拘留所|申請友鏈|付款方式|鲁虺繁体中文社区 |贊助我們 -

GMT+8, 2026-8-29 15:18 , Processed in 0.205011 second(s), 12 queries , WinCache On.

Powered by HCMS 1.0

© 2004-01-11 鲁虺文化网 版权所有

快速回復 返回頂部 返回列表