Hi, everybody! A while ago I started making some changes to David Tschumperlé’s “Add Grain” filter for my own purposes, as I wanted to use grain samples other than the ones provided with that filter. I ended up doing a little bit more than that, and David kindly helped me get there, by providing ideas of how I could achieve what I wanted, plus offering some working code and suggestions. Of course this filter is not guaranteed to meet anyone else’s needs, and it may contain a few bugs and not cover use cases that may be relevant to other users (for example, I don’t really care for grain in colour images, so my tests concerned only B&W photos and grain). Some remaining problems stem directly from my lack of proficiency in the G’MIC language.
David has uploaded the code to the gmic-community repository. After updating the filters in G’MIC-Qt it should appear under “Testing/Marcos Capelini”.
As to the grain pattern, users are supposed to use their own, since none are provided with the filter itself. For testing purposes, I’ve attached to this topic a grain sample image with grain simulated using Darktable’s “grain” module. Just download the file and save it somewhere on the file system where the G’MIC-Qt and the host application (GIMP, Krita,…) can access it. Then it’s just a matter navigating and selecting the file via the “Grain pattern file” parameter.
Actual film scans will most likely give better results than the DT simulation, and you may be able to find some free to download through a simple Google search. If you want a more or less faithful rendering of the grain size, make sure your sample files are crops (e.g. 500x500 or 1000x1000 px) of 35mm frames at 24 MP (6000x4000 px), or that the full grain images are resized to 24MP prior to cropping.
The images on to which the grain is to be applied can have any size: the “Film Grain” filter will make sure the selected grain pattern is properly scaled (down scaled if the image width is less than 6000px, up scaled if the image is wider than that). The “Scale” parameter should then give a reasonable reproduction of the original grain size when set to “100%”.
That’s about it! I hope this little filter can be useful to other G’MIC users. Please let me know if you find any issues, or have any ideas for improvement.
Thanks! By the way, yesterday I made some corrections as I caught a few bugs when testing the filter with a variety of my own photos, so you may want to update your filters before testing.
Bugs corrected:
An extra layer (the grain layer) was left when applying the filter
Grain scaling calculation was wrong for portrait-oriented images
Error when loading grain file pattern if path (directory or file name) contained spaces
New feature:
Now the filter accepts a full-sized grain scan of any resolution (file must be at least 6MP) instead of only crops of 24MP grain scans.
Concerning the latter, if a full frame scan is provided, the filter will internally crop the grain sample image to 1000x1000 (so the remaining logic remains untouched), but will use the longest dimension to infer the scan resolution for grain scaling.
As noted before, the scanned (or synthesized) grain is always assumed to come from a 35mm full frame image. When applied to an image, the purpose of the internal grain scaling is to render the grain size characteristics close enough to what it might have been if the photo were taken with a 35mm film camera.
Also, no matter what your input grain pattern may be (a crop of 24MP scan, or a full-frame scan of any size), the applied grain is always synthetic. The default texture generating algorithm (G’MIC’s ‘syntexturize’) is quite fast, and gives a good enough resemblance of the original grain in most cases. The other option (“Pattern Match”) uses the ‘syntexturize_matchpatch’ command, which is rather slow, but I feel the grain reproduction is more faithful to the original grain pattern.
New option to select an output Film Format preset (35mm, 35mm half-frame, 110, 120, APS). This will scale the grain to approximate the appearance of the selected film pattern for a given film format.
Option to disable grain normalisation, or to use ‘cut’ instead of ‘normalize’
If grain pattern is detected as full-frame scan, grain sample now is taken off the centre of image instead of from top-left corner.
Highlighting the new options:
Examples of the same grain pattern with the default film format (35mm) preset, and the “110 (Pocket Instamatic)” preset:
After a few iterations which I didn’t bother to announce here, I consider the filter “finished” at last (as in “it does what I need”), and I have run out of ideas and time for new features. Thanks to everyone who helped me have it working as I intended, especially David Tschumperlé who not only created G’MIC but is always supportive in helping the novice, correcting the few bugs, adding new features, and working behind the scenes to make G’MIC what it is! I don’t think I’ll ever really have a good grasp of the language, but I keep reading all the posts by the experts here in the hope that it will eventually make a little more sense to me…
Things that I have added since the previous post:
Image adjustments:
Blur/Sharpen: Input images can be blurred or sharpened before adding the grain. Often I find that a little bit of extra softness can give more pleasing results, especially when the input image is sharper than the applied grain. Or the image may be too soft and may benefit from an extra amount of sharpening to match the gran characteristics.
Highlight compression: Sometimes a photo may have areas of completely or partially blown highlights, and this limits the amount of grain that will be visible in these areas after blending in the grain. A little bit of compression in the highlights can improve this. This is achieved by a gentle curve that brings down the values between 192~255.
Micrograin:
When the grain is enlarged as a result of the “Film Format” selection (smaller formats=larger grain), the grain “Scale” amount, or the current image resolution (grain is enlarged if image is larger than the assumed or calculated grain frame resolution), it can look too coarse and not natural, especially if the image is too sharp (see section above). The “Micrograin” parameter attempts to mitigate this by downscaling the original grain to 1/3 size, and blending the result with the enlarged grain, before synthesising the final grain. In the end the effect may be too subtle and not really help much. You be the judge…
Usage tips:
I don’t think there’s much a G’MIC-Qt filter designer can do to provide guidance on how to use a filter, or to give tips or insights on how the filter is supposed to work. What I came up with was using the preview area and a button to present the user with a block of text. Of course the amount of text is limited to the preview area (that will be different for each user), there’s no way to scroll the text, etc.
There have been some internal changes to how the grain is scaled, sharpened, etc. I don’t think there’ll be any noticeable external side effects of these changes.
Here’s the current appearance of the filter, with the new features highlighted:
Edit: I found the grain sample shown above (“Kodak TMax.jpg”) free for download on the web. There are other grain samples available, all quite good indeed, and they work very well with the “Film Grain” filter! Do a search for “scanned film grain” for example, with your favourite search engine and you will find it quite easily. Not having contacted the commercial web site that makes the grain pack available free of charge, I don’t feel comfortable linking it directly here though.
Edit 2: Sorry, I didn’t know editing an old message would “bump” it to the top! Nothing new in the messages above, please disregard the update… Someone asked me where I’d got the “Kodak TMax.jpg” grain sample, so I thought I should mention it here too…