Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random pictures from a file
#1
Hello I'm trying to create a discord bot and I'm stuck one subject. I want to share a random photo from a folder when someone write in the chat specific words. For example when someone write 'dog', ı want to share a dog photo from a folder. I'll show my basic code I hope you will help me.

import os
import random
import discord
from discord.ext import commands

my_files = [discord.File('A:\\ma\\1.jpg'), discord.File('A:\\ma\\2.jpg')]
bat = ['dog', 'cat']
.
.
.
if any(word in msg for word in bat):
await message.channel.send(files=my_files)
Reply
#2
(Apr-23-2021, 11:37 AM)aliwien Wrote: Hello I'm trying to create a discord bot and I'm stuck one subject. I want to share a random photo from a folder when someone write in the chat specific words. For example when someone write 'dog', ı want to share a dog photo from a folder. I'll show my basic code I hope you will help me.

import os
import random
import discord
from discord.ext import commands

my_files = [discord.File('A:\\ma\\1.jpg'), discord.File('A:\\ma\\2.jpg')]
bat = ['dog', 'cat']
.
.
.
if any(word in msg for word in bat):
await message.channel.send(files=my_files)

Well, first, it is not a "random" photo, it is a specific photo. What I would do is tag the metadata of each photo with the tags that would include it. I would write a program that I would run every so often (once a month, maybe) that opened each photo and read the metadata, and found the tags. It would then create a dictionary of tag : (photo1, photo2, ..., photoN). I would save this dictionary to a disk file. When I ran the Python program, it would start up by reading this dictionary in. Then, when it found a tag in the chat that matched a tag in the dictionary, it would select one of the photos in the list, and in this case it might choose, if > 1 photos, a photo randomly of that set. This way, I don't have to create a list of the photos by hand in the source code. I might use a program that let me add captions to a photo, and I might add a caption like
Fido and I at the beach :dog: :beach:
The format for metadata in photos (particularly JPEG) is documented; there may even be a place for a text caption and for keywords, in which case the syntax I just gave may not be necessary. This means that if you want to remove a tag, you can do it easily, rerun the indexing program, and that photo is no longer in the set. I would never consider any solution that required I build a table on my own, separate from the photos.

I'm not sure what discord.File() does, but it strikes me that if you had a thousand photos, this involves reading all thousand photos in when the program starts. Having two tables, "my_files" and "bat", is insane and unmaintainable once the quantity of photos hits two digits. I would store just the filenames, and not apply discord.File() until I knew which photo I was going to send.

I worry about things like "await" applied to something that could take a long, possibly unbounded, time. This is frequently bad style, because it makes an assumption which might frequently be invalid: that the operation will complete quickly. This is a dangerous assumption.
Reply
#3
Can show demo of a generic solution.
Let say have a folder animal that have sub-folder like dog_pic, cat_pic...ect.
So when dog is the chosen word pick a random image from dog_pic folder.
import os
import  random

def folder_pic(path, name_choice):
    '''Choice right animal folder'''
    for folder in os.scandir(path):
        if os.path.isdir(folder):
            if name_choice in folder.name:
                return folder.name

def pick_random_animal(path, animal_folder):
    '''Get a random image from animal folder'''
    path = os.path.join(path, animal_folder)
    pic = random.choice(os.listdir(path))
    return pic

if __name__ == '__main__':
    path = r'E:\div_code\animals'
    name_choice  = 'dog'
    animal_folder = folder_pic(path, name_choice)
    print(pick_random_animal(path, animal_folder))
Output:
# Run 3 times dog_2.png dog_1.png dog_3.png
So if change name_choice = 'cat' will pic from cat_pic folder.
Output:
# Run 3 times cat_1.png cat_4.png cat_4.png
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 341 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Find a shift between 2 pictures TazFleck 0 1,103 Jan-18-2023, 09:56 PM
Last Post: TazFleck
Photo put a green boarder and text around multiple pictures jackosullivan 5 1,402 Jul-05-2022, 10:39 AM
Last Post: snippsat
  Help with taking pictures on raspberry pi octavia 1 1,662 Feb-07-2020, 04:54 PM
Last Post: Larz60+
  client-server pictures mcgrim 4 2,917 Oct-25-2019, 09:53 PM
Last Post: micseydel
  Print random musik to wav file Pereskia 2 2,638 Mar-27-2019, 11:53 AM
Last Post: Pereskia
  Open Pictures with Python MaxouRuEnIpTF34 1 3,002 Apr-22-2018, 07:48 PM
Last Post: Gribouillis
  Write selected (random) columns to output-file anjohepa 0 2,312 Feb-27-2018, 02:19 PM
Last Post: anjohepa
  Comparing pictures pedro 1 2,710 Sep-11-2017, 07:51 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020