OSX: automated (every 1-2sec) screenshot (not full screen but (x,y,w,h)) using python - macos

I want to make screenshots on OSX using python. I dont want make full screen shots but only certain rectangles on the screen. Something like (291,305,213,31). I need the correct pixel because afterwards the image files are processed by OCR (python-tesseract) to extract the text.
By the way this is since 6 years the first time I am programming, so far I only know Java a bit. I started yesterday and gave up this morning at 4am. So basically I have no clue yet...For example I still cannot build with Sublime because of path settings, but thats a different story. Cant figure out everything on one day.
I was trying already the following:
- wxPython
But the result are black images, see also:
stackoverflow.com/questions/8644908/take-screenshot-in-python-cross-platform
Additionally it only works in 32-bit mode, but when I do OCR using python-tesseract openCV requires 64-bit....
autopy
when trying to install I got errors, see also:
stackoverflow.com/questions/12993126/errors-while-installing-python-autopy
ImageGrab
only Windows
effbot.org/imagingbook/imagegrab.htm
commandline screencapture
os.system('screencapture test.png')
When I found this I thought, nice but only fullscreen when checking man screencapture. But then I found this: guides.macrumors.c om/screencapture
-R capture screen rect
That would be already enough, but on OSX 10.7.5 I dont have this option. Any ideas?
import Quartz.CoreGraphics
neverfear.org/blog/view/156/OS_X_Screen_capture_from_Python_PyObjC
Create screenshot as CGImage
image = CG.CGWindowListCreateImage(
region,
CG.kCGWindowListOptionOnScreenOnly,
CG.kCGNullWindowID,
CG.kCGWindowImageDefault)
Unfortunately the image is not in file format but a CGImage, no idea how to save as file.
So if possible I would like to use the commandline screencapture with -R if somebody knows how. Just as a start to continue.
Are there any other command line tools available?
What about other libs that I have missed?
Cheers
M

Given that you can get a CGImageRef, you can get its pixel data using the techniques described in Technical Q&A QA1509: Getting the pixel data from a CGImage object. In particular, it shows a function to get the pixel data as a CFDataRef using this function:
CFDataRef CopyImagePixels(CGImageRef inImage) { return CGDataProviderCopyData(CGImageGetDataProvider(inImage)); }
and says:
The pixel data returned by CGDataProviderCopyData has not been color
matched and is in the format that the image is in, as described by the
various CGImageGet functions …
It shows an alternative for getting the pixel data in other formats if you need that.

Related

Getting Julia to talk to Mac Plotting Devices

Running julia code from the command line, cannot get plots to show.
Simple example:
using ImageView
using Images
img = load("../images/cat.jpg")
imshow(img)
In a notebook
img shows the image, but running the above script from the command line, the code runs without fail, but no plot is ever visible.
Have searched, but have not been able to find documentation on how to get it to talk to, e.g., Quartz (like R does) or other plotting device.
Any help or redirecting to resources would be greatly appreciated.
Thanks!

GhostScript Image Quality issue while Printing

Am using GhostScript.Net 1.2.0 version. Am converting a pdf file into list of images to print. My Printed image height and width is fine but the printed image quality is poor. Please help me how to improve the image quality while converting a pdf to image using ghostscript.net
You need to either take this up with the Ghostscript.Net maintainer or find some way to tell us what command line/configuration you are using (ALL of it!), you will also need to supply an example file and define what you find objectionable in your current prints. 'image quality is poor' is extremely subjective, not helpful at all, there could be many, many reasons for 'poor quality', starting with your input file.
You also need to state what operating system you are using, and what your printing setup is. If you have tried anything already, then you need to say what you have done or we will waste much time suggesting dead ends.
Note that if you are using the mswinpr2 device, there may be little that can be done as that relies on the printer driver in the Windows system to do the actual printing.

TIFF16 image looks different in windows file viewer and MATLAB

General problem description
I have 33 TIFF16 images and I want to do some processing on them using MATLAB. So reading them is the first step. After I download an image from the web and then try to read it using MATLAB's imread (as well as Tiff and read). I display the image using imshow. The image displayed by the Windows File Viewer and MATLAB is totally different. I cannot process them since I don't trust MATLAB has read them correctly. I give more specifics of the problem now.
EDIT: If it helps, the details of the TIFF16 images are: TIFF (16 bits per channel, ProPhoto RGB color space, lossless compression)
More details:
I download an image a0008-WP_CRW_3959.tif. Destination: Go to this link -> img0008 -> Expert B (In case somebody wants to try, otherwise I have screenshots below).
I read that image in MATLAB using: img=imread('imgFilename.tif','tiff'); imshow(img,[]); or
t = Tiff('imgFilename.tif','r');
imageData = read(t);
imshow(imageData);
Now, I display the snapshots of Windows file viewer:
Next, snapshot of what MATLAB shows me:
Now, I have a good reason to believe that Windows file viewer is correct. Go the same link as previous. Scroll down to img0008. Hover your mouse onto the leftmost img0008. A thumbnail view of Expert B will come which looks same as what Windows shows me.
Does anybody know how to make MATLAB read and show the tiff16 image correctly?
Thank you #MarkRansom for pointing me to the embedded color profile possibility. I believe the following solution is correct and produces the same output as Windows File Viewer.
First read the icc-color-profile using iccread command.
I_rgb = imread('a0008-WP_CRW_3959.tif');
outprof = iccread('sRGB.icm');
P = iccread('a0008-WP_CRW_3959.tif');
Then convert the image into sRGB profile using makecform and applycform:
C = makecform('icc',P,outprof);
I_cmyk = applycform(I_rgb,C);
imwrite(I_cmyk,'pep_cmyk.tif','tif')
info = imfinfo('pep_cmyk.tif');
imshow('pep_cmyk.tif');
The original image saved on disk and the new - pep_cmyk.tif - look exactly the same with Windows file viewer.

