Posts Tagged ‘photoshop’
SPARticus
Sunday, May 13th, 2012Subtracting 1 pixel from outline of every frame in animated gif
Tuesday, May 1st, 2012When I overlay animations on background images there’s often a a thin outline of white or near white coming from the texture mapping + anti-aliasing in my application.

I came up with two easy ways to get around this in photoshop. First just add a black stroke to every frame. This works well for cartoons:

The other option is very similar. Add a stroke to each layer but instead of “adding” the stroke color, subtract it from the image. Select the blending options in the stroke layer style to look like this:

which produces something like:

Matlab face/edge alpha and phong lighting
Wednesday, February 29th, 2012Turns out matlab’s figure renderers can’t handle transparency and phong lighting simultaneously. I noticed this when trying to have transparent edges overlaid on a phong shaded surface.
Matlab documentation reads:
You do not specify Phong lighting (OpenGL does not support Phong lighting; if you specify Phong lighting, MATLAB uses the ZBuffer renderer).
Or
Figure objects use transparency (OpenGL is the only MATLAB renderer that supports transparency).
So that’s that. Hopefully Matlab will fix this in the future. For now I will probably just composite figures in Photoshop if need be.
Open .tga files with alpha channel in photoshop
Monday, September 5th, 2011Here’s the tedious step by step to open a .tga file with transparency in photoshop as if it were a .png file:
- Open your image.tga
- In the main menu at the top choose Layer > New > Layer from Background… then click OK
- Open the Channels dialog: Window > Channels
You should see:
- RGB
- Red
- Blue
- Green
- Alpha (unchecked)
If you don’t see the Alpha channel then your .tga file doesn’t have one
- Command+click on the alpha channel’s thumbnail (or select the Alpha channel and click the little dashed circle at the bottom of the channels dialog “Load channel as selection”)
- Choose Layer > Layer Mask > Reveal Selection
- With the layer mask selected choose Layer > Layer Mask > Apply
- In the the Channels dialog, trash the Alpha channel
Extract full resolution image from PDF using Illustrator and Photoshop
Tuesday, July 12th, 2011I had the problem that when kinkos scanned my artwork they gave me a PDF. This doesn’t make a whole lot of sense because the scan is a raster image. It’d be much better off in PNG or in many cases JPG. I tried just opening the PDF in Preview or Photoshop and saving as a PNG but when prompted for the resolution I wasn’t sure what to put. What was the resolution of the PDF? And what was the resolution of the embedded image? Then I tried opening the PDF in illustrator, selecting the image and pasting it as raw pixels into a new photoshop document. This didn’t work either since the image was embedded into the PDF in a scaled form so the copied image was too small. My final solution is as follows:
In illustrator
- Select the image object
- Open View > Document Info > Embedded Objects
- Record the size of the image
- Copy the selected image
In Photoshop
- Create a new document (this will be the correct dimensions of the copied object, but perhaps the wrong resolution)
- Paste the object, but as a Smart Object
- Choose Image > Image size…
- Enter the size of the image, previously recorded
- Choose Layer > Flatten Image
Update: As suggested in the comments it’s much easier to use pdfimages to extract embedded images. I made a follow up post which does this then uses imagemagick’s mogrify to convert the files to pngs.
Update: Also, (see the comments), this is not even the easiest way to a get a single image from a pdf into photoshop.
Convert vector graphics into raster without anti-aliasing, using Illustrator and Photoshop
Thursday, March 3rd, 2011I recently made an image in Photoshop using the Shape Tool. Then I want to save the image at a certain size >b?without anti-aliasing. This turns out to be very difficult. I couldn’t find a way to save/rasterize the shapes I’d already drawn with anti-aliasing turned off just in Photoshop.
The way I finally did it was:
- Photoshop: Save the image as a .psd
- Illustrator: Loading that .psd and save as a .ai
- Photoshop: Load that .ai, when prompted uncheck anti-aliased and pick your size
Screen capture into photoshop
Monday, January 10th, 2011Making figures for our upcoming submission I find my self repetitively taking screen captures of my program and then pasting them into photoshop where I can crop and arrange them. This is a lot of clicking around and switching apps and it’s hard to take a sequence of shots quickly. I could make just write all the screen shots to file and load them into photoshop as a batch. But Photoshop’s batch loader script is slow, so pasting is the fastest way to get all the images as layers and move on to editing.
Here’s a script that stays open with a prompt asking whether to take another screenshot and dump it into the current photoshop document. Replace MYAPP with the name of the app your are trying to screen capture.
repeat
tell application "MYAPP"
activate
display dialog "Screen capture and paste into Photoshop?"
end tell
do shell script "screencapture -c"
tell application "Adobe Photoshop CS5"
activate
tell current document
paste
end tell
end tell
end repeat
Autocrop images in MATLAB like Photoshop’s Trim tool
Friday, November 5th, 2010I wrote a little MATLAM script called imtrim.m that automatically crops images like Photoshop’s Trim tool. It removes pixels on all sides of the image that match either the top-left corner of the bottom-right corner. Here’s the script I save in imtrim.m:
function [out1,out2,out3,out4,out5] = imtrim(im,location)
%IMTRIM auto-crop an image like Photoshop's Edit>Trim feature, as of yet only
%grayscale imagse are supported
%
% cropped = IMTRIM(IM) crop image based on top left corner
%
% [cropped,t,b,l,r] = IMTRIM(IM) return cropped image and indices used to
% crop the image. So cropped = im(t:b,l:r);
%
% [t,b,l,r] = IMTRIM(IM) return only indices used to crop the image. So
% cropped = im(t:b,l:r);
%
% [...] = IMTRIM(IM,location) same as above but location may specify
% top-left corner ('NorthWest') or bottom-right corner ('SouthEast') to be
% the picel used in determining the auto-crop
%
% Copyright Alec Jacobson, 2010
%
if(~exist('location'))
location = 'NorthWest';
end
% gather corner value to which the image is compared
if(strcmp(location,'NorthWest'))
corner_value = im(1,1);
elseif(strcmp(location,'SouthEast'))
corner_value = im(1,1);
else
error([location ' is not a valid location']);
end
% hard-coded threshold parameter, works equivalently with Photoshop's
% hardcoded parameter
threshold = 0.1;
% get difference of image with corner value
%difference = abs(im - corner_value)>0.1;
% should work for any number of channels
difference = sqrt(sum((im - corner_value).^2,3)) > ...
sqrt(threshold^2*size(im,3));
[left_i,left_j] = ind2sub(size(difference),find(difference,1));
[right_i,right_j] = ind2sub(size(difference),find(difference,1,'last'));
[top_j,top_i] = ind2sub(size(difference'),find(difference',1));
[bottom_j,bottom_i] = ind2sub(size(difference'),find(difference',1,'last'));
if(nargout == 1)
out1 = im(top_i:bottom_i,left_j:right_j);
elseif(nargout == 5)
out1 = im(top_i:bottom_i,left_j:right_j);
out2 = top_i;
out3 = bottom_i;
out4 = left_j;
out5 = right_j;
else
out1 = top_i;
out2 = bottom_i;
out3 = left_j;
out4 = right_j;
end
end
Clean up pen-on-paper line drawings
Friday, November 5th, 2010The most mechanical and mundane behavior I find myself doing over and over again involves cleaning up my pen and paper line drawings before importing them into Illustrator and turning them into vector graphics.
My usual procedure is:
- Draw the lines with a real pen on real paper
- Scan or photograph the drawing

