合并一组图像以创建背景图像。

huangapple go评论69阅读模式
英文:

Combine set of images to create background image

问题

To iterate through the folder books_images and randomly select a specified number of images each time, then combine them into one single image to be used as a background for index.html, you can use Python with libraries like PIL (Pillow) and os. Here's a basic outline of the steps you can follow:

import os
import random
from PIL import Image, ImageDraw

# Set the path to the folder containing your images
folder_path = 'books_images/'

# Set the number of images to pick randomly each time
num_images_to_pick = 3

# List all image files in the folder
image_files = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]

# Randomly select a subset of image files
selected_images = random.sample(image_files, num_images_to_pick)

# Create a blank canvas for combining images
canvas_width = 800
canvas_height = 600
combined_image = Image.new('RGB', (canvas_width, canvas_height))

# Create a drawing context for pasting images onto the canvas
draw = ImageDraw.Draw(combined_image)

# Paste selected images onto the canvas
x_position = 0
for image_file in selected_images:
    img = Image.open(os.path.join(folder_path, image_file))
    combined_image.paste(img, (x_position, 0))
    x_position += img.width

# Save the combined image
combined_image.save('combined_image.jpg')

# Now, 'combined_image.jpg' can be used as a background for your HTML page.

This code selects a specified number of images randomly from the books_images folder, combines them horizontally into one single image, and saves it as combined_image.jpg, which you can then use as a background for your index.html page.

英文:

If i had a folder books_images with set of images

- books_images 
-- 1.jpg
-- 2.jpg
-- 3.jpg
-- 4.jpg
-- 5.jpg
-- 6.jpg

How can i iterate through this folder and pick up (number) of images in random way each time and combine all into one single image to be used as background for index.html

just like this one and if you make refresh, it makes different sequences in images.

合并一组图像以创建背景图像。

答案1

得分: 1

这个小演示有效,我只完成了6张图片中的4张选择。您可以根据需要修改对 'randomNo' 的调用。您需要扩展 CSS 规则以适应要一次显示的图像数量。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>背景演示</title>
</head>
<body onload="randomBackground()">

  <script>
    function randomNo(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min) + min);
    }
    function randomBackground() {
        var element = document.createElement('style'), sheet;
        document.head.appendChild(element); // 创建样式表
        sheet = element.sheet;

        var styles = 'body {';
        styles += 'background: url(images/'+randomNo(1,6)+'.jpg), url(images/'+randomNo(1,6)+'.jpg), url(images/'+randomNo(1,6)+'.jpg), url(images/'+randomNo(1,6)+'.jpg);'; // 构建随机背景
        styles += 'background-repeat: no-repeat, no-repeat, no-repeat, no-repeat;';
        styles += 'background-size: 50vw 50vh, 50vw 50vh, 50vw 50vh, 50vw 50vh;';
        styles += 'background-position: 0 0, 50vw 0, 0 50vh, 50vw 50vh;';
        styles += '}';
        sheet.insertRule(styles, 0); // 添加规则到新的样式表
    }
  </script>
</body>
</html>
英文:

This little demo works, I have only done 4 images from a selection of 6. You can amend the calls to 'randomNo' as needed. You would need to expand the css rules to fit the number of images you want to display at once.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;title&gt;Background Demo&lt;/title&gt;

&lt;/head&gt;
&lt;body onload=&quot;randomBackground()&quot;&gt;

  &lt;script&gt;
    function randomNo(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min) + min);
    }
    function randomBackground() {
        var element = document.createElement(&#39;style&#39;), sheet;
        document.head.appendChild(element); //create a stylesheet
        sheet = element.sheet;

        var styles = &#39;body {&#39;;
        styles += &#39;background: url(images/&#39;+randomNo(1,6)+&#39;.jpg), url(images/&#39;+randomNo(1,6)+&#39;.jpg), url(images/&#39;+randomNo(1,6)+&#39;.jpg), url(images/&#39;+randomNo(1,6)+&#39;.jpg);&#39;; //Build the random background
        styles += &#39;background-repeat: no-repeat, no-repeat, no-repeat, no-repeat;&#39;
        styles += &#39;background-size: 50vw 50vh, 50vw 50vh, 50vw 50vh, 50vw 50vh;&#39;
        styles += &#39;background-position: 0 0, 50vw 0, 0 50vh, 50vw 50vh;&#39;
        styles += &#39;}&#39;;

        sheet.insertRule(styles, 0); //add rule to new sheet

    }
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案2

得分: 0

Here are the translated parts of your code:

if you have for example 20 images in your folder , you should give a random number between 0 and 20 and set it as a variable at end of your folder directory , for example : 

let myRandomNumber = Math.random() * 20;
let img = document.querySelector("img");
let randomPath = '/your-folder-name/' + myRandomNumber + '.jpg';
img.src = randomPath;

if you want to iteration through a folder and combine a random number of image :
let images = document.querySelectorAll("img");
let path = '/your-folder-path/';

const myArr = []
while(myArr.length <= images.length){
    let randomNumber = Math.floor(Math.random() * images.length);
    if (myArr.indexOf(randomNumber) === -1) {
        myArr.push(randomNumber);
    }
}

if(images){
    images.forEach((img , index) => {
        img.src = path + myArr[index]
    })
}

Is there anything else you'd like me to translate?

英文:

if you have for example 20 images in your folder , you should give a random number between 0 and 20 and set it as a variable at end of your folder directory , for example :

let myRandomNumber = Math.random() * 20;
let img = document.querySelector(&quot;img&quot;);
let randomPath = &#39;/your-folder-name/&#39; + myRandomNumber + &#39;.jpg&#39;;
img.src = randomPath;

if you want to iteration through a folder and combine a random number of image :

let images = document.querySelectorAll(&quot;img&quot;);
let path = &#39;/your-folder-path/&#39;

const myArr = []
while(myArr.length &lt;= images.length){
    let randomNumber = Math.floor(Math.random() * images.length);
    if (myArr.indexOf(randomNumber) === -1) {
        myArr.push(randomNumber);
    }
}

if(images){
    images.forEach((img , index) =&gt; {
        img.src = path + myArr[index]
    })
}

huangapple
  • 本文由 发表于 2023年5月10日 16:55:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定