Creating a gif in python using ImageMagick - python

I am trying to create a gif using python either through the GraphicsMagick module or using os.system commands with the ImageMagick.exe. The most relevant question I have found here was this question about 6 years ago.
Similar Question 6 years old
URL for imagemagick download
file:///C:/Program%20Files/ImageMagick-6.9.1-Q16/www/binary-releases.html#windows
GraphicsMagick Download
Below is my code (not working). I am currently trying the ImageMagick route as the above question stated that this was more pythonic. Any help would be appreciated.
from pgmagick import Image, ImageList, Geometry, Color
import os, sys
import glob
#dataDir = sys.argv[1]
#outDir = sys.argv[2]
dataDir = 'C:\\Users\\name\\Desktop\\a'
os.chdir(dataDir)
fileList = glob.glob(dataDir + '\*.jpg')
fileList.sort()
os.system('convert fileList -delay 20 -loop 0 *jpg animated.gif')
I get an invalid syntax error on the last line of my code.

For anyone that sees this in the future when trying to make a gif with imagemagick on a windows machine, this is the solution that I figured out. I don't know if this is the most efficient way in terms of memory, but it works.
import os, sys
dataDir = 'fullpath of directory with images'
os.chdir(dataDir)
os.system('SETLOCAL EnableDelayedExpansion')
#This is the path of the imagemagick installation convert command.
#The invalid parameter I was getting was because the computer was trying to
#use a different convert command. This code sets convert as a different
#variable and then calls that new variable as the convert command.
os.system('SET IMCONV="C:\Program Files\ImageMagick-6.9.1-Q16\Convert"')
os.system('%IMCONV% *.jpg animated.gif')
Some changes I made to simplify the solution. Instead of making a file list as I did in my question, I just stuck all the images in a directory, and then called the command to convert these images to a gif. This is the simplest solution that I have come across.

Related

Image moves or deletes when displaying with PIL

Recently, I have been working on one of my python3 programs and then I wanted to to open a picture. Here is the code that I used to do it:
from PIL import Image
r = Image.open('C:/Users/sudam/OneDrive/Desktop/programming/python/projects/good night app/morning.png' )
r.show()
But as soon as I run this code,the windows photo viewer opens and gives and error saying that the specified file was moved. I tried googling this question but all of the answers I got only worked for python2, but not for python3.
Because you have whitespace in your path you need to use the r"string" format.
Also you need to use:
PIL.ImageShow.show(r)
to show your image.
It's also recommended to check if the file exist before opening any file.
You can do like this:
from pathlib import Path
from PIL import Image, ImageShow
path =r"C:/Users/sudam/OneDrive/Desktop/programming/python/projects/good night app/morning.png"
if Path(path).is_file():
r = Image.open(path)
ImageShow.show(r)
else:
print(f'{path} not exist')

Saving image files from Music21

I've downloaded a bunch of .krn files, and I'd like to convert them into images - either pngs or jpgs - using music21. I've tried this:
When I do this:
from music21 import *
op = krnfile
s = converter.parse(op)
s.show()
I see a great image file in the Jupyter Notebook I'm using, but when I try to save that file programatically like this:
s.write(fp = 'outputfile.png', fmt = 'png')
It says:
Music21ObjectException: cannot support showing in this format yet: png
Which seems a little weird since it obviously manages to make an image for display in the notebook.
It looks like maybe I could use LilypondConverter.createPNG(fileName=None) from this, but is installing Lilypond required? I already have MuseScore2 installed, which opens when I call s.show().
Thanks a lot!
Alex
Install musescore on your computer, re-run python -m music21.configure to help it find it and then do:
from music21 import *
op = 'krnfile.krn'
s = converter.parse(op)
fp = s.write('musicxml.png')
# or just s.show('musicxml.png') to test that it works.
If it's a multi-page file, fp will be the path to the first page. It will end in -1 or -01 or -001 etc. You can read through the directory to find other files with the same name until there are no more to get all the images.
If you use n.show('lily.png'), it should create a temporary png file somewhere. Try to use it and an image may open.
Sorry i don't know much yet, I hope it helps.

python reading a video