This produces a very raw image of the line drawing.
- Use Curves and the Magic Wand Tool in Photoshop to reduce the image to black lines on a white background
- Import into Illustrator, use Live Trace with the One Color Logo preset to turn the lines into vector paths. Then color with the Live Paint Bucket.
And in the end I can make something like this:

This process is not very hard, but it’s rather boring. The fun parts are step #1 and #4, the drawing and coloring steps. Cleaning up the raw images in photoshop is tedious. Sometimes it can be very quick: 5 mins or so if you include opening and closing photoshop. Other times, if there are too many shadows in the image or the lines aren’t dark enough or there are too many spurious smudges and marks, I have to spend much more time in photoshop: 10-15 mins. Or worse, I have to retake the photo/scan and start over.
There are two ways I thought of solving this problem:
- Make better photographs/scans, so cleaning up in Photoshop is easier
- Automate the Photoshop tasks
For #1, it seems that just buying a nice scanner would suffice. But getting the scanner hooked up is slow and I can’t carry the scanner around with me everywhere. I’ve also noticed that scanners tend to collect hairs and dust that appear as little marks on the image. Instead I’m working on a way to take better pictures of paper with my crappy cell-phone camera. But that’s another post…
This post will focus on option #2. The main problem with the Photoshop steps is not even that it takes so long, it’s that it needs my attention the entire time. It’s in no way automatic. Scripting Photoshop is possible, but actually scripting the curves steps that I do would be too hard and not general enough.
Automatic line extraction
I’ve come up with the following idea. I will bank on the assumption that the input image is an image of a line drawing on a piece of white paper. The whole problem stems from the fact that shadows and imperfections on the paper cause the paper to appear unevenly gray. If only I had another image of just the paper without the line drawing I could subtract the paper image from the (line + paper) image, leaving just the lines.

I wrote up a proof of concept in MATLAB that performs the following steps:
- Find edges in the image (note that this is different than finding lines, edges in our case occur on either side of a pen-on-paper line).
- Blur these edges using a blur kernel whose width is parameterized based on the expected thickness of the lines (smaller for ball-point pens, larger for thick markers).

- Threshold all values over some small amount in the blurred edges mask, also get rid of tiny regions.

- Fill use this mask as “holes” to be filled in via Laplacian hole-filling. Notice we now have an approximation of the blank sheet of paper:

- Subtract the paper from the original image and intensify the lines so the image is black on white (but not necessarily a binary image):

Results
Here are some results. I compare the original photograph or scan of the line drawing with my best clean up job using (only) Curves in Photoshop and my automatic script. For my script I am not adjusting the parameters for each input. I have found a set of parameters that work well for pen on paper inputs and I used this preset for all the images shown in the table.
| Original | Photoshop Curves | MATLAB script |
|---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Note: I have implemented my MATLAB script to support all 3 RGB color channels so that color pencil/pen drawings still come out in color.
Download: Download the MATLAB source for line drawing clean up algorithm.



