MATLAB newbie: error using "saveas" to save a plot as a pdf file

I am a Mac (10.6.8) user. I have written MATLAB code for plotting calculation results and then saving the plots as pdfs. I use "saveas" (see example below).
I get this error:
??? Error using ==> print at 325
Problem converting PostScript. System returned error: -1.Failed to convert to output format; Ghostscript status: -100.**** Unable to open the initial device, quitting.
Error in ==> saveas at 155
print( h, name, ['-d' dev{i}] )
Error in ==> Results_processor at 1219
saveas(gcf,saveFigTo1, 'pdf')
Here is the relevant piece of code:
calculationResultsPath = '/Me/Projects/ThisProject';
calculationResultsDirectory = strcat( calculationResultsPath,'MATLABProcessedResults' );
mkdir( calculationResultsDirectory );
% ...Code for importing results to be plotted from external files (works fine)...
% ...Code for plotting (works fine)... I get the figures I want.
% The problem is:
saveFigTo1 = strcat(resultsDirectory,'/majorsMgO.pdf')
saveas(gcf,saveFigTo1, 'pdf')
hold off
pause
clf;
Some further information... This worked fine last week when I first wrote it! Since then, I think I updated to Mac OS 10.6.8 from 10.6.7, but nothing else has changed in my code or in the version of Matlab I use (R2009a) (unless I have a very bad memory!).
Also, I came across some older suggestions on similar questions to use "print." I tried using:
print(gcf,'filename'). I did get a pdf, but it would not open in any pdf viewing programme. I assume (but don't know for sure) that this could be related to the fact that I use a Mac. I have noticed that there are a few things (especially related to external file manipulation) that don't run on Mac.
If anyone could help, I would be very grateful.
Update:
I found a GhostScript for Mac and installed it as Chris suggested. Unfortunately, that didn't work. I read on a forum that many Mac users are having problems with MATLAB plots at present, possibly related to java. There was an operating system update (to OS X 10.6.8) last week and that is when the problems began. My code worked before then.
I still have not found a solution to this and I don't think the MATLAB people have either, so if anyone has suggestions about how to save plots WITHOUT using saveas, I would love to hear them. The "print" command does not work for me either--it produces PDFs that I can't open.
I think the problem here is GhostScript dying and not so much Matlab. A Google for that GS error turns up many pages such as this. Is this at all applicable? If you use GS outside of Matlab does it work?
As an aside, you might take a look at this FEX submission export_fig. It has treated me well. Worst case you could output to png and convert to PDF later.
One recommendation I can make is to use a different format that OS X understands and simply convert the result to pdf with a system call
See if the following works:
% Load a test image
im = imread('cameraman.tif');
imshow(im); % display the image
saveas(gcf,'test.tif','tif');
% convert to pdf using a syscall to cupsfilter
!cupsfilter test.tif > test.pdf 2> /dev/null
% open the file with your default pdf viewer
!open test.pdf
If the above is not working, yet another approach is to get the bitmap from the figure window and write it using imwrite. Note, this method doesn't benefit from the nice font scaling features of the saveas and print. This approach assumes the im variable above still exists.
imagesc(im); colormap gray;
% Set the border color to white
set(h,'Color',[1 1 1]);
% Get the image in the figure
frame = getframe(gcf);
imout = frame.cdata;
% on OS X, the stretch window image
% appears in the bottom right corner
% of the image. Remove it.
imout = imout(10:end-9,10:end-9,:);
% Write the image out to a lossless tif
imwrite(imout,'test.tif','tif','Compression','none')
You could then convert the tifs to pdf files as above. The quality of the figure will depend on the figure size. In most cases, I wouldn't use this second approach as saveas handles the fonts nicely. Using getframe is meant only as a workaround to the true problem with saveas.

How to import GIF files into Beamer presentation?

I need to import animations from Maple into my LaTeX/Beamer presentation. I save a file in GIF format. But later I have problems converting that file into PNG. All I get is a static PNG file and can't proceed ((( What's the full code to do that in LaTeX?
You can use the animate package to animate a series of PNGs. To get the series of PNGs from an animated GIF, use a tool like ImageMagick's convert.
Does this help: LINK? (This is the same answer as marcog... just wanted to provide a reference to it being asked previously -- the solution was the same: the animate package).
Also, your OS will matter. I don't know that Linux (not saying you're using it) has any ability to play animated PDFs. I've tried embedding movies using LaTeX and while it "works," you can't actually view them in anything Linux offers yet. Okular is working on it, but last I checked (couple months?) it's not possible yet.
Anyway, just wanted to add that just in case you were doing everything completely right and by chance are not seeing the fruits of your labor since you're using a Linux viewer. Check your work with Acrobat on Windows to be sure.

Resources