I am using Python 2.7.11 and OpenCV 2.4.9. I cannot read a video by using cv2.imread() or cv2.VideoCapture().
import cv2
cap = cv2.VideoCapture('cam.avi')
print ("open = ",cap.isOpened())
OR
import cv2
cap = cv2.imread('cam.avi')
print ("open = ",cap.isOpened())
It will return false.
I don't know why. I am sure that the cam.avi is here.
imread() does not support reading from video files directly.
See also the documentation of OpenCV.
If you want to read a video with imread you will first have to convert it to single images, either via a serperate program (ffmpeg comes to mind) or using OpenCV and store the images in memory.
Try providing full path to video, like:
import cv2
cap = cv2.VideoCapture(r'C:\Users\e01069\Downloads\drop.avi')
print ("open = ",cap.isOpened())
If you run following in your same file, you would know that python is looking for your file on some different location.
import os
print os.path.abspath(__file__) #this is your current working directory
Note: .imread wouldn't work this way.

Python troubles

I've been trying to throw together a python program that will align, crop and create an RGB image from HST and VLA .fits data. Unfortunately I've run into a bit of a problem with it continually opening a past file that does not exist in the folder and neither is it opening in the code itself. I've googled and googled and haven't found anything like it, so perhaps it's just common sense to most, but I can't figure it out. Here's the error message:
You can see at the top that the program I'm running has the filename rgbhstvla.py. I'm not sure what the error message means. Here's the python program as well:
import pyfits
import numpy as np
import pylab as py
import img_scale
from pyraf import iraf as ir
fits.open('3c68.fits', readonly)
j_img = pyfits.getdata('230UVIS.fits')
h_img = pyfits.getdata('230IR.fits')
k_img = pyfits.getdata('5GHZ.fits')
jmin,jmax = j_img.mean()+0.75*j_img.std(),j_img.mean()+5*j_img.std()
hmin,hmax = h_img.mean()+0.75*h_img.std(),h_img.mean()+5*h_img.std()
kmin,kmax = k_img.mean()+0.75*k_img.std(),k_img.mean()+5*k_img.std()
img = numpy.zeros((1024,1024,3))
img[:,:,0] = img_scale.asinh(j_img,scale_min=jmin,scale_max=jmax)
img[:,:,1] = img_scale.asinh(h_img,scale_min=hmin,scale_max=hmax)
img[:,:,2] = img_scale.asinh(k_img,scale_min=kmin,scale_max=kmax)
pylab.clf()
pylab.imshow(img)
pylab.show()
(I'm still working on the program since I'm new to python, tips here would be nice as well but they're mostly unnecessary as I'm sure I'll figure it out eventually).
Python cannot find the file 3c68.fits, which is expected to be in the current working directory, C:\Users\Brandon\Desktop\Research. Either make sure the file is in that directory, or provide an absolute path in your code.

Use ImageMagick with python. (on a linux system) [duplicate]

This question already has answers here:
Can I access ImageMagick API with Python?
(3 answers)
Closed 9 years ago.
I want to define a function that "call" imagemagick to convert an image.
def convert(filein,fileout):
#imagemagick>convert filein fileout
How can I call and use imagemagick with Python?
I'm running on a linux system, imagemagick is installed, and I'm not using PIL.module because it doesn't handle PPM[p3].
Disclaimer: I am the author of Wand.
You can easily do that using Wand, a simple binding of ImageMagick for Python. For example, the following code converts a PNG image to a JPEG image:
from wand.image import Image
with Image(filename='in.png') as img:
img.format = 'jpeg'
img.save(filename='out.jpg')
See this tutorial as well.
Either use one of the shell interfaces of Python (os.system, subprocess.Popen) to call the imagemagick binary, or try out PythonMagick.
I would suggest u use subprocess it is safer
import subprocess
params = ['convert', 'src_file', 'result_file']
subprocess.check_call(params)
I have not used image magic but you could use os.system to call a shell command:
import os
os.system('imagemagick-converting-command filein fileout')
I suggest you go with PythonMagic as Creshal said. It is provided by ImageMagic and thus must be one of the best port available for python.

Categories