I'm not a coder at all I'm just learning bits and bobs so I can get some faces for my art project. I want to download 100 faces from the thispersondoesnotexist website and I have so far gotten this far with my code.
import urllib.request
urllib.request.urlretrieve("http://www.thispersondoesnotexist.com", "image.jpg")
The code downloads the face and saves it as "image.jpg" but when I run it again it overwrites the image I just saved. How can I find out how to write code that will randomly generate a filename? I'm using the PyCharm IDE.
To generate random file names you can include this piece of code.
This code will generate a random string of length 6 and uses it as the filename everytime you run the program.
import random
import string
import urllib.request
filename = ''.join(random.choices(string.ascii_uppercase + string.digits, k = 6)) + '.jpg'
urllib.request.urlretrieve("http://www.thispersondoesnotexist.com", filename)
A nice way can be generating timestamp and use it to name a file:
import urllib.request
import datetime
def generate_timestamp():
return str(datetime.datetime.now()).replace('-', '_').replace(':', '_').replace(' ', '__')
urllib.request.urlretrieve("http://www.thispersondoesnotexist.com", "image_" + generate_timestamp() + ".jpg")
This will serve dual purposes:
Naming a file uniquely.
Getting idea when it has finished downloading.
Related
[This is a screenshot ofHeres a screenshot of the error code block]2Heres the code:
import os
from moviepy.editor import *
Memes = []
numLabel = 0
for pictures in os.listdir("memes"):
ImageClip("memes/" + pictures).set_duration(5)
Memes.append(pictures)
video = concatenate(Memes, method="compose")
video.write_videofile('test.mp4', fps=24)
I get this error:
'str' object has no attribute 'duration'
as you can see there is a duration assigned to the list of memes
please help me I wasted 3 days on this error
First of all, you shouldn't write
from moviepy.editor import *.
Write from moviepy.editor import ImageClip, concatenate to import only the class and function you need. Plus concatenate is deprecated. Use concatenate_videoclips instead: from moviepy.editor import ImageClip, concatenate_videoclips.
The line ImageClip("memes/" + pictures).set_duration(5) will also not work. You have to assign the ImageClip to a variable to append it to the list. ImageClip has the parameter duration in it's constructure to use. The line should look like this: clip = ImageClip("memes/" + pictures, duration=5)
And now append the clip to the list: Memes.append(clip).
This should work and generate the expected result.
I can see what you're trying to do and put my spin on it. Firstly you need to put all the images into a list. Then you need to change all the images in the list to imageclips with durations and fps which is probably what is causing your error message. Then you can concatenate it however you wish. Remember, Never put concatenate of any kind inside a loop, Moviepy bugs out.
import os
import moviepy.editor as mp
from moviepy.editor import * #As the other guy said, importing everything from moviepy editor will slow it down substantially so it's best to just import what you need instead of *
Memes = [] #this is gonna be images
slides = [] #this is gonna be imageclips
size = (1920,1080) #Size of the images must be the same or you'll have to reformat them to be the same
image_dir = "Z:blablabl/blabla/blabla" #This is the folder with all the images
#This is the correct way to put whatever image into a list
for filename in os.listdir(image_dir):
if filename.endswith(".jpg") or filename.endswith(".png"):
Memes.append(os.path.join(image_dir, filename))
for n, url in enumerate(Memes): #All Images in image_dir is now in slideshow
slides.append(
mp.ImageClip(url).set_fps(25).set_duration(5).resize(size) #Change 5 into whatever you want duration to be and fps too
)
video = mp.concatenate_videoclips(slides)
video.write_videofile("test4.mp4")
I am attempting to write a bit of python that uses EasyOCR to write the numbers it sees in the images into a text file. My goal is to batch process all images in a directory, rather than a single images at a time, as I have several thousand images to process.
The python code:
import cv2
import os
import io
reader = easyocr.Reader(['en'])
for image_name in os.listdir("ocr-source"):
image = cv2.imread(f'ocr-source/{image_name}')
result = reader.readtext(image, allowlist='0123456789', detail=0)
print(image_name, " ", result, file=open('output.txt', 'w'))
My test ocr-source directory contains about 10 images.
The resulting output.txt file only contains the results from a single image.
How to I get it to properly iterate through the entire directory?
Simple fix: Instead of writing over the file each loop, I needed to append.
import cv2
import os
import io
reader = easyocr.Reader(['en'])
for image_name in os.listdir("ocr-source"):
image = cv2.imread(f'ocr-source/{image_name}')
result = reader.readtext(image, allowlist='0123456789', detail=0)
print(image_name, " ", result, file=open('output.txt', 'a'))
Note the 'a' in the print call
I am trying to get properties (just width & heigth so far, but probably more later) of an album art picture from an mp3 file using python 3.7.1 and mutagen 1.42, but nothing seems to work so far. I am yet able to extract some other information correctly
The doc is telling about APIC, but trying to display all tags doesn't show anything related to any picture (and my mp3 test files does have album pictures) :
import os,sys
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
song_path = os.path.join(sys.argv[1]) # With sys.argv[1] the path to a mp3 file containing a picture
track = MP3(song_path, ID3=EasyID3)
pprint(track.get('title')[0] + ' ' + str(track.info.length) + 's, ' + str(int(track.info.bitrate / 1000)) + 'kbps')
print(track.keys())
The result, using a file of mine :
> Exponential Tears 208.0s, 205kbps
> ['album', 'copyright', 'encodedby', 'length', 'title', 'artist', 'albumartist', 'tracknumber', 'genre', 'date', 'originaldate']
(This mp3 file does have an embedded picture, that I can see with any music software I use.)
I have found a lot of different ways of handling this with mutagen, but some seems outdated, others just doesn't work, I don't understand what I am missing here.
Any help here would be gladly appreciated
OK, i eventually figured it out : the EasyID3 module only handles most common tags, and it does not includes picture data (APIC). For that, you need to use the ID3 module, which is way more complex to understand. Then, look for the APIC: key, which stores the picture as a byte string.
Here is a little exemple, using PIL to deal with pictures :
import os,sys
from io import BytesIO
from mutagen.mp3 import MP3
from mutagen.id3 import ID3
from PIL import Image
song_path = os.path.join(sys.argv[1])
track = MP3(song_path)
tags = ID3(song_path)
print("ID3 tags included in this song ------------------")
print(tags.pprint())
print("-------------------------------------------------")
pict = tags.get("APIC:").data
im = Image.open(BytesIO(pict))
print('Picture size : ' + str(im.size))
Hope it helps, good luck ! ;)
This is my code to take the Astronomy picture of the day and automatically make it my background every 24 hours. In windows, when you make a background picture you can choose a variety of ways that it is applied. An example would be tiled, or forced to take up the whole screen. I need that ability and cant find anything online.
import ctypes
import urllib
import time
import os, sys
from bs4 import BeautifulSoup
while True:
try:
url = "http://apod.nasa.gov/apod/astropix.html"
page = BeautifulSoup(urllib.urlopen(url))
for image in page.findAll("img"):
print "Image: %(src)s" % image
parsed = "http://apod.nasa.gov/apod/"+"%(src)s" % image
x = urllib.urlretrieve(parsed)
ctypes.windll.user32.SystemParametersInfoA(20, 0,x[0], 0)
os.remove(x[0])
time.sleep(86400)
except:
continue
I want to convert some multi-pages .tif or .pdf files to individual .png images. From command line (using ImageMagick) I just do:
convert multi_page.pdf file_out.png
And I get all the pages as individual images (file_out-0.png, file_out-1.png, ...)
I would like to handle this file conversion within Python, unfortunately PIL cannot read .pdf files, so I want to use PythonMagick. I tried:
import PythonMagick
im = PythonMagick.Image('multi_page.pdf')
im.write("file_out%d.png")
or just
im.write("file_out.png")
But I only get 1 page converted to png.
Of course I could load each pages individually and convert them one by one. But there must be a way to do them all at once?
ImageMagick is not memory efficient, so if you try to read a large pdf, like 100 pages or so, the memory requirement will be huge and it might crash or seriously slow down your system. So after all reading all pages at once with PythonMagick is a bad idea, its not safe.
So for pdfs, I ended up doing it page by page, but for that I need to get the number of pages first using pyPdf, its reasonably fast:
pdf_im = pyPdf.PdfFileReader(file('multi_page.pdf', "rb"))
npage = pdf_im.getNumPages()
for p in npage:
im = PythonMagick.Image('multi_page.pdf['+ str(p) +']')
im.write('file_out-' + str(p)+ '.png')
A more complete example based on the answer by Ivo Flipse and http://p-s.co.nz/wordpress/pdf-to-png-using-pythonmagick/
This uses a higher resolution and uses PyPDF2 instead of older pyPDF.
import sys
import PyPDF2
import PythonMagick
pdffilename = sys.argv[1]
pdf_im = PyPDF2.PdfFileReader(file(pdffilename, "rb"))
npage = pdf_im.getNumPages()
print('Converting %d pages.' % npage)
for p in range(npage):
im = PythonMagick.Image()
im.density('300')
im.read(pdffilename + '[' + str(p) +']')
im.write('file_out-' + str(p)+ '.png')
I had the same problem and as a work around i used ImageMagick and did
import subprocess
params = ['convert', 'src.pdf', 'out.png']
subprocess.check_call(params)