新手需要有关获取数据并以可用格式返回数据的脚本的建议

发布于 2024-08-03 11:06:20 字数 419 浏览 11 评论 0原文

我有大量图像要放入网页中。我认为我可以编写一个脚本来完成这项工作,而不是费力地输入所有图像属性。我只需要朝着正确的方向一点点推动。

我希望脚本获取每个图像的宽度和高度,然后将其格式化为包含我正在使用的 url 的 img 标签。我想首先我会循环遍历目录中的文件并将我正在查找的结果输出到文件中。然后我就可以复制和粘贴了。

理想情况下,它最终应该有一个 GUI,您可以在其中选择每个文件,将结果保存到剪贴板,然后将其直接粘贴到文档中。但我相信目前这超出了我的能力范围。

我对编码有基本的了解,并且用 Python 和 PHP 编写了一些脚本。

我从哪里开始?我可以使用Python吗?还是PHP?或者不同的语言? PHP 有 getimagesize() 函数,可以返回我需要的数据。 Python有类似的功能吗?

抱歉啰嗦了。感谢您的任何意见。

I have a large number of images that I am putting into web pages. Rather than painstakingly enter all of the image attributes, I thought I could write a script that would do the work for me. I just need a little push in the right direction.

I want the script to get the width and height of each image, then format this into an img tag that would include the url I'm using. I'm thinking that first I would loop through the files in the directory and output the results that I'm looking for to a file. Then I can copy and paste.

Ideally, it should eventually have a GUI where you can choose each file, save the result to the clipboard, then paste it directly into the document. But I believe this is beyond me at this point.

I have a basic understanding of coding and have done a smattering of scripts in Python and PHP.

Where do I start? Can I use Python? Or PHP? Or a different language? PHP has the getimagesize() function that returns the data I need. Does Python have a similar function?

Sorry to be long winded. And thanks for any input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然 2024-08-10 11:06:20

查看 PIL

from PIL import Image
im = Image.open("yourfile.jpg")
print im.size

有关循环文件,请参阅 本教程

Check out the PIL:

from PIL import Image
im = Image.open("yourfile.jpg")
print im.size

For looping through files see this tutorial.

谎言 2024-08-10 11:06:20

也许更好的解决方案不是创建一个脚本来生成所有图像元素的列表以供您放入文档中,而是在所需文档中动态生成图像元素。这可以这样做:

if ($handle = opendir('/path/to/images')) {
    while (false !== ($file = readdir($handle))) {
        ?>
        <img src="http://you.server.com/path/to/images/<?php echo $file ?>" alt="<?php echo $file ?>" />
        <?php
    }
    closedir($handle);
}

此外,我不明白为什么你想要在 img 元素中包含图像高度和宽度,因为不需要指定这些。高度和宽度仅用于修改图像尺寸,因为浏览器会自动显示实际尺寸。

但要包括图像大小, getimagesize() 函数可以工作:

if ($handle = opendir('/path/to/images')) {
    while (false !== ($file = readdir($handle))) {
        $size=getimagesize('/path/to/images/'.$file);
        ?>
        <img <?php echo $size[3] ?> src="http://you.server.com/path/to/images/<?php echo $file ?>" alt="<?php echo $file ?>" />
        <?php
    }
    closedir($handle);
}

更多信息: http://nl2.php .net/function.getimagesize

Maybe the better solution would be not to create a script that generates a list of all the image elements for you to put in your document but rather generate the image elements on the fly in the desired document. This could be done like this:

if ($handle = opendir('/path/to/images')) {
    while (false !== ($file = readdir($handle))) {
        ?>
        <img src="http://you.server.com/path/to/images/<?php echo $file ?>" alt="<?php echo $file ?>" />
        <?php
    }
    closedir($handle);
}

Furthermore I don't understand why you would want to include image height and width in the img elements as there is no need to specify those. The height and the width are only used to modify the image dimensions as the browser automatically display's the actual size.

But to include the image size the getimagesize() function would work:

if ($handle = opendir('/path/to/images')) {
    while (false !== ($file = readdir($handle))) {
        $size=getimagesize('/path/to/images/'.$file);
        ?>
        <img <?php echo $size[3] ?> src="http://you.server.com/path/to/images/<?php echo $file ?>" alt="<?php echo $file ?>" />
        <?php
    }
    closedir($handle);
}

more info: http://nl2.php.net/function.getimagesize

百善笑为先 2024-08-10 11:06:20

您还可以使用 identify 命令.imagemagick.org/script/index.php" rel="nofollow noreferrer">Imagemagick:

for file in dir/*; do
  identify -format "Width: %w, Height: %h" $file
done

You can also use shell identify command from Imagemagick:

for file in dir/*; do
  identify -format "Width: %w, Height: %h" $file
done
若水般的淡然安静女子 2024-08-10 11:06:20

我认为这应该做你想做的。但这不一定是最好的方法(已经晚了,我累了,等等......):

<?php

$directory = "img"; // The path in which your images are located.

if ($directory) {
$files = scandir($directory); // $files becomes an array of files in the relevant directory.
}

foreach($files as $k => $v) {
  if ($v == "." || $v == "..") {
    // because I'm not good enough to think of a better way
    unset($k);
  }
  else {
    $size = getimagesize($directory . "/" . $v);
    $images[] = array('img' => $v, 'w' => $size[0], 'h' => $size[1]); // $size[0] is width, $size[1] is height, both in px.
  }
}

unset($files); // I just like to clear as I go. I make more than enough mess as it is without keeping it around.

  if ($images) {
    foreach($images as $key => $value) {
      echo "\n\t\t<img src=\"$directory/" . $value[img] . "\" width=\"" . $value[w] . "px\" height=\"" . $value[h] . "px\" alt=\"" . $value[img] . "\" />";
    }
  }

?>

I think this should do what you want. But it's not necessarily the best way (it's late, I'm tired, etc...):

<?php

$directory = "img"; // The path in which your images are located.

if ($directory) {
$files = scandir($directory); // $files becomes an array of files in the relevant directory.
}

foreach($files as $k => $v) {
  if ($v == "." || $v == "..") {
    // because I'm not good enough to think of a better way
    unset($k);
  }
  else {
    $size = getimagesize($directory . "/" . $v);
    $images[] = array('img' => $v, 'w' => $size[0], 'h' => $size[1]); // $size[0] is width, $size[1] is height, both in px.
  }
}

unset($files); // I just like to clear as I go. I make more than enough mess as it is without keeping it around.

  if ($images) {
    foreach($images as $key => $value) {
      echo "\n\t\t<img src=\"$directory/" . $value[img] . "\" width=\"" . $value[w] . "px\" height=\"" . $value[h] . "px\" alt=\"" . $value[img] . "\" />";
    }
  }

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文