如何从文件夹中检索特定图像,由带有图像名称的字符串列表指示

时间:2018-02-23 11:51:39

标签: python

我有一般性问题,没有代码,因为我对如何做到这一点没有明确的想法。

我有一个包含许多不同图像(.jpg)的文件夹,还有一个csv文件,用于在字符串列表中保存不同的图像名称。每个列表都有不同的图像名称组合,例如:

str_all1 = ['01_2.jpg','06_2.jpg','08_1.jpg','04_2.jpg','10_1.jpg']

str_all2 = ['10_1.jpg','01_2.jpg','06_2.jpg','08_1.jpg','03_1.jpg']

str_all3 = ['06_2.jpg','08_1.jpg','01_2.jpg','04_2.jpg','10_1.jpg']

在csv文件中,上面列表中的每个元素都对应一列。此外,str_all1在第一行,str_all2在第二行,等等。

我们的想法是从cvs文件中选择一个随机行(因为一行包含将呈现给一个参与者的图像列表)并将图像名称与其实际图像匹配(在图像文件夹中)我想要展示。

如何使用csv文件中的此列表从文件夹中检索图像并显示它们?

1 个答案:

答案 0 :(得分:0)

您需要随机模块。您可以使用 <?php //ajaxIndex.php include 'connectionFile1.php' ; //connection file $sql1 = "SELECT * FROM users" ; if ($result1 = mysqli_query($connection, $sql1) ) { $resultsSql1 = mysqli_num_rows($result1) ; $return_array = array(); while ($row1 = mysqli_fetch_array($result1)) { $row_array['userName'] = $row1['userName']; $row_array['email'] = $row1['email']; //$return_array[] = $row1 ; array_push($return_array, $row_array); } //encode the mysql array print json_encode($return_array); // this array is correctly encoded } ?> 随机选择一个列表。

示例:

<script>

//$(document).ready(fetchData) ;

$("#button").click(function () {


    $.ajax({ 

        url: "ajaxIndex.php",      
        type: "GET",
        dataType: 'json' ,                
        success: function(response){ 

            console.log(response) ;
            $.each(response, function() {

                        console.log(this.userName + "<br>") ;
                        console.log(this.email) ;

            }) ;

        }

    });


}) ;


//window.setTimeout(fetchData, 1000);

 </script>