小编Dan*_*iel的帖子

裁剪图像到圆圈的最有效方法(在R中)?

TL; DR:将矩形图像裁剪成圆圈的最有效方法是什么?

说明/背景:

我正在研究R中的一些代码,它会将Spotify艺术家图像显示为圆圈而不是默认的矩形/正方形.我找不到在R中裁剪图像的任何包或命令,特别是圆圈,所以我编写了自己的函数circ,它读取三维(或四维)RGB(A)数组并将它们裁剪成一个圆圈使用圆的参数方程来确定每个唯一y的x值.这是我的伪代码:

Given an RGB(A) array:
    Find the center of the image, radius = min(x coord, y coord)
    Pre-crop the image to a square of dimensions 2r x 2r
    For every unique y value:
        Determine the x coordinates on the circle
        Make pixels outside of the circle transparent
    Return the cropped image as an RGBA array
Run Code Online (Sandbox Code Playgroud)

这个功能比我之前的功能有了很大的改进,它检查了每个像素的位置,看它是在圆圈的内部还是外部,但我仍然觉得它可以进一步加速.

有没有办法可以检查一半的y值而不是所有的y值,然后镜像在圆圈上?我可以使用实际的裁剪功能吗?任何和所有的帮助非常感谢!

编辑添加一些复制粘贴运行代码(感谢@lukeA):

我的原始裁剪方法:

circ = function(a){
  # First part of the function finds the radius of …
Run Code Online (Sandbox Code Playgroud)

r crop

7
推荐指数
2
解决办法
1018
查看次数

烧瓶request.files为空

就像这个问题一样,我正在尝试按照简单的Flask教程将文件上传到Flask服务器。在我的特定情况下,我正在尝试上传XML文件。

我正在使用的(简体)HTML是:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file">
    <input type="submit" value="Let's go!">
</form>
Run Code Online (Sandbox Code Playgroud)

该请求由一个if request.method == 'POST':块正确处理,因此我放入了一些打印语句来进行故障排除:

print('request.method', request.method)
print('request.args', request.args)
print('request.form', request.form)
print('request.files', request.files)
Run Code Online (Sandbox Code Playgroud)

结果是:

request.method POST
request.args ImmutableMultiDict([])
request.form ImmutableMultiDict([])
request.files ImmutableMultiDict([])
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如果需要,我可以提供更完整的源代码。

python flask

5
推荐指数
1
解决办法
5378
查看次数

d3-无法从json请求返回数据?

我正在尝试d3.json()在函数内部使用给定艺术家ID(例如5K4W6rqBFWDnAN6FQUkS6x)的Spotify API返回数据,但我不知道如何有效地返回数据。该功能看起来像

// Get artist's related artist's information
function relatedArtists(id){
  var jsonPromise = new Promise(function(resolve, reject) {
    // Async JSON request
    d3.json('https://api.spotify.com/v1/artists/' + id + '/related-artists', function(error, data){
      if(error) reject(error);
      resolve(data.artists);
    });
  });

  jsonPromise.then(function(success) {
    console.log(success);
    //return(success)  //doesn't work
  });

  jsonPromise.catch(function(error){
    console.error(error);
  });

}
Run Code Online (Sandbox Code Playgroud)

我试过在函数中创建变量,然后对其进行修改

function relatedArtists(id){
  var testVar = 'hello';
  var jsonPromise = new Promise(...{
    // Async JSON request
    d3.json(...)
  });
  jsonPromise.then(function(success) {
    testVar = success;
  });
  return(testVar);
}
Run Code Online (Sandbox Code Playgroud)

尽管我尽了最大努力,但testVar仍然如此'hello'。我已经阅读了一些有关范围和承诺的文章,但是如果有一些我不理解的核心机制,我很乐意做更多的事情。谢谢阅读!

ajax json d3.js

4
推荐指数
1
解决办法
1796
查看次数

标签 统计

ajax ×1

crop ×1

d3.js ×1

flask ×1

json ×1

python ×1

r ×1