I am trying to visualize music into an image by using sounddevice to input the sound and then converting it to a numpy array.
The array is 2D and so I convert it to 3D (otherwise I only get a single thin vertical line in the image).
However when I use PIL to show the image it says 'Cannot handle this datatype'
The code is mentioned below:
import sounddevice as sd
from scipy.io.wavfile import write
import soundfile as sf
import numpy
from numpy import zeros, newaxis
from PIL import Image
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
print(myrecording)
print(numpy.shape(myrecording))
write('output.wav', fs, myrecording) # Save as WAV file
filename = 'output.wav'
A=myrecording[:,:,newaxis]
print(A)
im = Image.fromarray((A * 255).astype(numpy.uint8))
im.show()
I expect to get an image which shows colours corresponding to the sound being inputted in
This depends a little bit on what you want to do.
You have two channels with n-samples ((nsamples, 2) ndarray); do you want each channel to be a column of the image where the color varies depending on what the value is? That is why you were getting a very narrow image when you just plot myrecording.
You do not really have the data to create a full 2D image, unless you reshape the time series data to be something more like a square (so it actually looks like an image), but then you sort of lose the time dependence nature that I think you are going for.
I want to encode an image in my directory "x.png" to a String or Array{UInt8, 1}.
I am writing a code in Julia to serialize an image using protobufs. It requires the image to be in encoded
String format.
In Python, it is done as follows. I am looking for similar functionality in Julia.
from PIL import Image
img = Image.load('x.png')
import io
output = io.BytesIO()
img.save(output, 'PNG')
img_string_data = output.getvalue()
output.close()
The output may be a String object or an Array{UInt8, 1}
In Julia you can achieve by writing:
img_string_data = read("x.png")
img_string_data now is Vector{UInt8}. You could also write read("x.png", String) to get a String (which is not that useful though as it will probably mostly contain invalid characters).
There is one difference between Julia solution and your Python solution. Julia approach will store in img_string_data the contents identical to what "x.png" holds on binary level while your Python solution will store an identical image, but possibly different on binary level (i.e. PIL might change some bytes in your file).
I am trying to write images over each other. Ideally, what I want to do is to write every image in one folder over every image in another folder and output every unique image to another folder. So far, I am just working on having one image write over one image, but I can't seem to get that to work.
import numpy as np
import cv2
import matplotlib
def opencv_createsamples():
mask = ('resized_pos/2')
img = cv2.imread('neg/1')
new_img = img * (mask.astype(img.dtype))
cv2.imwrite('samp', new_img)
opencv_createsamples()
It would be helpful to have more information about your errors.
Something that stands out immediately is the lack of file type extensions. Your images are probably not being read correctly, to begin with. Also, image sizes would be a good thing to consider so you could resize as required.
If the goal is to blend images, considering the alpha channel is important. Here is a relevant question on StackOverflow:How to overlay images in python
Some other OpenCV docs that have helped me in the past: https://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html
https://docs.opencv.org/3.1.0/d5/dc4/tutorial_adding_images.html
Hope this helps!
I am using the image_to_string function in the pytesseract package to convert multiple parts of a single picture file to string. All parts are working except for this image:
Here is the script that I am using to convert it:
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
im = Image.open('image.png')
text = pytesseract.image_to_string(im)
print(text)
Which gives the output:
—\—\—\N—\—\—\—\—\N
I have tried breaking up the image into smaller parts as well as processing the image as a jpg and as png. What can I do to have it output the values in the image?
Using a different page segmentation instead of the default one seems to work.
text = pytesseract.image_to_string(im,config ='--psm 6'))
According to the tesseract wiki, option 6 assumes a single uniform block of text. I tried with other options but only this one worked.
To check for other page segmentation methods read the tesseract wiki on how to improve quality of an image.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is there an easy way to create a "movie" by stitching together several plots, within R?
Here is one method I found using R help:
To create the individual image frames:
jpeg("/tmp/foo%02d.jpg")
for (i in 1:5) {
my.plot(i)
}
dev.off()
To make the movie, first install ImageMagick.
Then call the following function (which calls "convert", part of ImageMagick I suppose):
make.mov <- function(){
unlink("plot.mpg")
system("convert -delay 0.5 plot*.jpg plot.mpg")
}
Or try using the ffmpeg function as described in this article (I've found this gives cleaner results):
ffmpeg -r 25 -qscale 2 -i tmp/foo%02d.jpg output.mp4
May require a bit of tinkering, but this seemed pretty simple once everything was installed.
Of course, anywhere you see "jpg" or "jpeg", you can substitute GIF or PNG to suit your fancy.
Take a look at either the animation package created by Yihui Xie or the EBImage bioconductor package (?animate).
I think you can do this also with the write.gif function in the caTools library. You'd have to get your graph into a multi-frame image first. I'm not sure how to do that. Anyone? Bueller?
The classic example of an animated GIF is this code which I didn't write but I did blog about some time ago:
library(fields) # for tim.colors
library(caTools) # for write.gif
m = 400 # grid size
C = complex( real=rep(seq(-1.8,0.6, length.out=m), each=m ), imag=rep(seq(-1.2,1.2, length.out=m), m ) )
C = matrix(C,m,m)
Z = 0
X = array(0, c(m,m,20))
for (k in 1:20) {
Z = Z^2+C
X[,,k] = exp(-abs(Z))
}
image(X[,,k], col=tim.colors(256)) # show final image in R
write.gif(X, 'Mandelbrot.gif', col=tim.colors(256), delay=100)
Code credit goes to Jarek Tuszynski, PhD.
If you wrap your R script within a larger Perl/Python/etc. script, you can stitch graphs together with your favorite command-line image stitching tool.
To run your R script with a wrapper script, use the R CMD BATCH method.
I'm not sure it is possible in R. I did a project once when data points from R were exported to a MySQL database and a Flex/Flash application picked up those data points and gave animated visualizations..
I've done some movies using XNview's (freeware graphics viewer) Create Slideshow function. I wanted to show trends through time with spatial data, so I just created a series of plots, named sequentially [paste() is your friend for all sorts of naming calistethics] then loaded them into XNviews slideshow dialogue and set a few timer variables, voila. Took like 5 minutes to learn how to do it and produce some executable graphics.
Here's a full example on making an animated GIF "movie" from an HDF5 file. The data should be an HDF Dataset of a 3 dimensional array [Nframes][Nrows][Ncolumns].
#
# be sure to be run as Administrator to install new packages
#
source("http://bioconductor.org/biocLite.R")
biocLite("rhdf5")
install.packages('caTools')
install.packages('fields')
library(caTools)
library(fields)
library(rhdf5)
x = h5read(file="mydata.h5",name="/Images")
write.gif(x,"movie1.gif",col=rainbow,delay=10,flip=TRUE)